linux----smaba共享和samba的基本配置
SMB文件共享
用internet文件系统(CIFS)也成为服务器信是适用于MicrosoftWindows服务器和客户端的标准文件和打印共享系统息块
(SMB)
samba服务可用于将Linux文件系统作为CIFS/SMB网络文件共享进行共享,并将Linux打印机作为CIFS/SMB网络文件共享进行共享
##
[[email protected] ~] 服务端
##
[[email protected] ~] 客户端
一.服务的安装
服务端:
[[email protected] ~]# yum install samba samba-client samba-common -y ##安装三个服务软件
[[email protected] ~]# systemctl start smb ##打开服务
[[email protected] ~]# systemctl enable smb ##开机自启
[[email protected] ~]# systemctl stop firewalld.service ##关闭防火墙
[[email protected] ~]# systemctl disable firewalld.service
客户端:
[[email protected] ~]# yum install samba-client.x86_64 -y (这是一个客户端)
二.smb用户的添加和删除
[[email protected] ~]# smbpasswd -a student (添加一个新用户(这个用户必须存在),student密码与登陆密码无关)
New SMB password:
Retype new SMB password:
Added user student.
[[email protected] ~]# pdbedit -L (查看用户信息)
[[email protected] ~]# pdbedit -x student (删除用户)
[[email protected] ~]# pdbedit -L
[[email protected] ~]# smbpasswd -a student
New SMB password:
Retype new SMB password:
Added user student.
[[email protected] ~]# pdbedit -L
student:1000:Student User
客户端测试:
[[email protected] ~]# smbclient -L //172.25.30.235 -U student (登陆)
三.访问共享
[[email protected] ~]# smbclient //172.25.30.235/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \* (文件拒绝访问,原因来源与selinux)
查看错误原因来源于selinux安全上下文
服务端:
[[email protected] ~]# getsebool -a | grep samba (查看smb服务的安全上下文)
[[email protected] ~]# setsebool -P samba_enable_home_dirs on (永久开启)
客户端再次测试:
[email protected] ~]# smbclient //172.25.30.235/student -U student (访问)
四.客户端文件的上传
## 只能上传你所在目录里的文件
客户端:
[email protected] ~]# cd /mnt
[[email protected] mnt]# ls
[[email protected] mnt]# touch file
[[email protected] mnt]# smbclient //172.25.30.235/student -U student
smb: \> !ls (查看本地文件)
file
smb: \> put file
putting file file as \file (0.0 kb/s) (average 0.0 kb/s) (上传成功)
客户端上传的文件所在目录在服务端的/home/student目录下
[email protected] ~]# cd /home/student
[[email protected] student]# ls
五.挂载
客户端:
[email protected] mnt]# mount //172.25.30.235/student /mnt/ -o username=student,password=qin (挂载)
[[email protected] mnt]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3150840 7323060 31% /
devtmpfs 927060 0 927060 0% /dev
tmpfs 942648 140 942508 1% /dev/shm
tmpfs 942648 17048 925600 2% /run
tmpfs 942648 0 942648 0% /sys/fs/cgroup
//172.25.30.235/student 10473900 3156736 7317164 31% /mnt
[[email protected] mnt]# cd /mnt
[[email protected] mnt]# ls
file
[[email protected] mnt]# touch westos{1..10}
[[email protected] mnt]# ls
file westos1 westos10 westos2 westos3 westos4 westos5 westos6 westos7 westos8 westos9
[[email protected] mnt]# rm -fr westos*
服务端:
测试发现共享成立
六.samba共享的自动挂载
1.自动挂载配置文件 (当客户端smb未开启,或者服务端smb未开启时自动挂载会失败)
vim /etc/fstab
2.脚本自动挂载 (如果smb文件未开启,则不会自动挂载,如果开启自动挂载) 相较于1来说较好
[[email protected] ~]# vim /etc/rc.d/rc.local
[[email protected] ~]# chmod +x /etc/rc.d/rc.local (加权之后重启系统测试成功)
七.samaba的基本配置
1.名字同步:
服务端中:
[[email protected] student]# systemctl start smb (打开smb)
[[email protected] student]# vim /etc/samba/smb.conf (打开配置文件,修改名字)
[[email protected] student]# systemctl restart smb.service (重启服务)
客户端:
[[email protected] ~]# smbclient -L //172.25.30.235 (查看名字是否改了)
2.共享目录
2.1第一种方法:
服务端:
[[email protected] student]# mkdir /westos
[[email protected] student]# semanage fcontext -a -t samba_share_t '/westos(/*)?' (添加westos目录到安全上下文中)
[[email protected] ~]# restorecon -RvvF /westos/ (刷新)
[[email protected] student]# vim /etc/samba/smb.conf (添加配置文件)
[[email protected] student]# systemctl restart smb.service
[[email protected] student]# chmod 777 /westos
客户端:
[[email protected] ~]# smbclient //172.25.30.235/DIR -U student (名字登陆)
2.2第二种方法:
服务端:
[[email protected] student]# vim /etc/samba/smb.conf (添加配置文件)
write list = @或者+westos 表示对westos组可写 westos可写列表
[[email protected] ~]# useradd westos
[[email protected] ~]# smbpasswd -a westos
客户端:
[[email protected] ~]# smbclient //172.25.30.235/DIR -U westos (组登陆)
这时候student不属于westos组
要想student用户可以共享目录,需要将student用户加入到westos组中
服务端:
usermod -G westos student
客户端再次测试student用户登陆测试:
[[email protected] ~]# smbclient //172.25.30.235/DIR -U student
3.共享隐藏
服务端:
browseable = yes or no 表示是否可以使用smbckient -L 去浏览文件
客户端:
[email protected] ~]# smbclient -L //172.25.30.235 (可以看见DIR 共享)
4.匿名访问:
在默认情况下客户端是不能匿名访问文件的
服务端:
vim /etc/samba/smb。conf
map to guest = bad user (bad user所有用户)
guest ok = yes (匿名用户可以浏览)
systemctl restart smb (刷新)
客户端:
[email protected] ~]# smbclient //172.25.30.235/DIR (查看可以匿名访问)