Linux- 关于samba你应该懂的操作啊~

samba

Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。


服务器:系统(硬盘)的用途

网络文件系统
              sambac-cifs  -windows+svn linux unix
(
自动挂在卸载) nfs-nfs  IBM 403  yum clean all
 

yum search samb


yum install samba-common.x86_64 samba.x86_64 samba-client.x86_64 
安装这三个


systemctl start smb 
开启
netstat -anlpte | grep smb
两个接口

netstat -anlpte | grep nmb
smbclient -L //172.25.254.182 匿名提示输入密码 没有密码就直接回车

Enter root's password:
回车
Anonymous login successful(
匿名)
Domain=[MYGROUP] OS=[Windows 6.1] Server=[Samba 4.2.3]

    Sharename       Type     Comment
    ---------       ----     -------
    IPC$            IPC       IPC Service (Samba Server Version4.2.3)
Anonymous login successful
Domain=[MYGROUP] OS=[Windows 6.1] Server=[Samba 4.2.3]

    Server               Comment
    ---------            -------

    Workgroup            Master


 id student  (
必须是系统有的用户)
[[email protected] ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
smbpasswd -a student


[[email protected] ~]# smbpasswd -a student 
加入用户
New SMB password:
Retype new SMB password:
Added user student.

smbclient  //172.25.254.182 -U student用户登陆没有-L


getenforce
 getsebool -a | grep samba
[[email protected] ~]# systemctl stop iptables.service (测试的时候保证火墙是关闭的状态 selinux不是强制状态 或者也可以选择更改安全上下文 就不用更改selinux的状态)

82主机 进行挂载 mount -o username=student,password=123 //172.25.254.182/student /mnt/ (挂载到/mnt/下面共享自己目录


更改workgroup的名字为annie vim /etc/samba/smb.conf

重启服务进行测试查看 smbclient  -L  //172.25.254.182


在配置文件中修改只给那个ip看 这里是只给172.25.254.81看


然后我们用82 主机进行测试 会发现被禁止了


使用81主机进行测试 登录成功


同样的我们也可以只对某一个ip进行禁止 原理同上 这里就不多加进行测试了


2 共享目录的基本设定

vim /etc/samba/smb.conf

320 ;       write list = +staff
321         [DATA] 共享目录名称
322         comment = weixin data 对共享目录的描述
323         path = /westos 共享目录的绝对路径
324 
systemctl restart smb.service 修改完进行重启使得修改的配置生效



测试进行查看  smbclient -L //172.25.254.182


查看一下共享目录的东西


[[email protected] ~]# mkdir /annie  建立目录

[[email protected] ~]# ls -ld /annie/
drwxr-xr-x. 2 root root 6 Dec  3 01:05/annie/
[[email protected] ~]# touch /annie/file{1..3}
[[email protected] ~]# ls /annie/
file1  file2  file3              

如上图在共享目录下建立了三个文件 之后我们再用smb进行查看 会发现多了刚才建立的三个文件


这里如果我们把selinux 改为强制状态 会发现访问不了 这时我们可以通过更改文件上下文的方式来使其可以正常访问 setenforce 1


更改文件上下文的方式如下所示


[[email protected] ~]# ls -Zd /annie/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /annie/
[[email protected] ~]# semanage fcontext -a -t samba_share_t '/annie(/.*)?'
[[email protected] ~]# ls -Zd /westos/
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /annie/
[[email protected] ~]# restorecon -FvvR /annie/
restorecon reset /westos contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file1 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file2 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file3 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
之后再用smb的方式进行访问试试 发现访问可以成功了
我们把/DATA用普通用户的身份挂载到/mnt下 mount -o username=student,password=123 /mnt/ 

df查看挂载是否成功


上面的是临时挂载 这里我们在进行一下永久挂载 vim /etc/fstab中写入


之后mount -a 使得配置写入成功 df在进行查看挂载是否成功


之后我们修改配置文件使得匿名用户可以实行挂载



匿名用户这里用的guest(windows中的匿名用户也是这个叫法) 之后进行挂载测试 匿名用户 没有密码 挂载的时候直接回车

[[email protected] Desktop]# mount //172.25.254.182/DATA /mnt/ -ousername=guest,password=""  挂载
[[email protected] Desktop]# df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/sda3             307974224 58992412248981812  20% /
devtmpfs                1868076        0  1868076   0% /dev
tmpfs                   1881152      484  1880668   1% /dev/shm
tmpfs                   1881152     9024  1872128   1% /run
tmpfs                   1881152        0  1881152   0% /sys/fs/cgroup
/dev/sda1                508588   149304   359284  30% /boot
/dev/loop0              3947824  3947824         0 100% /var/www/html/rhe17.2
tmpfs                    376232       28   376204   1% /run/user/1000
//172.25.254.123/DATA  10473900  3202684  7271216  31% /mnt

vim /etc/fstab    
永久挂载
UUID=4ffae361-4c26-4403-b4e3-dd4855630611 swap                    swap    defaults        0 0
//172.25.254.182/DATA  /mnt cifs  defaults,username=guest,password=""0  0 


同样的匿名用户也可以用这个方法进行永久挂载


之后mount -a 使得配置写入成功 df在进行查看挂载是否成功


设定可写 


之后我们挂载后切换到/mnt/下进行测试 发现还是无法建立和删除文件


这时因为对于这个文件我们还没有权限 仅仅是服务支持 但是权限不支持  所以这里我们就要进行权限的给予


之后再到挂载的目录下进行测试 发现文件的建立与删除都可以 则测试成功


设定指定用户可写  我们也可以设定只有谁能写
write list = student #可写用户 
write list = +student #可写用户组 
write list = @student  如下图所示这里我们就设定的是只有student用户可写


我们用匿名用户进行测试一下 切换到挂载目录下 发现果然不可写

[[email protected] Desktop]# mount //172.25.254.182/DATA /mnt/ -ousername=guest,password=""

[[email protected] Desktop]# cd /mnt
[[email protected] mnt]# touch file
touch: cannot touch ‘file’: Permission denied 
(不可写)



studen用户登录发现还是正常可写 测试成功


admin users = admin #共享的超级用户指定

 admin users = admin 赋予超户身份


建立一个admin用户 并将其加入smb中


之后用admin用户进行挂载测试 挂载目录/mnt下看看是否可以写 发现果然可写这里使用的就是超级用户的权利


browseable=no 可浏览功能 valid users = +student只能student 的用户组才能浏览

之后进行测试 用annie用户进行测试 因为他属于student 的所有组(上面加过的) 所以可以进行登录查看


用其他用户riven进行登录发现查看不了 内容被隐藏了



[[email protected] ~]# rpm -qa | grep samba
samba-client-libs-4.2.3-10.el7.x86_64
samba-client-4.2.3-10.el7.x86_64
samba-common-4.2.3-10.el7.noarch
samba-common-tools-4.2.3-10.el7.x86_64
samba-common-libs-4.2.3-10.el7.x86_64
samba-libs-4.2.3-10.el7.x86_64
samba-4.2.3-10.el7.x86_64



SMB多用户挂载
原因
[[email protected]~]# mount //172.25.254.182/DATA /mnt/ -o username=student,password=123
[[email protected]~]# cd /mnt
[[email protected] mnt]# touch lh
[[email protected] mnt]# su - kiosk 
能看到不安全 所以这里我们需要用到多用户挂载 使得只有smb服务中的用户才可以看到
Last login: Sun Dec  3 08:37:26 CST 2017on :0
[[email protected] ~]$ ls /mnt
file  file1  file2 file3  l  lh o

(客户机) 这里是在客户中集中进行操作的 !!!!!
[[email protected] ~]# yum install cifs-utils -y
Loaded plugins: langpacks, product-id, search-disabled-repos,subscription-
              : manager
This system is not registered to Red Hat Subscription Management. You can usesubscription-manager to register.
Package cifs-utils-6.2-7.el7.x86_64 already installed and latest version
Nothing to do

[[email protected] ~]# vim /root/smbfile 
编辑文件
[[email protected] ~]# cat /root/smbfile 
里面写入smb用户

username=student
password=123


                                                    SMB
多用户认证
[[email protected] ~]# mount -o credentials=/root/smbfile,multiuser,sec=ntlmssp//172.25.254.182/DATA /mnt

[[email protected] ~]# df
Filesystem            1K-blocks     Used Available Use% Mounted on
/dev/sda3             307974224 58943520249030704  20% /
devtmpfs                1868076        0  1868076   0% /dev
tmpfs                   1881152      484  1880668   1% /dev/shm
tmpfs                   1881152     9024  1872128   1% /run
tmpfs                   1881152        0  1881152   0% /sys/fs/cgroup
/dev/sda1                508588   149304   359284  30% /boot
/dev/loop0              3947824  3947824         0 100% /var/www/html/rhe17.2
tmpfs                    376232       24   376208   1% /run/user/1000
//172.25.254.123/DATA  10473900  3202760  7271140  31% /mnt


[[email protected] ~]# cd /mnt
[[email protected] mnt]# touch lala  
建立文件lalastudent
[[email protected] mnt]# su - kiosk 切换到kiosk用户 用ls 会发现 无法进行查看!!!!
Last login: Sun Dec  3 16:10:53 CST 2017on pts/0
[[email protected] ~]$ ls /mnt
ls: cannot access /mnt: Permission denied
看不到

[[email protected] ~]$ cd /mnt
[[email protected] mnt]$ ls
ls: reading directory .: Permission denied
[[email protected] mnt]$ cifscreds add -u westos 172.25.254.123 
smb用户身份查看
Password:
[[email protected] mnt]$ ls 发现可以查看成功了!!!!!
file  file1  file2 file3  l  lalala1 lalala3
[[email protected] mnt]$ touch ni{1..3}  
建立文件(ni1 ni2 ni3)

服务器端查看
[[email protected] ~]# cd /annie/ 切换到共享目录的绝对路径中进行查看
[[email protected] annie]# ls
file  file1 ile2  file3 l  lalala1 lalala3 ni1 ni2 ni3可以看到刚才客户端用annie用户登陆创建的三个文件



                                                                                                           over~