Samba,是种自由软件,用来让UNIX系列的操作系统与微软Windows操作系统的SMB/CIFS(Server Message Block/Common Internet File System)网络协定做连结。在目前的版本(v3),不仅可存取及分享SMB的资料夹及打印机,本身还可以整合入Windows Server的网域、扮演为网域控制站(Domain Controller)以及加入Active Directory成员。简而言之,此软件在Windows与UNIX系列OS之间搭起一座桥梁,让两者的资源可互通有无。
简而言之:samba服务器就是可以让linux和windows都可以使用的共享服务。
yum install samba-common.x86_64 samba.x86_64 samba-client.x86_64 -y
systemctl stop firewalld.service
rpm -qc samba-common
vim /etc/samba/smb.conf
[mac] ##共享名称
path = /westos ##实际共享目录
semanage fcontext -a -t samba_share_t '/westos(/.*)?' ##修改共享目录的安全上下文
restorecon -RvvF /westos/ ##目录全部内容生效
getsebool -a | grep samba ##查看selinux策略samba相关策略布尔值
setsebool -P samba_enable_home_dirs on ##允许进入samba的用户家目录
smbpasswd -a student ##添加samba用户,student 必须是本机用户,输入相关密码,用于客户端登录
pdbedit -L ##查看用户列表
systemctl restart smb
smbpasswd -a student ##添加samba用户,student 必须是本机用户
useradd westos
useradd lee
smbpasswd -a westos ##添加westos用户
smbpasswd -a lee ##添加lee用户
pdbedit -L ##查看用户
pdbedit -x student ##删除用户,如果需要
在客户端测试:
yum install samba-client.x86_64 -y
smbclient -L //172.25.254.125
smbclient -L //172.25.254.125 -U student
smbclient //172.25.254.125/mac -U student
mount //172.25.254.125/mac /mnt -o username=student,password=redhat
[root@client /]# mount //172.25.254.125/mac /mnt -o username=student,password=redhat
[root@client /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3223980 7249920 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 80 942568 1% /dev/shm
tmpfs 942648 17016 925632 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
//172.25.254.125/mac 10473900 3209144 7264756 31% /mnt
此时,进入共享目录/mnt,只读,不可写,如何可写呢?看下面的参数配置。
这些参数大多数写在配置文件的底部
参数 | 说明 |
---|---|
comment | 添加说明 |
browseable | 添加隐藏,是否可以查询到,(默认是yes,可以查看)但是否隐藏都可以用 |
valid users=具体用户 | 添加有效用户,其他用户被拒。+或@westos用户,其组成员可以登陆 |
writable=yes | 目录可写 |
write list | 可写用户列表(只有列表里用户可写) |
guest ok = yes | 匿名用户登陆 |
全局 :所有的程序都能识别匿名用户 | |
map to guest= bad user | 匿名用户可以挂载 映射到用户=bad user |
admin users = student | 提升用户级别:在使用student登陆了samba之后,就对于这个目录相当于root身份 |
workgroup | 修改工作域 |
注意:以下所有的修改都是在服务端修改samba的配置文件,测试都是在客户端测试。
[root@server westos]# vim /etc/samba/smb.conf
写入:
comment = this is share dic
[root@server westos]# systemctl restart smb.service
[root@server westos]# vim /etc/samba/smb.conf
写入:
browseable=no
[root@server westos]# systemctl restart smb.service
服务端:
vim /etc/samba/smb.conf
用;注释调browseable,添加有效用户
systemctl restart smb
这时,我们把用户lee加入到westo的附加组中,此时lee也是westos组的组成员,故也是有效用户,除此之外,都不是有效用户。
[root@server westos]# usermod -G westos lee
[root@server westos]# id lee
uid=1002(lee) gid=1002(lee) groups=1002(lee),1001(westos)
客户端测试:
测试westos用户:
[root@client ~]# smbclient //172.25.254.125/mac -U westos
Enter westos's password:
Domain=[MACBASKETBALL] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Sun May 26 01:31:45 2019
.. D 0 Wed May 29 02:54:45 2019
file11 N 0 Sat May 25 22:17:57 2019
file3 N 0 Sat May 25 23:21:35 2019
file4 N 0 Sat May 25 23:21:35 2019
file5 N 0 Sat May 25 23:21:35 2019
anaconda-ks.cfg A 8619 Sat May 25 23:35:34 2019
newfile N 0 Sun May 26 01:28:08 2019
leefile N 0 Sun May 26 01:31:43 2019
40913 blocks of size 262144. 28570 blocks available
测试lee用户:
[root@client ~]# smbclient //172.25.254.125/mac -U lee
Enter lee's password:
Domain=[MACBASKETBALL] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Sun May 26 01:31:45 2019
.. D 0 Wed May 29 02:54:45 2019
file11 N 0 Sat May 25 22:17:57 2019
file3 N 0 Sat May 25 23:21:35 2019
file4 N 0 Sat May 25 23:21:35 2019
file5 N 0 Sat May 25 23:21:35 2019
anaconda-ks.cfg A 8619 Sat May 25 23:35:34 2019
newfile N 0 Sun May 26 01:28:08 2019
leefile N 0 Sun May 26 01:31:43 2019
40913 blocks of size 262144. 28570 blocks available
测试无效用户student,连接被拒。
[root@client ~]# smbclient //172.25.254.125/mac -U student
Enter student's password:
Domain=[MACBASKETBALL] OS=[Unix] Server=[Samba 4.1.1]
tree connect failed: NT_STATUS_ACCESS_DENIED
我们先对共享目录进行一个挂载,当我们进入到挂载好的目录时,发现不能进行写操作。
[root@client ~]# mount //172.25.254.125/mac /mnt -o username=lee,password=lee
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3226164 7247736 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2355 451824 1% /home
//172.25.254.125/mac 10473900 3159872 7314028 31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
anaconda-ks.cfg file11 file3 file4 file5 leefile newfile
[root@client mnt]# rm -fr file11
rm: cannot remove ‘file11’: Read-only file system
我们返回服务端:
vim /etc/samba/smb.conf
写入:
writable=yes
保存后重启服务
[root@client mnt]# rm -fr file11
rm: cannot remove ‘file11’: Read-only file system
[root@client mnt]# rm -fr file11
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 leefile newfile
当然,用户可写是建立在目录可写的前提下,注意此时的可写列表中+或者@表示其用户组成员也可写。
服务端:
vim /etc/samba/smb.conf
写入:
write list =@westos
保存后重启服务
客户端测试:
当使用westos用户组成员lee挂载时,可以对共享目录写。因为我们刚刚就是用lee挂载,所以直接测试,发现可写。
[root@client mnt]# touch filelee
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee leefile newfile
我们卸载共享目录/mnt,再通过samba用户westos挂载,测试是否可写。发现也可写,因为再可写用户列表中。
[root@client ~]# mount //172.25.254.125/mac /mnt -o username=westos,password=westos
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3226316 7247584 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2355 451824 1% /home
//172.25.254.125/mac 10473900 3160012 7313888 31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee leefile newfile
[root@client mnt]# touch filewestos
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee filewestos leefile newfile
同理,我们再测试不在可写用户列表里的student。
一开始,我们发现无法挂载,经过排错,发现是服务器smb.conf配置文件中,valid users没有注释,注释了就可以了。
[root@client ~]# mount //172.25.254.125/mac /mnt -o username=student,password=redhat
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3226180 7247720 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2355 451824 1% /home
//172.25.254.125/mac 10473900 3160148 7313752 31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee filewestos leefile newfile
[root@client mnt]# rm -fr filelee
rm: cannot remove ‘filelee’: Permission denied
发现是没有权限,被拒。
我们之前可以发现,在客户端无法使用匿名用户登陆samba共享目录,当修改配置文件中guest ok = yes参数时,重启服务后可以匿名登陆。
vim /etc/samba/smb.conf
[root@client mnt]# smbclient //172.25.254.125/mac
Enter root's password:
Domain=[MACBASKETBALL] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Wed May 29 03:43:07 2019
.. D 0 Wed May 29 02:54:45 2019
file3 N 0 Sat May 25 23:21:35 2019
file4 N 0 Sat May 25 23:21:35 2019
file5 N 0 Sat May 25 23:21:35 2019
anaconda-ks.cfg A 8619 Sat May 25 23:35:34 2019
newfile N 0 Sun May 26 01:28:08 2019
leefile N 0 Sun May 26 01:31:43 2019
filelee N 0 Wed May 29 03:40:06 2019
filewestos N 0 Wed May 29 03:43:07 2019
40913 blocks of size 262144. 28569 blocks available
我们之前挂载共享samba目录都是通过具体有效用户的用户名和密码的,我们现在可以通过修改配置文件参数,使得匿名用户也可挂载。
首先,在配置文件中找到[global],在此标示下面添加:
map to guest = bad user
在客户端测试:
先卸载之前挂载的,然后通过匿名挂载的方式尝试挂载。
[root@client ~]# mount //172.25.254.125/mac /mnt/
Password for root@//172.25.254.125/mac: ##直接按回车键就行
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3226192 7247708 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2355 451824 1% /home
//172.25.254.125/mac 10473900 3159892 7314008 31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee filewestos leefile newfile
[root@client mnt]# rm -rf leefile
rm: cannot remove ‘leefile’: Permission denied
发现挂载成功,但是发现,匿名用户不可以写。原因是,除了服务之外,还有共享目录权限问题,可以在服务端进行修改权限777,客户端匿名用户就可以写了,但是一般不建议这么做。
参数admin users = student,表示在使用student登陆了samba之后,就对于这个共享目录相当于root身份。
在服务端:
修改配置文件
vim /etc/samba/smb.cof
写入:
admin users = student
保存并重启服务
注意:此时可写用户列表只有westos用户组的用户可以,而student并不是。
在客户端测试:
[root@client ~]# mount //172.25.254.125/mac /mnt/ -o username=student,password=redhat
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3226196 7247704 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2355 451824 1% /home
//172.25.254.125/mac 10473900 3159916 7313984 31% /mnt
[root@client ~]# cd /mnt/
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee filewestos leefile newfile
[root@client mnt]# rm -rf leefile
[root@client mnt]# ls
anaconda-ks.cfg file3 file4 file5 filelee filewestos newfile
我们使用student用户挂载共享目录,但并没有可写权限,但是依然可以对目录进行写操作,是因为我们提升了student的用户级别为root用户,root用户当然可以写了。
我们之前的挂载方式是非常不安全的,因为在挂载时直接暴露出密码,而且某个用户挂载的内容其他用户也可以看的到,非常不安全。
我们可以在客户端安装可以实现多用户挂载的cifs服务,并通过man手册查看该服务的具体配置信息:
yum install cifs -y
man 8 mount.cifs
我们新建一个文件/root/smb_auth,里面写入服务端smb用户的用户名和密码,并修改文件的权限,默认情况下只有root用户可读写:
vim /root/smb_auth
写入:
username=westos
password=westos
chmod 600 /root/smb_auth
通过这种方式可以直接挂载,而不在历史记录中直接显示出密码:
[root@client ~]# mount //172.25.254.125/mac /mnt -o credentials=/root/smb_auth
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3216928 7256972 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2371 451808 1% /home
//172.25.254.125/mac 10473900 3160248 7313652 31% /mnt
为了解决上面的问题,结合man 8 mount.cifs,我们可以按照下面的格式挂载,表示认证的方式是采用多用户挂载,而且完美解决了安全问题:
[root@client ~]# mount //172.25.254.125/mac /mnt -o credentials=/root/smb_auth,sec=ntlmssp,multiuser
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3216924 7256976 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2371 451808 1% /home
//172.25.254.125/mac 10473900 3160012 7313888 31% /mnt
安全参数,可以参考man 8 mount.cifs
测试,发现其他用户不能查看:
如果我们想让其他用户查看,则需要得到一个服务端smb用户的身份才可以。
注意:必须是服务端已有的smb用户
[mac@client mnt]$ cifscreds add -u lee 172.25.254.125
Password:
[mac@client mnt]$ ls
anaconda-ks.cfg file3 file4 file5 filelee filewestos newfile
也就是说,前提你必须知道smb服务器smb用户的密码才可以看共享目录,更安全。
在客户端:
vim /etc/fstab
写入:
//172.25.254.125/mac /mnt cifs defaults,credentials=/root/smb_auth,sec=ntlmssp,multiuser 0 0
这个时候取消挂载,然后mount -a
,发现自动挂载,而且是多用户的认证安全模式的挂载!
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3216932 7256968 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
[root@client ~]# mount -a
[root@client ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3216932 7256968 31% /
devtmpfs 469332 0 469332 0% /dev
tmpfs 484920 80 484840 1% /dev/shm
tmpfs 484920 12752 472168 3% /run
tmpfs 484920 0 484920 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2371 451808 1% /home
//172.25.254.125/mac 10473900 3161260 7312640 31% /mnt