cifs网络文件共享系统是linux系统与windos系统之间的文件共享(用于linux和windos之间的文件传输系统)
企业中常用的web服务,用来提供http://(超文本传输协议)。
web系统是客户端/服务器模式的,所以应该有服务器和客户端里两个部分。常用的服务器程序时Apache,常用的客户端程序是浏览器。www服从http协议,默认的TCP/IP端口是80,客户端和服务器的通信过程简述如下:
[root@localhost ~]# yum install samba-common samba samba-client.x86_64 -y
[root@localhost ~]# systemctl start smb
[root@localhost ~]# systemctl enable smb
[root@localhost ~]# firewall-cmd --permanent --add-service=samba
[root@localhost ~]# firewall-cmd --permanent --add-service=samba-client
[root@localhost ~]# firewall-cmd --reload
[root@localhost ~]# smbclient -L //172.25.254.110 //匿名用户登陆
Enter root's password: //匿名用户登陆没有密码,可直接回车
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@localhost ~]# smbpasswd -a student //为smb添加用户student(该用户必须是真实存在的,能id student 出来的用户)
New SMB password: //设置用户登陆smb服务的密码
Retype new SMB password:
Added user student.
[root@localhost ~]#
[root@localhost ~]# pdbedit -L //列出可以登陆samba服务的用户
student:1000:Student User
[root@localhost ~]# cd /home/student/
[root@localhost student]# touch file{1..10}
[root@localhost student]# ls
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
[root@localhost ~]# getsebool -a | grep samba //查看smb的selinux状态
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off
[root@localhost ~]# setsebool -P samba_enable_home_dirs on //打开家目录功能
[root@localhost student]# smbclient -L //172.25.254.110 -U student //登陆本地用户,系统拒绝查看家目录。
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
student Disk Home Directories
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@localhost ~]# smbclient //172.25.254.110/student -U student //登陆查看家命令内容
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Mon Nov 12 21:42:58 2018
.. D 0 Thu Jul 10 18:19:09 2014
.bash_logout H 18 Wed Jan 29 07:45:18 2014
.bash_profile H 193 Wed Jan 29 07:45:18 2014
.bashrc H 231 Wed Jan 29 07:45:18 2014
.ssh DH 0 Thu Jul 10 18:19:10 2014
.config DH 0 Thu Jul 10 19:06:53 2014
file1 N 0 Mon Nov 12 21:42:58 2018
file2 N 0 Mon Nov 12 21:42:58 2018
file3 N 0 Mon Nov 12 21:42:58 2018
file4 N 0 Mon Nov 12 21:42:58 2018
file5 N 0 Mon Nov 12 21:42:58 2018
file6 N 0 Mon Nov 12 21:42:58 2018
file7 N 0 Mon Nov 12 21:42:58 2018
file8 N 0 Mon Nov 12 21:42:58 2018
file9 N 0 Mon Nov 12 21:42:58 2018
pdbedit -L 查看用户列表
smbpasswd -a westos //添加用户
pdbedit -x westos //删除用户
[root@localhost ~]# pdbedit -L
student:1000:Student User
[root@localhost ~]# useradd westos
[root@localhost ~]# smbpasswd -a westos
New SMB password:
Retype new SMB password:
Added user westos.
[root@localhost ~]# pdbedit -L
student:1000:Student User
westos:1001:
[root@localhost ~]# pdbedit -x westos
[root@localhost ~]# pdbedit -L
student:1000:Student User
1.组名的修改
在配置文件中修改
vim /etc/samba/smb.conf
将原来的
89 workgroup = MYGROUP
修改为:
89 workgroup = westos
[root@localhost ~]# systemctl restart smb.service //重启服务
[root@localhost ~]# smbclient -L //172.25.254.110 -U student
Enter student's password:
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
student Disk Home Directories
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@localhost ~]#
2.samba黑白名单的建立
[root@localhost ~]# rpm -qc samba-common //查找配置文件
[root@localhost ~]# vim /etc/samba/smb.conf
89 workgroup = WESTOS
98 hosts deny = 172.25.254.10 //添加这一行,禁止ip是172.25.245.10这台主机进行访问;这个时候可以用两台主机(客户端)去测试;
#(黑白名单任意添加一个,每次只能添加一个) hosts allow = 172.25.254.10 //添加这一行,只允许ip为172.25.254.10这台主机机进行访问登陆,其他客户拒绝登陆;
[root@localhost ~]# systemctl restart smb.service //重启服务
1.临时挂载:
[root@localhost ~]# mount //172.25.254.110/student /mnt/ -o username=student,password=123
[root@localhost ~]# touch file{1..3} ##在客户端新建文件
[root@localhost ~]# ls ##同步到服务端
2.开机自动挂载
[root@localhost ~]# vim /etc/fstab
//172.25.254.205/student /mnt cifs defaults,username=student,password=123
[root@localhost ~]# mount -a ##加载文件/etc/fstab中的内容
[root@localhost ~]# df ##挂载成功
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182952 7290948 31% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 140 484792 1% /dev/shm
tmpfs 484932 12808 472124 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
//172.25.254.110/student 10473900 3158308 7315592 31% /mnt
上述方法有一个缺点是如果文件内容修改错误会影响开机启动,于是推荐下述挂载策略
[root@localhost ~]# vim /etc/rc.d/rc.local
mount -t cifs //172.25.254.110/student /mnt -o username=student,password=123
[root@localhost ~]# chmod +x /etc/rc.d/rc.local ##增加可执行权限
[root@localhost ~]# reboot
[root@server mnt]# vim /etc/samba/smb.conf
[hello]
comment=westos dir
path = /westos //共享目录地址的绝对路径
browseable = yes //是否允许浏览:browseable=no(不允许)不用重启,即改生效;
writeable = yes //是否可写: 需要重启;
write list = +student //组用户可写 ;+ 或者 @ 都可以
[root@server mnt]# systemctl restart smb.service //重启服务
[root@server mnt]# chmod 777 /westos //改变权限
[root@server mnt]# usermod -G student westos //让westos为student用户的组
//客户端测试:
[root@client ~]# mount //172.25.254.110/hello /mnt -o username=westos,password=westos //westos用户挂载,测试
[root@client ~]# cd /mnt
[root@client mnt]# touch file4 //成功建立file4文件
[root@client mnt]# ll
-rw-r--r-- 1 1001 1001 0 Jun 2 02:43 file4 //建立成功,这里1001是westos用户的id,因为客户端主机没有westos用户;
客户端设置:
[root@client ~]# yum install cifs-utils.x86_64 -y
[root@client ~]# vim /root/smbpass //编辑文件,文件,参数指定:指定用户名和密码
username=student
password=student
[root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.247/DIR /mnt
//挂载,其中,credentials=/root/smbpass文件指定的用户名、密码,sec=ntlmssp 认证方式(因为下载的samba是4.1的,所有认证方式是ntlmssp;查询方式rpm -ql | grep samba), multiuser为多用户挂载
[root@client ~]# useradd test //添加一个不存在的用户
[root@client ~]# su - test //切换到此用户环境下
[test@client ~]$ cd /mnt
[test@client mnt]$ ls
ls: reading directory .: Permission denied //切换到test用户,访问拒绝,保证了安全性
[test@client ~]$ cifscreds add -u westos 172.25.254.247 //命令通过samba用户认证,看到目录下的内容
You already have stashed credentials for 172.25.254.247 (172.25.254.247)
If you want to update them use:
cifscreds update
[test@client ~]$ cifscreds add -u westos 172.25.254.247
Password:
[test@client ~]$ ls /mnt
file file1 file3
[root@server ~]# vim /etc/samba/smb.conf
126 map to guest=bad user ##这个配置的意思是将所有samba系统主机所不能正确识别的用户都映射成guest用户,这样其他主机访问目录时就不再需要用户名和密码了
328 guest ok=yes ##匿名用户可以登录
[root@server ~]# systemctl restart smb.servic
测试:
[root@client ~]# smbclient //172.25.254.110/DIR
Enter root's password:
Anonymous login successful
Domain=[HELLO] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
. D 0 Mon Jun 4 06:34:13 2018
.. D 0 Mon Jun 4 04:28:21 2018
hello N 0 Mon Jun 4 05:42:48 2018
file1 N 0 Mon Jun 4 06:42:48 2018