安装samba

yum install samba -y

配置Firewall-cmd与selinux

防火墙添加smb服务

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

关闭selinux(重启失效)

setenforce=0

配置samba的配置文件smb.conf

在 smb.conf 这个配置文件当中的设定项目有点像底下这样:

# 会有很多加上 # 或 ; 的批注说明,你也可以自行加上来提醒自己相关设定
[global]
   参数项目 = 设定内容
   ....

[分享资源名称]
   参数项目 = 设定内容
   ....

匿名登录配置方法

需求:配置Samba服务;新建/home/share目录,并将此目录共享给所在小组的Windows服务器;设置权限为只读

创建文件夹
mkdir /home/share
配置/etc/samba/smb.conf
[root@localhost home]# cat /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.

[global]
    workgroup = WORKGROUP       #工作组名称 需和windows一样
    security = user         #安全[登录方式]
    map to guest=Bad User       #无密码
    passdb backend = tdbsam

    printing = cups
    printcap name = cups        #共享的打印服务名称
    load printers = no      #加载打印机
    cups options = raw

[share]                 #分享资源名称
    comment = share dir     #简单介绍共享的资源
    path=/home/share        #本地目录路径
    browseable = yes        #允许被浏览到资源名称
    read only=yes           #只读
    guest ok=yes            #允许游客访问
[root@localhost home]#
启动服务
systemctl enable smb
systemctl enable nmb
systemctl start smb
systemctl start nmb

账户登录配置方法

需求
  • 修改工作组为WORKGROUP
  • 注释[homes]和[printers]的内容
  • 共享名为webdata
  • 共享目录为/data/web_data,且apache用户对该目录有读写执行权限,用setfacl命令配置目录权限。
  • 仅允许192.168.1XX.33的主机访问

创建文件夹
mkdir -p /data/web_data
使用setfacl命令配置目录权限
setfacl -m u:apache:rwx /data/web_data

setfaclの基本介绍

setfacl的用途

setfacl命令可以用来细分linux下的文件权限。
chmod命令可以把文件权限分为u,g,o三个组,而setfacl可以对每一个文件或目录设置更精确的文件权限。
换句话说,setfacl可以更精确的控制权限的分配。
比如:让某一个用户对某一个文件具有某种权限。

这种独立于传统的u,g,o的rwx权限之外的具体权限设置叫ACL(Access Control List)
ACL可以针对单一用户、单一文件或目录来进行r,w,x的权限控制,对于需要特殊权限的使用状况有一定帮助。
如,某一个文件,不让单一的某个用户访问。

setfacl的用法
用法: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
-m,       --modify-acl 更改文件的访问控制列表
-M,       --modify-file=file 从文件读取访问控制列表条目更改
-x,       --remove=acl 根据文件中访问控制列表移除条目
-X,       --remove-file=file 从文件读取访问控制列表条目并删除
-b,       --remove-all 删除所有扩展访问控制列表条目
-k,       --remove-default 移除默认访问控制列表
          --set=acl 设定替换当前的文件访问控制列表
          --set-file=file 从文件中读取访问控制列表条目设定
          --mask 重新计算有效权限掩码
-n,       --no-mask 不重新计算有效权限掩码
-d,       --default 应用到默认访问控制列表的操作
-R,       --recursive 递归操作子目录
-L,       --logical 依照系统逻辑,跟随符号链接
-P,       --physical 依照自然逻辑,不跟随符号链接
          --restore=file 恢复访问控制列表,和“getfacl -R”作用相反
          --test 测试模式,并不真正修改访问控制列表属性
-v,       --version           显示版本并退出
-h,       --help              显示本帮助信息

使用setfacl的几个例子

查看一个test文件的acl

#查看acl
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::r-x
user:tank:rwx                   #effective:---
group::r-x                      #effective:---
mask::---
other::---

修改之后再查看:

[root@localhost ~]# setfacl -m u:zhangy:rw- test    #修改文件的acl权限,添加一个用户权限
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::r-x
user:zhangy:rw-                       #多出来一个用户
user:tank:rwx
group::r-x
mask::rwx
other::---
[root@localhost ~]# setfacl -m g:zhangying:r-w test      #添加一个组
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::r-x
user:zhangy:rw-
user:tank:rwx
group::r-x
group:zhangying:rw-
mask::rwx
other::---

再看一个例子:

[root@localhost ~]# getfacl test     #查看acl
# file: test
# owner: root
# group: root
user::rw-
group::r--
other::r--

[root@localhost ~]# setfacl -m u:tank:rx test   #给tank用户向test文件增加读和执行的acl规则
[root@localhost ~]# getfacl test    #查看acl
# file: test
# owner: root
# group: root
user::rw-
user:tank:r-x      #已加入
group::r--
mask::r-x
other::r--

[root@localhost ~]# setfacl -m u::rwx test   #设置默认用户,读,写,可执行
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:tank:r-x
group::r--
mask::r-x
other::r--

[root@localhost ~]# setfacl -b test     #清除所有acl
[root@localhost ~]# getfacl test 
# file: test
# owner: root
# group: root
user::rwx
group::r--
other::r--

[root@localhost ~]# setfacl -m u:tank:rx test      #给tank用户向test文件增加读和执行的acl规则
[root@localhost ~]# setfacl -m u:testtank:rx test  #给testtank用户向test文件增加读和执行的acl规则
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:testtank:r-x
user:tank:r-x
group::r--
mask::r-x
other::r--


[root@localhost ~]# setfacl -x u:tank test    #清除tank用户,对test文件acl规则
[root@localhost ~]# getfacl test
# file: test
# owner: root
# group: root
user::rwx
user:testtank:r-x
group::r--
mask::r-x
other::r--

设置组的话只需要把setfacl -m u::rwx 中的u改为g即可,大致差不多。

设置mask的话,setfacl -m u::rwx 中的u改为m,并且这个可不针对用户和组哦,其他的大致差不多。

在使用-R时,记得放在-m前面,否则不可以地

使用-d的话,就会把默认的都加上去,针对目录哦。

配置/etc/samba/smb.conf
[root@localhost home]# cat /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.

[global]
    workgroup = WORKGROUP
    security = user
    passdb backend = tdbsam

    #printing = cups
    #printcap name = cups
    #load printers = no
    #cups options = raw

[webdata]
    comment = apache dir
    path=/data/web_data
    browseable = yes
    writeable=yes
    hosts allow=192.168.1**.33

重新启动smbnmb服务

systemctl restart nmb
systemctl restart smb

添加smb用户并激活

如果没有系统账户的话 先创建一个

useradd test
passwd test

然后添加进smb用户中

smbpasswd -a [用户名]      #添加smb账户并创建密码
smbpasswd -e [用户名]      #激活smb账户

大功告成~

最后修改:2021 年 04 月 01 日
如果觉得我的文章对你有用,请随意赞赏~