赞
踩
CIFS(Common Internet FileSystem)
Samba,是种自由软件,用来让UNIX系列的操作系统与微软Windows操作系统的SMB/CIFS(Server Message Block/Common Internet File System)网络协定做连结。在目前的版本(v3),不仅可存取及分享SMB的资料夹及打印机,本身还可以整合入Windows Server的网域、扮演为网域控制站(Domain Controller)以及加入Active Directory成员。简而言之,此软件在Windows与UNIX系列OS之间搭起一座桥梁,让两者的资源可互通有无。
简而言之:samba服务器就是可以让linux和windows都可以使用的共享服务。
这些参数大多数写在配置文件的底部
安装三个软件
[root@smbserver ~]# yum install samba-common-tools.x86_64 samba.x86_64 samba-client.x86_64 -y
关闭火墙:
[root@smbserver ~]# systemctl stop firewalld.service
[root@smbserver ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
查看samba的配置文件:
[root@smbserver ~]# rpm -qc samba-common
/etc/logrotate.d/samba
/etc/samba/lmhosts
/etc/samba/smb.conf
/etc/sysconfig/samba
修改配置文件/etc/samba/smb.conf
[root@smbserver ~]# vim /etc/samba/smb.conf
[smbtest] # 共享名称
path = /smbdata # 实际共享目录
[root@smbserver ~]# mkdir /smbdata
[root@smbserver ~]# chown alice:alice /smbdata/
[root@smbserver ~]# chmod 775 /smbdata/
[root@smbserver ~]# ll -d /smbdata/
drwxrwxr-x. 2 alice alice 4096 May 16 14:04 /smbdata/
semanage fcontext -a -t samba_share_t '/smbdata(/.*)?' # 修改共享目录的安全上下文
restorecon -RvvF /smbdata/ # 目录全部内容生效
[root@smbserver ~]# getsebool -a | grep samba # 查看selinux策略samba相关策略布尔值 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_load_libgfapi --> off samba_portmapper --> off samba_run_unconfined --> off samba_share_fusefs --> off samba_share_nfs --> off sanlock_use_samba --> off tmpreaper_use_samba --> off use_samba_home_dirs --> off virt_use_samba --> off [root@smbserver ~]# setsebool -P samba_enable_home_dirs on ##允许进入samba的用户家目录 [root@smbserver ~]# smbpasswd -a student # 添加samba用户,student 必须是本机用户,输入相关密码,用于客户端登录 New SMB password: Retype new SMB password: Added user student. [root@smbserver ~]# pdbedit -L # 查看用户列表 student:1001: [root@smbserver ~]# systemctl restart smb
[root@smbserver ~]# useradd alice [root@smbserver ~]# useradd bob [root@smbserver ~]# smbpasswd -a alice New SMB password: Retype new SMB password: Added user alice. [root@smbserver ~]# smbpasswd -a bob New SMB password: Retype new SMB password: Added user bob. [root@smbserver ~]# pdbedit -L student:1001: bob:1003: alice:1002: [root@smbserver ~]# pdbedit -x student # 删除用户,测试,等下加回去 [root@smbserver ~]# pdbedit -L bob:1003: alice:1002:
安装客户端软件
[root@smbclient ~]# yum install samba-client.x86_64 cifs-utils -y
匿名用户查看测试
[root@smbclient ~]# smbclient -L //192.168.217.153 Enter SAMBA\root's password: Anonymous login successful Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers smbtest Disk IPC$ IPC IPC Service (Samba 4.10.4) Reconnecting with SMB1 for workgroup listing. Anonymous login successful Server Comment --------- ------- Workgroup Master --------- -------
具体samba用户查看
[root@smbclient ~]# smbclient -L //192.168.217.153 -U student Enter SAMBA\student's password: Sharename Type Comment --------- ---- ------- print$ Disk Printer Drivers smbtest Disk IPC$ IPC IPC Service (Samba 4.10.4) student Disk Home Directories Reconnecting with SMB1 for workgroup listing. Server Comment --------- ------- Workgroup Master --------- -------
进入samba服务器共享目录
现在服务端创建touch /smbdata/test
[root@smbclient ~]# smbclient //192.168.217.153/smbtest -U student
Enter SAMBA\student's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Sat May 16 23:50:48 2020
.. D 0 Sat May 16 20:59:30 2020
test N 0 Sat May 16 23:50:48 2020
10190100 blocks of size 1024. 8479152 blocks available
smb: \>
截图
此时,匿名用户将无法登录。
[root@smbclient ~]# smbclient //192.168.217.153/smbtest
Enter SAMBA\root's password:
Anonymous login successful
tree connect failed: NT_STATUS_ACCESS_DENIED
挂载共享目录
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=student,password=123
[root@smbclient ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 10190100 9380624 268804 98% /
devtmpfs 490212 0 490212 0% /dev
tmpfs 499848 0 499848 0% /dev/shm
tmpfs 499848 6876 492972 2% /run
tmpfs 499848 0 499848 0% /sys/fs/cgroup
/dev/sda1 194235 89327 90572 50% /boot
tmpfs 99972 0 99972 0% /run/user/0
//192.168.217.153/smbtest 10190100 1170276 8479152 13% /mnt
注意:要输入对应的samba用户和密码,否则无法挂载成功。
此时,进入共享目录/mnt,只读,不可写,
[root@smbclient ~]# cd /mnt
[root@smbclient mnt]# ls
test
[root@smbclient mnt]# rm -rf test
rm: cannot remove ‘test’: Read-only file system
[root@smbclient mnt]# ll
total 0
-rw-r--r-- 1 root root 0 May 16 23:50 test
[root@smbserver ~]# vim /etc/samba/smb.conf
comment = this is share dir
[root@smbserver ~]# systemctl restart smb
客户端测试
[root@smbserver ~]# vim /etc/samba/smb.conf
browseable=no
[root@smbserver ~]# systemctl restart smb
测试
[root@smbclient ~]# smbclient -L //192.168.217.153 -U student
这时看不到共享目录
虽然隐藏了,但依旧可以挂载。
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=student,password=123
[root@smbclient ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 10190100 9381280 268148 98% /
devtmpfs 490212 0 490212 0% /dev
tmpfs 499848 0 499848 0% /dev/shm
tmpfs 499848 6880 492968 2% /run
tmpfs 499848 0 499848 0% /sys/fs/cgroup
/dev/sda1 194235 89327 90572 50% /boot
tmpfs 99972 0 99972 0% /run/user/0
//192.168.217.153/smbtest 10190100 1170264 8479164 13% /mnt
注:卸载不了,就用这个
[root@smbclient ~]# yum install -y fuser
[root@smbclient ~]# fuser -mv /mnt/
USER PID ACCESS COMMAND
/mnt: root kernel mount /mnt
root 1157 ..c.. bash
[root@smbclient ~]# fuser -kv /mnt/ # 杀死进程
[root@smbclient ~]# fuser -mv /mnt/
USER PID ACCESS COMMAND
/mnt: root kernel mount /mnt
[root@smbserver ~]# vim /etc/samba/smb.confvim /etc/samba/smb.conf
valid users=+alice
[root@smbserver ~]# systemctl restart smb
这时,我们把用户bob加入到alice的附加组中,此时bob也是alice组的组成员,故也是有效用户.
[root@smbserver ~]# usermod -G alice bob
[root@smbserver ~]# id alice
uid=1002(alice) gid=1002(alice) groups=1002(alice)
[root@smbserver ~]# id bob
uid=1003(bob) gid=1003(bob) groups=1003(bob),1002(alice)
测试alice用户:
[root@smbclient ~]# smbclient //192.168.217.153/smbtest -U alice
Enter SAMBA\alice's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Sat May 16 23:50:48 2020
.. D 0 Sat May 16 20:59:30 2020
test N 0 Sat May 16 23:50:48 2020
10190100 blocks of size 1024. 8479108 blocks available
smb: \>
测试bob用户:
[root@smbclient ~]# smbclient //192.168.217.153/smbtest -U bob
Enter SAMBA\bob's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Sat May 16 23:50:48 2020
.. D 0 Sat May 16 20:59:30 2020
test N 0 Sat May 16 23:50:48 2020
10190100 blocks of size 1024. 8479108 blocks available
smb: \>
测试无效用户student,连接被拒。不给student用户登录了。
[root@smbclient ~]# smbclient //192.168.217.153/smbtest -U stduent
Enter SAMBA\stduent's password:
session setup failed: NT_STATUS_LOGON_FAILURE
[root@smbclient ~]# smbclient //192.168.217.153/smbtest -U stduent
Enter SAMBA\stduent's password:
session setup failed: NT_STATUS_LOGON_FAILURE
我们先对共享目录进行一个挂载,当我们进入到挂载好的目录时,发现不能进行写操作。
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=bob,password=123 [root@smbclient ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 10190100 9381292 268136 98% / devtmpfs 490212 0 490212 0% /dev tmpfs 499848 0 499848 0% /dev/shm tmpfs 499848 6880 492968 2% /run tmpfs 499848 0 499848 0% /sys/fs/cgroup /dev/sda1 194235 89327 90572 50% /boot tmpfs 99972 0 99972 0% /run/user/0 //192.168.217.153/smbtest 10190100 1170316 8479112 13% /mnt [root@smbclient ~]# cd /mnt/ [root@smbclient mnt]# ls test [root@smbclient mnt]# rm -rf test rm: cannot remove ‘test’: Read-only file system
在服务端
[root@smbserver ~]# vim /etc/samba/smb.conf
writable=yes
[root@smbserver ~]# systemctl restart smb
在客户端测试:
发现成功删除,可以进行写操作。
[root@smbclient mnt]# ls
123 test
[root@smbclient mnt]# rm -rf test
[root@smbclient mnt]# ls
123
当然,用户可写是建立在目录可写的前提下,注意此时的可写列表中+或者@表示其用户组成员也可写。
服务端:
[root@smbserver ~]# vim /etc/samba/smb.conf
write list = @alice
[root@smbserver ~]# systemctl restart smb
客户端测试:
当使用alice用户组成员bob挂载时,可以对共享目录写。
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=bob,password=123
[root@smbclient ~]# cd /mnt
[root@smbclient mnt]# ls
123 test
[root@smbclient mnt]# touch bob2020
[root@smbclient mnt]# ls
123 bob2020 test
我们卸载共享目录/mnt,再通过samba用户westos挂载,测试是否可写。发现也可写,因为再可写用户列表中。
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=alice,password=123 [root@smbclient ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 10190100 9381340 268088 98% / devtmpfs 490212 0 490212 0% /dev tmpfs 499848 0 499848 0% /dev/shm tmpfs 499848 6880 492968 2% /run tmpfs 499848 0 499848 0% /sys/fs/cgroup /dev/sda1 194235 89327 90572 50% /boot tmpfs 99972 0 99972 0% /run/user/0 //192.168.217.153/smbtest 10190100 1170324 8479104 13% /mnt [root@smbclient ~]# cd /mnt/ [root@smbclient mnt]# ls 123 bob2020 test [root@smbclient mnt]# touch alice2020 [root@smbclient mnt]# ls 123 alice2020 bob2020 test
同理,我们再测试不在可写用户列表里的student。
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=student,password=123 [root@smbclient ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 10190100 9381344 268084 98% / devtmpfs 490212 0 490212 0% /dev tmpfs 499848 0 499848 0% /dev/shm tmpfs 499848 6880 492968 2% /run tmpfs 499848 0 499848 0% /sys/fs/cgroup /dev/sda1 194235 89327 90572 50% /boot tmpfs 99972 0 99972 0% /run/user/0 //192.168.217.153/smbtest 10190100 1170348 8479080 13% /mnt [root@smbclient ~]# cd /mnt [root@smbclient mnt]# ls 123 alice2020 bob2020 test [root@smbclient mnt]# rm -rf test rm: cannot remove ‘test’: Permission denied
服务端
[root@smbserver ~]# vim /etc/samba/smb.conf
guest ok = yes
[root@smbserver ~]# systemctl restart smb
客户端测试
[root@smbclient ~]# smbclient //192.168.217.153/smbtest
Enter SAMBA\root's password:
Anonymous login successful
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Sun May 17 02:06:24 2020
.. D 0 Sat May 16 20:59:30 2020
alice2020 N 0 Sun May 17 01:43:31 2020
bob2020 N 0 Sun May 17 01:41:58 2020
123 N 0 Sun May 17 02:06:16 2020
test N 0 Sun May 17 02:06:24 2020
10190100 blocks of size 1024. 8479084 blocks available
smb: \>
这时匿名用户挂载不了
服务端
在配置文件中找到[global],在此标示下面添加
[root@smbserver ~]# vim /etc/samba/smb.conf
map to guest = bad user
[root@smbserver ~]# systemctl restart smb
在客户端测试:
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt/ Password for root@//192.168.217.153/smbtest: # 直接回车 [root@smbclient ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 10190100 9381832 267596 98% / devtmpfs 490212 0 490212 0% /dev tmpfs 499848 0 499848 0% /dev/shm tmpfs 499848 6880 492968 2% /run tmpfs 499848 0 499848 0% /sys/fs/cgroup /dev/sda1 194235 89327 90572 50% /boot tmpfs 99972 0 99972 0% /run/user/0 //192.168.217.153/smbtest 10190100 1170312 8479116 13% /mnt [root@smbclient ~]# cd /mnt [root@smbclient mnt]# ls 123 alice2020 bob2020 test [root@smbclient mnt]# rm -rf test rm: cannot remove ‘test’: Permission denied
发现挂载成功,但是发现,匿名用户不可以写。原因是,除了服务之外,还有共享目录权限问题,可以在服务端进行修改权限777,客户端匿名用户就可以写了,但是一般不建议这么做。
参数admin users = student,表示在使用student登陆了samba之后,就对于这个共享目录相当于root身份。
在服务端:
[root@smbserver ~]# vim /etc/samba/smb.conf
admin users = student
[root@smbserver ~]# systemctl restart smb
注意:此时可写用户列表只有westos用户组的用户可以,而student并不是。
在客户端测试:
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o username=student,password=123 [root@smbclient ~]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda2 10190100 9381836 267592 98% / devtmpfs 490212 0 490212 0% /dev tmpfs 499848 0 499848 0% /dev/shm tmpfs 499848 6880 492968 2% /run tmpfs 499848 0 499848 0% /sys/fs/cgroup /dev/sda1 194235 89327 90572 50% /boot tmpfs 99972 0 99972 0% /run/user/0 //192.168.217.153/smbtest 10190100 1170336 8479092 13% /mnt [root@smbclient ~]# cd /mnt [root@smbclient mnt]# ls 123 alice2020 bob2020 test [root@smbclient mnt]# rm -rf test [root@smbclient mnt]# ls 123 alice2020 bob2020
我们使用student用户挂载共享目录,但并没有可写权限,但是依然可以对目录进行写操作,是因为我们提升了student的用户级别为root用户,root用户当然可以写了。
新建一个文件/root/smb_auth,里面写入服务端smb用户的用户名和密码,并修改文件的权限,默认情况下只有root用户可读写:
[root@smbclient ~]# vim /root/smb_auth
username=alice
password=123
[root@smbclient ~]# chmod 600 /root/smb_auth
通过这种方式可以直接挂载,而不在历史记录中直接显示出密码:
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o credentials=/root/smb_auth
[root@smbclient ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 10190100 9381844 267584 98% /
devtmpfs 490212 0 490212 0% /dev
tmpfs 499848 0 499848 0% /dev/shm
tmpfs 499848 6880 492968 2% /run
tmpfs 499848 0 499848 0% /sys/fs/cgroup
/dev/sda1 194235 89327 90572 50% /boot
tmpfs 99972 0 99972 0% /run/user/0
//192.168.217.153/smbtest 10190100 1170392 8479036 13% /mnt
然而此时其他用户还是可以看到root用户挂载目录下的内容:
[root@smbclient ~]# su - hehe
[hehe@smbclient ~]$ cd /mnt/
[hehe@smbclient mnt]$ ls
123 alice2020 bob2020
[root@smbclient ~]# mount //192.168.217.153/smbtest /mnt -o credentials=/root/smb_auth,sec=ntlmssp,multiuser
[root@smbclient ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 10190100 9381852 267576 98% /
devtmpfs 490212 0 490212 0% /dev
tmpfs 499848 0 499848 0% /dev/shm
tmpfs 499848 6880 492968 2% /run
tmpfs 499848 0 499848 0% /sys/fs/cgroup
/dev/sda1 194235 89327 90572 50% /boot
tmpfs 99972 0 99972 0% /run/user/0
//192.168.217.153/smbtest 10190100 1170392 8479036 13% /mnt
测试
[root@smbclient ~]# su - hehe
[hehe@smbclient ~]$ cd /mnt/
[hehe@smbclient mnt]$ ls
ls: reading directory .: Permission denied
如果我们想让其他用户查看,则需要得到一个服务端smb用户的身份才可以。
注意:必须是服务端已有的smb用户
[hehe@smbclient mnt]$ cifscreds add -u alice 192.168.217.153
Password:
[hehe@smbclient mnt]$ ls
123 alice2020 bob2020
也就是说,前提你必须知道smb服务器smb用户的密码才可以看共享目录,更安全。
在客户端
[root@smbclient ~]# vim /etc/fstab
//192.168.217.153/smbtest /mnt cifs defaults,credentials=/root/smb_auth,sec=ntlmssp,multiuser 0 0
[root@smbclient ~]# mount -a
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。