不关闭selinux,在CentOS8中安装和配置samba共享

2023-05-16

1. 安装samba,并设置为开机启动;开放防火墙对samba的限制 ;配置samba共享的内容

#下面两行,安装和设定samba开机启动
dnf install samba samba-client
systemctl enable --now {smb,nmb}

#下面几行,显示samba的有关防火墙配置和开放samba的端口
firewall-cmd --info-service samba
#显示的内容如下
samba
  ports: 137/udp 138/udp 139/tcp 445/tcp
  protocols:
  source-ports:
  modules: netbios-ns
  destination:

firewall-cmd --permanent --add-service=samba

firewall-cmd --reload
firewall-cmd --list-services
#显示的内容如下:
cockpit dhcpv6-client samba ssh

#下面是修改samba的配置文件
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
vi /etc/samba/smb.conf
#配置文件的内容
[global]
        workgroup = SAMBA
        security = user

        passdb backend = tdbsam

        printing = cups
        printcap name = cups
        load printers = yes
        cups options = raw
        map to guest = bad user #这一行配置是新添加的

# 下面的共享是完全开放的, 任何账户都可以进入, 不用密码
[Share]
        path = /home/share
        browseable = yes
        writable = yes
        guest ok = yes
        guest only = yes
        force create mode = 755
        force directory mode = 755 

#下面的是有限制进入账户名单的保密共享目录
[Secret]
        path = /home/Secret
        browseable = No
        read only = No
        valid users = vip userA userB
        write list = vip userA
        read list = vip userA userB
        create mode = 0774
        force create mode = 0774
        force directory mode = 0774 


#重启samba服务,使得配置生效
systemctl restart {smb,nmb}

"map to guest = bad user" 这个配置,会将使用错误的用户名以及不存在的用户名进行的登录,映射为使用默认的guest用户(默认情况下,nobody)进行登录和操作。 此配置的作用就是允许匿名访问而无需提供密码。 如果担心guest用户的权限过大,可以增加配置项进行限制。例如:"writeable = no" 可以令guest用户无法对文件进行修改。

2.为samba共享添加用户,并设定仅能由授权用户进入的共享

#增加没有家目录,也无法登录系统的空用户
useradd -M userA -s /sbin/nologin
#-M 选项是--no-create-home的简写形式,即不为该用户配置家目录;-s选项,是为用户指定一个shell,这里指定的是 /sbin/nologin ,也就是无法登录。
#不需要为该类用户在系统上设置密码,但是要为其设定samba中的登录密码。

#配置userA的samba登录密码
smbpasswd -a userA
New SMB password:
Retype new SMB password:
Added user userA.

#配置一个非guest用户的共享
[Files]
        path = /home/files
        guest only = no

#自动检验配置是否有问题
testparm

Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[Files]"
Loaded services file OK.
Server role: ROLE_STANDALONE

 

 3.与selinux相关的配置

进行第1,2步的设置后,用windows进行共享的映射,会出现无法找到该路径的提示,这就是selinux的权限限制在起到作用了。

#显示selinux当前的配置状况
getenforce
Enforcing
#简单的临时性关闭selinux
setenforce 0

#selinux的配置文件
cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing   #三种级别,在这里修改即可
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

为了使SELinux处于“enforcing”模式时,samba共享也能够正常工作,我们必须为共享目录和文件,进行适当的策略配置

chcon -R -t samba_share_t /home/files

采用这样的配置方式,更改在重启后是继续有效的;但是如果目录和文件存在默认策略,则新增加的文件和文件夹,将会应用默认设置,而不会使用上面的策略。 如果要对新增的文件也起到作用,则需要将规则添加到策略中。

#先安装配置命令
dnf install policycoreutils-python-utils
#下面是配置
semanage fcontext -a -t samba_share_t "/home/files(/.*)?"

由于使用了    (/.*)?"   ,该规则将应用于“files”目录的所有内容以及目录本身。 为了验证该策略是否起作用,可以通过列出系统上正在使用的标签来验证是否已将规则添加到策略中。

semanage fcontext -l | grep /home/files
#显示的内容
/home/files(/.*)?                                  all files          system_u:object_r:samba_share_t:s0

到这里为止,我们已经完成了一个有效的samba配置。

为了进一步调整samba配置,我们可能还需要设定selinux的布尔值。 例如,将现有的系统用户添加到samba中时,如果要共享其主目录,则必须启用专用的selinux的布尔值。

#列出与samba相关的所有布尔值
semanage boolean -l|grep samba
#内容可能如下:
#   配置项名称                  当前值   默认值         简要说明
samba_create_home_dirs         (off  ,  off)  Allow samba to create home dirs
samba_domain_controller        (off  ,  off)  Allow samba to domain controller
samba_enable_home_dirs         (off  ,  off)  Allow samba to enable home dirs
samba_export_all_ro            (off  ,  off)  Allow samba to export all ro
samba_export_all_rw            (off  ,  off)  Allow samba to export all rw
samba_load_libgfapi            (off  ,  off)  Allow samba to load libgfapi
samba_portmapper               (off  ,  off)  Allow samba to portmapper
samba_run_unconfined           (off  ,  off)  Allow samba to run unconfined
samba_share_fusefs             (off  ,  off)  Allow samba to share fusefs
samba_share_nfs                (off  ,  off)  Allow samba to share nfs
sanlock_use_samba              (off  ,  off)  Allow sanlock to use samba
tmpreaper_use_samba            (off  ,  off)  Allow tmpreaper to use samba
use_samba_home_dirs            (off  ,  off)  Allow use to samba home dirs
virt_use_samba                 (off  ,  off)  Allow virt to use samba

仍以我们要激活启用主目录共享,则对应的布尔值项目是samba_enable_home_dirs ,需要使用setsebool命令执行操作

setsebool samba_enable_home_dirs=1

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

不关闭selinux,在CentOS8中安装和配置samba共享 的相关文章

随机推荐