文章目录
1、samba服务简介及基本信息
作用
windows系统共享文件时用到的协议smbsmb是由miscrosoft+sun
Linux cifs
samba基本信息
- 服务启动脚本:smb.service
- 主配置目录:/etc/samba
- 主配置文件:/etc/samba.conf
- 安全上下文:samba_share_t
- 端口:139 445
- 安装包:samba samba-common
2、samba的安装与启用
linux读取windows共享文件
[root@localhost mnt]# dnf install samba samba-common samba-client -y
[root@localhost mnt]# systemctl enable --now smb
[root@localhost mnt]# firewall-cmd --permanent --add-service=samba
[root@localhost mnt]# firewall-cmd --reload
[root@localhost mnt]# cd /etc/samba/
[root@localhost samba]# cp smb.conf.example smb.conf
[root@localhost samba]# mkdir /westos
[root@localhost samba]# semanage fcontext -a -t samba_share_t '/westos(/.*)?'
[root@localhost samba]# restorecon -RvvF /westos/
Relabeled /westos from unconfined_u:object_r:default_t:s0 to system_u:object_r:public_content_t:s0
[root@localhost samba]# vim /etc/samba/smb.conf
[LINUX] ##共享名称
comment = westos dir ##共享说明
path = /westos ##共享路径
[root@localhost samba]# systemctl restart smb.service
[root@localhost samba]# systemctl enable smb.service
3、 samba用户的建立及加目录访问
[root@server1 samba]# smbpasswd -a linux ##添加用户
New SMB password:
Retype new SMB password:
Added user linux.
[root@server1 samba]# smbpasswd -a westos
New SMB password:
Retype new SMB password:
Added user westos.
[root@server1 samba]# id westos
uid=1000(westos) gid=1000(westos) groups=1000(westos)
[root@server1 samba]# id linux
uid=1001(linux) gid=1001(linux) groups=1001(linux)
[root@server1 samba]# pdbedit -L ##查看用户列表
linux:1001:
westos:1000:westos
[root@server1 samba]# pdbedit -x linux ##删除用户
[root@server1 samba]# pdbedit -L
westos:1000:westos
[root@server1 samba]# smbpasswd -a linux
New SMB password:
Retype new SMB password:
Added user linux.
4、samba服务共享目录
[root@server1 samba]# smbclient -L //192.168.0.1
Enter MYGROUP\root's password:
Anonymous login successful
[root@server1 samba]# smbclient //192.168.0.1/LINUX ##匿名用户登陆成功但是访问被拒绝,不支持匿名用户访问
Enter MYGROUP\root's password:
Anonymous login successful
tree connect failed: NT_STATUS_ACCESS_DENIED
[root@server1 samba]# smbclient //192.168.0.1/LINUX -U linux
Enter MYGROUP\linux's password:
Try "help" to get a list of possible commands.
smb: \> ls
[root@server1 samba]# mount -o username=linux,password=123 //192.168.0.1/LINUX /mnt
[root@server1 samba]# df
//192.168.0.1/LINUX 17811456 2127232 15684224 12% /mnt
[root@server1 samba]# cd /mnt/
[root@server1 mnt]# ls
5、在客户端实现自动挂卸载
重新开启一个虚拟机做客户端
[root@server2 yum.repos.d]# dnf install samba samba-common samba-client -y
[root@server2 yum.repos.d]# systemctl enable --now smb
[root@node1 yum.repos.d]# firewall-cmd --permanent --add-service=samba
[root@server2 yum.repos.d]# dnf install autofs -y
[root@server2 etc]# systemctl enable --now autofs
[root@server2 etc]# vim /etc/auto.master
/mnt /etc/auto.samba
[root@server2 etc]# vim /etc/auto.samba
samba -fstype=cifs,username=linux,password=123 ://192.168.0.1/LINUX
[root@server2 etc]# systemctl restart autofs
[root@server2 etc]# cd /mnt/
[root@server2 mnt]# cd samba
[root@server2 samba]# df
//192.168.0.1/LINUX 7353344 4992264 2361080 68% /mnt/samba
[root@server2 mnt]# vim /etc/autofs.conf
timeout = 3
[root@server2 mnt]# systemctl restart autofs
[root@server2 samba]# cd .. ##取消挂载
[root@server2t mnt]# df
6、共享系统目录
共享系统目录时不能更改安全上下文,需要更改布尔值。
[root@13 ~]# vim /etc/samba/smb.conf
[LEE]
comment = westos1 dir
path = /mnt
[root@13 ~]# getsebool -a | grep samba
[root@13 ~]# setsebool -P samba_export_all_ro on
[root@13 ~]# systemctl restart smb
[root@13 ~]# smbclient //192.168.0.1/LINUX -U linux
7、samba的常用配置参数
writable = yes ##可写
write list = westos ##指定用户可写
write list = +westos ##指定组可写
write list = @westos
valid users = lee ##指定访问用户
valid users = +lee|@lee ##指定访问组
browseable = yes|no ##是否隐藏共享
map to guest = bad user ##写到全局设定中
guest ok = yes ##允许匿名用户访问
admin users = lee ##指定此共享的超级用户身份
[root@localhost mnt]# chmod 777 /westos/
[root@localhost mnt]# cd /etc/samba/
[root@localhost samba]# vim smb.conf
[root@localhost samba]# systemctl restart smb.service
[root@localhost samba]# mount //172.25.254.212/LEE /mnt/ -o username=linux,password=westos
[root@localhost samba]# df
[root@localhost mnt]# touch file4
root@localhost samba]# usermod -G linux lee
[root@localhost samba]# id lee
uid=1004(lee) gid=1004(lee) groups=1004(lee),1001(linux)
[root@localhost ~]# smbclient -L //172.25.254.212/mnt
8、samba的多用户挂载
在客户端如果用普通的挂载方式,没有用过用户验证的人也可以访问samba服务。
[root@localhost ~]# dnf search cifs
[root@localhost ~]# dnf install cifs-utils.x86_64 -y
[root@localhost samba]# pdbedit -L
linux:1001:
[root@localhost samba]# smbpasswd -a lee
[root@localhost samba]# smbpasswd -a westos
[root@localhost ~]# vim /root/smbauth
username=linux
password=westos
[root@localhost ~]# chmod 600 /root/smbauth
[root@localhost ~]# ls -ld /root/smbauth
[root@localhost ~]# mount -o credentials=/root/smbauth //172.25.254.212/LEE /samba/
[root@localhost samba]# su - westos
[westos@localhost ~]$ cd /samba
[westos@localhost samba]$ ls
ls: cannot open directory '.': Permission denied ##客户端主机的wetos用户没有通过认证
[westos@localhost samba]$ cifscreds add -u linux 172.25.254.212
[westos@localhost samba]$ ls ##通过认证可以显示
file4 smb.conf
[westos@localhost samba]$ touch file1
[westos@localhost samba]$ cifscreds add -u linux 172.25.254.212
Key search failed: Key has expired ##当遇到此报错信息
[westos@localhost samba]$ cifscreds add -u linux -d 172.25.254.212
Password:
[westos@localhost samba]$ cifscreds clearall ##执行以上两条命令解决报错