一、samba的作用
samba服务:实现的是linux和windows不同平台的资源共享.
samba 是一款软件,主要功能是提供cifs服务.
二、samba的安装与启用
server 为服务端;client为客户端。
【server】:
[root@server ~]# yum install samba samba-client samba-common -y ##下载服务
[root@server ~]# systemctl start smb ##开启(森巴)服务
[root@server ~]# systemctl enable smb ##开机自动开启
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
[root@server ~]# systemctl stop firewalld ##关闭火墙
[root@server ~]# systemctl disable firewalld ##开机自动关闭火墙
[root@server ~]# netstat -antlupe | grep smb ##查看smb的端口
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 0 65128 3230/smbd
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 0 65129 3230/smbd
tcp6 0 0 :::445 :::* LISTEN 0 65126 3230/smbd
tcp6 0 0 :::139 :::* LISTEN 0 65127 3230/smbd
[root@client ~]# yum install samba-client -y ##下载smb客户端
[root@client ~]# smbclient -L //172.25.254.229 ##匿名登陆
Enter root's password: ##没有密码,按回车
Anonymous login successful
IPC$ IPC IPC Service (Samba Server Version 4.1.1) ##这里没有信息
Anonymous login successful
三、samba服务用户的建立及访问
[root@server ~]# id westos ##查看用户
id: westos: no such user
[root@server ~]# useradd westos ## 新建用户,因为登陆必须得是本机已有用户
[root@server ~]# smbpasswd -a student ##将用户添加到smb
New SMB password: ##给密码123
Retype new SMB password:
Added user student. ##添加成功
[root@server ~]# smbpasswd -a westos
New SMB password:
Retype new SMB password:
Added user westos.
[root@server ~]# pdbedit -L ##列出smb添加所有用户
student:1000:Student User
westos:1001:
[root@server ~]# pdbedit -x student ##删除student用户
[root@server ~]# pdbedit -L
westos:1001:
[root@server ~]# smbpasswd -a student
New SMB password:
Retype new SMB password:
Added user student.
[root@server ~]# pdbedit -L
student:1000:Student User
westos:1001:
[root@client ~]# smbclient -L //172.25.254.229 -U student ##用本机(server)已有用户登陆
Enter student's password: ##与student用户登陆密码无关,输入刚才在server里新设的密码
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
student Disk Home Directories ##出现了student信息
[root@client ~]# smbclient //172.25.254.229/student -U student ##登陆进去访问
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \* ##访问被拒绝,则说明SELINUX关闭了用户对家目录的访问权限
[root@server ~]# getsebool -a | grep samba
samba_enable_home_dirs --> off
[root@server ~]# setsebool -P samba_enable_home_dirs on ##开启家目录的访问权限
四、上传文件以及永久挂载
方法一、
[root@client ~]# cd /etc/ ##必须在一个环境内
[root@client etc]# smbclient //172.25.254.229/student -U student
Enter student's password:
smb: \> !ls ##显示出在这个环境内的所有可以下载的文件
smb: \> put passwd ##下载passwd
putting file passwd as \passwd (22.5 kb/s) (average 22.5 kb/s)
smb: \> touch file ##不能touch文件,因为没有touch命令
touch: command not found
smb: \> put /bin/ls ##不能下载除/etc/以外其他目录里的东西
NT_STATUS_OBJECT_PATH_NOT_FOUND opening remote file \/bin/ls
smb: \> quit
[root@server ~]# cd /home/student ##环境
[root@server student]# ls ##就可以看到刚才下载的文件
passwd
【这种方法只能在一个环境内下载这个环境的文件,所以有局限性。】
方法二、(有两种,支持用第二种)
第一种:
[root@client ~]# mount -o //172.25.254.229/student /mnt/ username=student,password ##手动挂载
[root@client ~]# vim /etc/fstab ##永久挂载文件,但不支持写,因为会影响客户端的电脑启动
写入://172.25.254.229/student /mnt cifs defaults,username=student,password=123 0 0
[root@client ~]# mount -a ##重新加载
[root@client ~]# reboot ##重启
[root@client ~]# df ##重启后,查看是否挂载成功
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182156 7291744 31% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 80 484852 1% /dev/shm
tmpfs 484932 12768 472164 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
//172.25.254.229/student 10473900 3157296 7316604 31% /mnt ##成功了
第二种:
[root@client ~]# vim /etc/rc.d/rc.local ##脚本,开机不会被影响,之在系统服务最后读取(永久挂载)
写入:
mount //172.25.254.229/student /mnt -o username=student,password=123
[root@client ~]# chmod +x /etc/rc.d/rc.local ##给可执行权限
[root@client ~]# reboot ##重启
[root@client ~]# df ##重启后,查看是否挂载成功
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3182156 7291744 31% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 80 484852 1% /dev/shm
tmpfs 484932 12768 472164 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/vg0-vo 483670 2339 451840 1% /home
//172.25.254.229/student 10473900 3157296 7316604 31% /mnt ##成功了
[root@client ~]# cd /mnt/
[root@client mnt]# touch file{1..5} ##随意下载建立,删掉也可以
[root@client mnt]# ls
file1 file2 file3 file4 file5 passwd
[root@server student]# ls ##就出现了
file1 file2 file3 file4 file5 passwd
五、server 服务端 的配置
【服务端server】
[root@server ~]# rpm -qc samba-common ##查看samba的配置文件
/etc/logrotate.d/samba
/etc/samba/lmhosts
/etc/samba/smb.conf
/etc/sysconfig/samba
[root@server ~]# vim /etc/samba/smd.conf ##samba的主配置文件
写入:
89 workgroup = WESTOS #修改组名称
[root@server ~]# systemctl restart smb.service ##重启服务
【客户端desktop】
[root@client ~]# smbclient -L //172.25.254.229
Domain=[WESTOS] ##改成功
【server】
[root@server ~]# vim /etc/samba/smd.conf
写入:
98 hosts allow = 172.25.254.129 ##白名单
[root@server ~]# systemctl restart smb.service
【desktop】
[root@client ~]# smbclient -L //172.25.254.229 ##允许访问
Enter root's password:
Anonymous login successful
【server】
[root@server ~]# vim /etc/samba/smd.conf
写入:
98 hosts deny = 172.25.254.129 ##黑名单
[root@server ~]# systemctl restart smb.service
【desktop】
[root@client ~]# smbclient -L //172.25.254.229 ##不允许访问
Enter root's password:
protocol negotiation failed: NT_STATUS_INVALID_NETWORK_RESPONSE
六、自定义共享目录
上面我们可以看到在服务端是一个家目录共享,如何自定义共享目录
第一种情况:当这个目录是用户自己建立时
【server】
[root@server ~]# mkdir /westos
[root@server ~]# semanage fcontext -a -t samba_share_t '/westos(/.*)?' ##改变此目录的安区上下文列表
[root@server ~]# restorecon -RvvF /westos ##改变完成后记得刷新查看
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
[root@server ~]# vim /etc/samba/smb.conf ##编写主配置文件(名字、说明、绝对路径)
写入:
321 [DIR]
322 comment = westos dir
323 path = /westos
[root@server ~]# systemctl restart smb.service
【desktop】
[root@client ~]# smbclient //172.25.254.229/DIR -U student
Enter student's password:
##可以看到内容,说明共享成功
第二种情况:当共享目录是系统目录时例如是/mnt
(两种方法,第二中的权限大,但安全性差。相反,第一种的权限小,安全性高。支持用第二种)
方法一、
【server】
[root@server ~]# vim /etc/samba/smb.conf ##在主配置文件里编辑好相关配置信息:名字、说明、绝对路径
写入:
325 [mnt]
326 comment = /mnt dir
327 path = /mnt
[root@server ~]# systemctl restart smb.service
[root@server ~]# touch /mnt/file{1..5}
[root@server ~]# ls /mnt/ ##建好之后去客户端看不到新建
file1 file2 file3 file4 file5
[root@server ~]# setenforce 0 ##SELINUX 为警告模式就可以去访问查看了
【desktop】
[root@client mnt]# smbclient //172.25.254.229/mnt -U student ##就可以看到刚才所建文件
file1 N 0 Sat Jun 2 01:54:12 2018
file2 N 0 Sat Jun 2 01:54:12 2018
file3 N 0 Sat Jun 2 01:54:12 2018
file4 N 0 Sat Jun 2 01:54:12 2018
[root@server ~]# setenforce 1 ##还原不要影响下一步操作
方法二、
【server】
[root@server ~]# setsebool -P samba_export_all_ro on ##打开这个权限
【desktop】
[root@client mnt]# smbclient //172.25.254.229/mnt -U student ##可以进行访问,并且自己建立的目录不改变安全上下文也可以进行访问(因为这个开放的权限大)
file1 N 0 Sat Jun 2 01:54:12 2018
file2 N 0 Sat Jun 2 01:54:12 2018
file3 N 0 Sat Jun 2 01:54:12 2018
file4 N 0 Sat Jun 2 01:54:12 2018
file5 N 0 Sat Jun 2 01:54:12 2018
七、权限控制
在自定义共享目录下做此次试验
[root@server ~]# vim /etc/samba/smb.conf
写入:
324 browseable = no ##不允许浏览DIR
[root@client ~]# smbclient -L //172.25.254.229/DIR -U student
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
mnt Disk /mnt dir ##看不到DIR
student Disk Home Directories
[root@server ~]# vim /etc/samba/smb.conf
写入:
324 browseable = yes ##允许浏览DIR
[root@client ~]# smbclient -L //172.25.254.229/DIR -U student
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
mnt Disk /mnt dir
DIR Disk westos dir ##可以看到了
student Disk Home Directories
1.所有用户权力
【server】
[root@server ~]# vim /etc/samba/smb.conf
写入:
325 writable = yes ##开启写权力(所有用户都可以读写)
[root@server ~]# systemctl restart smb.service
【desktop】
[root@client ~]# umount /mnt/ ##先卸载掉
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=student,password=123 ##再重新挂载
[root@client ~]# ll /mnt/
total 0
[root@client ~]# touch /mnt/file1
touch: cannot touch ‘/mnt/file1’: Permission denied ##权限不允许
[root@server mnt]# chmod 777 /westos/
[root@server mnt]# vim /etc/samba/smb.conf
[root@server mnt]# systemctl restart smb.service
2.指定用户权力
【server】
[root@server ~]# vim /etc/samba/smb.conf
写入:
326 write list = student ####写权力对student用户开放
[root@server mnt]# systemctl restart smb.service
[root@client ~]# umount /mnt/
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=student,password=123
[root@client ~]# touch /mnt/file2
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=westos,password=123
[root@client ~]# touch /mnt/file3
touch: cannot touch ‘/mnt/file3’: Permission denied
[root@client ~]#
3.指定用户组权力
[root@server ~]# vim /etc/samba/smb.conf
326 write list = @student ##写权力对syudent用户组开放
[root@server ~]# systemctl restart smb.service
[root@server ~]# usermod -G student westos
[root@server ~]# id westos
uid=1001(westos) gid=1001(westos) groups=1001(westos),1000(student)
[root@client ~]# umount /mnt/
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=westos,password=123
[root@client ~]# touch /mnt/file4
[root@client ~]# ll /mnt/
-rw-r--r-- 1 1001 1001 0 Jun 2 03:07 file4 ##因为129没有westos用户
4.设定用户为超级用户身份
设定student用户为当前共享的root(因为将文件改为755只有超级用户可以建立文件而且不用重启服务即可生效)
[root@server ~]# chmod 755 /westos/ ##除了root谁也不能对/westos读写
[root@server ~]# vim /etc/samba/smb.conf
写入:
325 writable = yes
326 #write list = @student
327 admin users = westos ##给westos是root的身份
[root@server ~]# systemctl restart smb.service
[root@client ~]# umount /mnt/
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=westos,password=123
[root@client ~]# touch /mnt/file6
[root@client ~]# ll /mnt/
-rw-r--r-- 1 root 1001 0 Jun 2 03:28 file6 ##属于root组
5.smb的匿名访问
[root@client ~]# smbclient //172.25.254.229/DIR
Enter root's password:
Anonymous login successful
Domain=[WESTOS] OS=[Unix] Server=[Samba 4.1.1]
tree connect failed: NT_STATUS_ACCESS_DENIED ##被拒绝。不可以
[root@server ~]# vim /etc/samba/smb.conf
写入:
328 guest ok = yes
[root@server ~]# systemctl restart smb.service
[root@client ~]# smbclient //172.25.254.229/DIR
Enter root's password:
Anonymous login successful
6.匿名用户挂载
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=guest,password=""
mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs) ##不能挂载
[root@server ~]# vim /etc/samba/smb.conf
写入:
125 map to guest = bad user ##把匿名用户映射到guest上
[root@server ~]# systemctl restart smb.service
[root@client ~]# mount //172.25.254.229/DIR /mnt -o username=guest,password="" ##可以挂载了
[root@client ~]#
八、samba的多用户挂载
[root@client ~]# useradd test
[root@client ~]# su - test
[test@client ~]$ cd /mnt/
[test@client mnt]$ ls
file1 file2 file3 file4 file5 file6 ##不安全
[root@client ~]# yum install cifs-utils -y
[root@client ~]# vim /root/smbpass
写入:
1 username=student
2 password=123
[root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.229/DIR /mnt
##credentials=/root/smbpass 的意思就是,挂载需要的用户和密码。 sec=ntlmssp :用户认证方式
[root@client ~]# cd /mnt/
[root@client mnt]# ls
file1 file2 file3 file4 file5 file6
[test@client ~]$ cifscreds add -u westos 172.25.254.229
Password:
[test@client ~]$ ls /mnt
ls: cannot access /mnt: Permission denied
[test@client ~]$ cifscreds add -u westos 172.25.254.229 ##用westos身份登陆访问(必须保证westos时samba用户)
You already have stashed credentials for 172.25.254.229 (172.25.254.229)
If you want to update them use:
cifscreds update
[test@client ~]$ cifscreds clearall
[test@client ~]$ cifscreds add -u westos 172.25.254.229
Password:
[test@client ~]$ ls /mnt ##建立文件并且文件是属于westos的
file1 file2 file3 file4 file5 file6
[test@client ~]$