Linux_Samba服务器部署
一、CIFS
1.CIFS系统:Internet文件共享系统,也称服务器信;适用于MicrosoftWindows服务器和客户端的标准文件和打印机的共享系统息块(SMB)。
2.Samba服务:用于将linux文件系统作为CIFS/SMB网络文件进行共享,并将linux打印机作为CIFS/SMB打印机进行共享。
二.Samba安装
一.实验环境:在server(server)服务端:172.25.254.216
在desktop(client)客户端:172.25.254.166
二.安装
在server(server)服务端
[[email protected] ~]# yum install samba samba-client samba-common -y ## Samba-common -Samba的支持文件 samba-client -客户端应用程序 Samba -服务器应用程序
[[email protected] ~]# systemctl start smb
[[email protected] ~]# systemctl enable smb.service
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# netstat -antlupe | grep smb ##查看samba端口信息
在desktop(client)客户端
[[email protected] ~]# yum install samba-client -y
三.Samba用户管理
<1>添加与删除用户
在server(server)服务端
[[email protected] ~]# smbpasswd -a student ##添加smb用户,smb用户必须是本地用户
[[email protected] ~]# pdbedit -L ##查看smb用户信息
[[email protected] ~]# pdbedit -x student ##删除smb用户
[[email protected] ~]# setsebool -P samba_enable_home_dirs on ##在SElinux开启的情况下,修改sebool值,打开访问,在selinux中设定smb用户可以访问自己的家目录。
在desktop(client)客户端测试
[[email protected] ~]# smbclient -L //172.25.254.216 ##匿名用户登陆 -L显示
[[email protected] ~]# smbclient -L //172.25.254.216 -U student ##输入密码可以看到系统共享家目录信息 -U用户
[[email protected] ~]# smbclient //172.25.254.216/student -U student ## 进入到家目录里,若以student登陆无法显示共享内容,主要是因为Selinux的开启无法正常访问。
<2>临时挂载
在desktop(client)
[[email protected] ~]# cd /etc/
[[email protected] etc]# smbclient //172.25.254.216/student -U student
smb: \> put passwd ##只能上传所在目录的文件,其它目录的无法上传。无法建立文件。
在server(server)
查看是否由passwd
为了方便使用,将其挂载到本机目录下,即可以使用所有操作命令。
在desktop(client)
[[email protected] ~]# mount //172.25.254.216/student /mnt/ -o username=student,password=westos ##临时挂载
[[email protected] ~]# df
[[email protected] mnt]# touch file{1..10}
在server(server)
<3>永久挂载
在desktop(client)
方法一:##此方法有缺点是如果配置文件内容修改错误会影响开机启动
[[email protected] mnt]# cd
[[email protected] ~]# umount /mnt/
[[email protected] ~]# vim /etc/fstab
[[email protected] ~]# mount -a
[[email protected] ~]# df
方法二:
[[email protected] ~]# vim /etc/rc.d/rc.local
mount -o username=student,password=westos //172.25.254.216/student /mnt ##写入此命令
[[email protected] ~]# chmod +x /etc/rc.d/rc.local
[[email protected] ~]# reboot
[[email protected] ~]# df
<4>修改工作组名称
在server(server)
[[email protected] student]# vim /etc/samba/smb.conf##编辑配置文件
[[email protected] student]# systemctl restart smb.service
在desktop(client)
[[email protected] ~]# smbclient -L //172.25.254.216
<5>访问权限设定
黑名单:
在server(server)
[[email protected] student]# vim /etc/samba/smb.conf##编辑配置文件
hosts deny = 172.25.254.116##不允许访问id
[[email protected] student]# systemctl restart smb.service
在desktop(client)测试
白名单:
在server(server)
[[email protected] student]# vim /etc/samba/smb.conf##编辑配置文件
hosts allow = 172.25.254.216
<6>自定义文件共享目录
第一种情况:这个目录是用户自己建立的
在server(server)
[[email protected] ~]# mkdir /westos
[[email protected] ~]# semanage fcontext -a -t samba_share_t '/westos(/.*)?'##更改安全上下文
[[email protected] ~]# semanage fcontext -l | grep /westos
[[email protected] ~]# restorecon -FvvR /westos/ ##刷新
[[email protected] ~]# vim /etc/samba/smb.conf
[[email protected] ~]# systemctl restart smb.service
[[email protected] ~]# cd /westos/
[[email protected] westos]# touch file
在desktop(client)测试
[[email protected] ~]# smbclient -L //172.25.254.216
第二种情况:当目录是系统目录时例如/mnt
在server(server)
[[email protected] westos]# vim /etc/samba/smb.conf
[[email protected] westos]# systemctl restart smb.service
[[email protected] westos]# cd
[[email protected] ~]# touch /mnt/file{1..5}
[[email protected] ~]# ls /mnt/
file1 file2 file3 file4 file5
[[email protected] ~]# setsebool -P samba_export_all_ro on ##打开只读共享权限
在desktop(client)测试
[[email protected] ~]# smbclient //172.25.254.216/mnt -U student
四.用户权限的设定
<1>可浏览权限
在server(server)服务端
[[email protected] ~]# vim /etc/samba/smb.conf
browseable= no ##隐藏目录信息
在desktop(client)客户端
[[email protected] ~]# smbclient -L //172.25.254.216
<2>可写权限
在server
[[email protected] ~]# vim /etc/samba/smb.conf
writable = yes ##所有用户均可写
[[email protected] ~]# systemctl restart smb.service
[[email protected] ~]# chmod 777 /westos/
[[email protected] ~]# ll -d /westos/
drwxrwxrwx. 2 root root 17 Jun 2 01:52 /westos/
在desktop测试
[[email protected] ~]# mount //172.25.254.216/DIR /mnt -o username=westos,password=westos
[[email protected] ~]# touch /mnt/file
[[email protected] ~]# ll /mnt
<3>只对某个用户可写
在server
[[email protected] ~]# vim /etc/samba/smb.conf
write list = student ##可写用户
[[email protected] ~]# systemctl restart smb.service
在desktop
[[email protected] ~]# mount //172.25.254.216/DIR /mnt -o username=westos,password=westos
[[email protected] ~]# touch /mnt/file1
touch: cannot touch ‘/mnt/file1’: Permission denied##westos用户无法建立文件
[[email protected] ~]# umount /mnt/
[[email protected] ~]# mount //172.25.254.216/DIR /mnt -o username=student,password=westos
[[email protected] ~]# touch /mnt/file1##student用户可以建立文件
<4>组成员可写
在server
[[email protected] ~]# vim /etc/samba/smb.conf
write list = @student##可写用户组
[[email protected] ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos)
[[email protected] ~]# usermod -G student westos##将westos加入student
[[email protected] ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos),1000(student)
在desktop
[[email protected] ~]# umount /mnt/
[[email protected] ~]# mount //172.25.254.216/DIR /mnt -o username=westos,password=westos##因为westos用户属于student组的用户
[[email protected] ~]# touch /mnt/file2
[[email protected] ~]# ls /mnt/
file file1 file2
<5>目录的超级用户
在server
[[email protected] ~]# vim /etc/samba/smb.conf
admin users= westos##共享的超级用户指定
[[email protected] ~]# systemctl restart smb.service
在desktop
[[email protected] ~]# cd /mnt
[[email protected] mnt]# ll
total 0
-rw-r--r-- 1 1001 1001 0 Jun 2 02:50 file
-rw-r--r-- 1 student student 0 Jun 2 02:55 file1
[[email protected] mnt]# touch file5
[[email protected] mnt]# ll
total 0
-rw-r--r-- 1 1001 1001 0 Jun 2 02:50 file
-rw-r--r-- 1 student student 0 Jun 2 02:55 file1
-rw-r--r-- 1 root 1001 0 Jun 2 03:06 file5
五.多用户挂载
在客户端desktop
[[email protected] ~]# umount /mnt/
[[email protected] ~]# yum install cifs-utils -y
[[email protected] ~]# vim /root/smbpass##创建访问时所需密码
username=westos
passwd=redhat
[[email protected] ~]# mount -o credentials=/root smbpass,sec=ntlmssp,multiuser //172.25.254.216/DIR /mnt##多用户挂载
[[email protected] ~]# cd /mnt/
[[email protected] mnt]# useradd test
[[email protected] mnt]# su - test
[[email protected] ~]$ cifscreds add -u westos 172.25.254.216
Password: ##若密码输入错误
[[email protected] ~]$ ls
[[email protected] ~]$ ls /mnt
ls: cannot access /mnt: Permission denied##没有做smb的认证所以无法访问smb共享
[[email protected] ~]$ cifscreds add -u westos 172.25.254.216
You already have stashed credentials for 172.25.254.216 (172.25.254.216)
If you want to update them use:
cifscreds update
[[email protected] ~]$ cifscreds clearall##清除之前的缓存
[[email protected] ~]$ cifscreds add -u westos 172.25.254.216##认证后可以访问
Password:
[[email protected] ~]$ ls /mnt/
file file1 file4
[[email protected] ~]$ touch /mnt/file5
[[email protected] ~]$ ll /mnt/
total 0
-rw-r--r-- 1 test test 0 Jun 2 02:50 file
-rw-r--r-- 1 student student 0 Jun 2 02:55 file1
-rw-r--r-- 1 root test 0 Jun 2 03:06 file4
-rw-r--r-- 1 root test 0 Jun 2 03:35 file5
六.匿名用户访问
在server服务端
[[email protected] ~]# vim /etc/samba/smb.conf
security = user
passdb backend = tdbsam
map to guest = bad user##客户匿名访问
guest ok = yes##匿名用户访问
[[email protected] ~]# systemctl restart smb.service
在desktop