您的位置:首页 > 运维架构 > Linux

Linux之SMB/CIFS网络文件系统

2018-06-07 00:55 134 查看

SMB 文件共享

  • 是windows和Unix系统之间共享文件的一种协议
  • 主要用于Windows 和 Linux下的文件共享、打印共享
  • 实现匿名与本地用户文件的共享
  • 支持多节点挂载

Samba 服务的组成部分

软件包:
Samba的支持文件:Samba-common
客户端应用程序:Samba-client
服务器应用程序:Samba
服务名称:smb nmb
服务进程:
1、smbd进程:控制发布共享目录与权限、负责文件传输;TCP 139 445端口
2、nmbd进程:用于名称解析 UDP 137 138
主配置文件:/etc/samba/smb.conf
实验:
准备环境:
1、重置两台虚拟机,作为实验的两台主机
2、配置两台主机的 ip、yum源、主机名
服务端:

[root@server ~]# yum install samba samba-client samba-common #下载smb相关软件
[root@server ~]# systemctl start smb  #开启samba服务
[root@server ~]# systemctl enable smb
[root@server ~]# systemctl stop firewalld   #关闭防火墙
[root@server ~]# systemctl disable firewalld

查看smb的端口:

SMB 用户

smbpasswd
如果您没有Samba密码服务器,则必须在本地计算机上创建身份验证数据。使用smbpasswd创建Samba账户和密码
注意:smb用户必须是本地存在的用户
服务端:

[root@server ~]# useradd haha
[root@server ~]# id student
uid=1000(student) gid=1000(student) groups=1000(student),10(wheel)
[root@server ~]# id haha
uid=1001(haha) gid=1001(haha) groups=1001(haha)
[root@server ~]# smbpasswd -a student  #添加用户student并设置密码,这里的用户必须是系统实际存在用户
New SMB password:
Retype new SMB password:
Added user student.
[root@server ~]# smbpasswd -a haha
New SMB password:
Retype new SMB password:
Added user haha.
[root@server ~]# pdbedit -L  #查看smb用户
student:1000:Student User
haha:1001:
[root@server ~]# pdbedit -x student   #删除smb用户student
[root@server ~]# pdbedit -L
haha:1001:
[root@server ~]# smbpasswd -a student
New SMB password:
Retype new SMB password:
Added user student.

客户端:

[root@client ~]# yum install samba-client -y
[root@client ~]# smbclient -L //172.25.254.196   #列出196主机的samba服务的文件系统,-L 列出
Enter root's password:    #因为服务器没有添加smb用户,这里直接出车(匿名用户访问),显示的没有具体文件
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

Sharename       Type      Comment
---------       ----      -------
IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]

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

Workgroup            Master
---------            -------
[root@client ~]# smbclient  //172.25.254.196/student -U student   #指定用户登录访问smb文件系统
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
smb: \> quit
[root@client ~]# smbclient  //172.25.254.196/student -U student
Enter student's password:
session setup failed: NT_STATUS_LOGON_FAILURE

保护 smb

布尔值允许本地Linux主目录作为CIFS文件共享导出至其他系统:
samba_enable_home_dirs
布尔值允许挂载远程CIFS文件共享并将其用作本地Linux主目录:
use_samba_ home_dirs
smb共目录的selinux安全上下文: samba_share_t
共享系统目录时要打开:
samba_export_all _ro 和 samba_export_all_rw

服务端:
查看布尔值允许本地目录作为 cifs 文件共享的相关主目录

[root@server ~]# setsebool -P samba_enable_home_dirs on   #打开访问samba的布尔值

客户端:

[root@client ~]# smbclient  //172.25.254.196/student -U student   #访问student的文件系统可以查看
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
.                                   D        0  Thu Jul 10 19:06:52 2014
..                                  D        0  Tue Jun  5 07:51:50 2018
.bash_logout                        H       18  Wed Jan 29 07:45:18 2014
.bash_profile                       H      193  Wed Jan 29 07:45:18 2014
.bashrc                             H      231  Wed Jan 29 07:45:18 2014
.ssh                               DH        0  Thu Jul 10 18:19:10 2014
.config                            DH        0  Thu Jul 10 19:06:53 2014

40913 blocks of size 262144. 28554 blocks available
smb: \> quit

smb 挂载

客户端:

[root@client ~]# smbclient  //172.25.254.196/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> put /etc/passwd   #直接通过路径不能上传文件,需要前进入到文件的所在目录
NT_STATUS_OBJECT_PATH_NOT_FOUND opening remote file \/etc/passwd
smb: \> quit
[root@client ~]# cd /etc/   #进入文件所在目录
[root@client etc]# smbclient  //172.25.254.196/student -U student
Enter student's password:
Domain=[MYGROUP] OS=[Unix] Server=[Samba 4.1.1]
smb: \> put passwd    #上传文件
putting file passwd as \passwd (244.7 kb/s) (average 244.8 kb/s)
smb: \> quit

服务端:

[root@server ~]# cd /home/student/
[root@server student]# ls   #查看到文件以上传
passwd

1、临时挂载

客户端:

[root@client ~]# mount //172.25.254.196/student /mnt/ -o username=student,password=haha
[root@client ~]# df
Filesystem               1K-blocks    Used Available Use% Mounted on
/dev/vda1                 10473900 3178624   7295276  31% /
devtmpfs                    469344       0    469344   0% /dev
tmpfs                       484932     140    484792   1% /dev/shm
tmpfs                       484932   12804    472128   3% /run
tmpfs                       484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo          483670    2339    451840   1% /home
//172.25.254.196/student  10473900 3163960   7309940  31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# touch haha{1..3}
[root@client mnt]# ls
haha1  haha2  haha3  passwd
[root@client mnt]# rm -fr *
[root@client mnt]# ls
[root@client mnt]# touch file
[root@client mnt]# ls
file
[root@client mnt]# cd
[root@client ~]# umount /mnt

服务端:

[root@server ~]# ls /home/student/
file

2、永久挂载

第一种方式:如果服务没开,会影响系统启动,不建议使用

客户端:

[root@client ~]# vim /etc/fstab

[root@client ~]# mount -a
[root@client ~]# df
Filesystem               1K-blocks    Used Available Use% Mounted on
/dev/vda1                 10473900 3178644   7295256  31% /
devtmpfs                    469344       0    469344   0% /dev
tmpfs                       484932     140    484792   1% /dev/shm
tmpfs                       484932   12804    472128   3% /run
tmpfs                       484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo          483670    2339    451840   1% /home
//172.25.254.196/student  10473900 3167768   7306132  31% /mnt
第二种方式:在所有服务启动后,初始化的时候开始执行,不会影响系统启动

客户端:

[root@client ~]# vim /etc/rc.d/rc.local

[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 3177704   7296196  31% /
devtmpfs                    469344       0    469344   0% /dev
tmpfs                       484932      80    484852   1% /dev/shm
tmpfs                       484932   12764    472168   3% /run
tmpfs                       484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo          483670    2339    451840   1% /home
//172.25.254.196/student  10473900 3167752   7306148  31% /mnt

smb 客户端使用

[root@server ~]# rpm -qc samba-common  #查看samba的配置文件
/etc/logrotate.d/samba
/etc/samba/lmhosts
/etc/samba/smb.conf
/etc/sysconfig/samba

1、修改组名称Domain

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service

客户端:

2、设置黑名单用户、白名单用户

默认白名单,设置的ip是黑名单用户:hosts deny = ip
默认黑名单,设置的ip是白名单用户:hosts allow = ip
实验:
服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service

客户端169:

[root@client ~]# smbclient -L //172.25.254.196
Enter root's password:
protocol negotiation failed: NT_STATUS_INVALID_NETWORK_RESPONSE

客户端69:

[root@foundation69 Desktop]# smbclient -L //172.25.254.196
Enter kiosk's password:
Anonymous login successful
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]

Sharename       Type      Comment
---------       ----      -------
IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]

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

Workgroup            Master
---------            -------

smb 共享目录

smb共享目录的selinux安全上下文: samba_share_t
共享系统目录时要打开:
samba_export_all _ro 和 samba_export_all_rw
文件 /etc/samba/smb.conf 中有修改smb的selinux安全上下文等相关配置的策略

1、自己创建的目录

服务端:

[root@server ~]# mkdir /test
[root@server ~]# touch /test/haha{1..3}
[root@server ~]# semanage fcontext -a -t samba_share_t '/test(/.*)?'    #修改安全上下文
[root@server ~]# restorecon -FvvR /test/    #刷新
restorecon reset /test context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /test/haha1 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /test/haha2 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /test/haha3 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
[root@server ~]# semanage fcontext -l | grep /test    #过滤看是否/test的安全上下文是否修改成功
/test(/.*)?                                        all files          system_u:object_r:samba_share_t:s0
/usr/lib/pgsql/test/regress(/.*)?                  all files          system_u:object_r:postgresql_db_t:s0
/usr/lib/pgsql/test/regress/.*\.sh                 regular file       system_u:object_r:bin_t:s0
/usr/lib/pgsql/test/regress/.*\.so.*               regular file       system_u:object_r:lib_t:s0
/usr/lib/pgsql/test/regress/pg_regress             regular file       system_u:object_r:postgresql_exec_t:s0
[root@server ~]# vim /etc/samba/smb.conf    #编辑配置文件



[DIR] 可以看到的共享目录的名称
comment = westos files 说明
path = /westos 共享位置

[root@server ~]# systemctl restart smb.service

客户端:

[root@client ~]# smbclient //172.25.254.196/DIR
Enter root's password:
Anonymous login successful
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]
tree connect failed: NT_STATUS_ACCESS_DENIED   #这里表示匿名用户不可以访问
[root@client ~]# smbclient //172.25.254.196/DIR -U
20000
student
Enter student's password:
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
.                                   D        0  Tue Jun  5 08:38:47 2018
..                                  D        0  Tue Jun  5 08:38:34 2018
haha1                               N        0  Tue Jun  5 08:38:47 2018
haha2                               N        0  Tue Jun  5 08:38:47 2018
haha3                               N        0  Tue Jun  5 08:38:47 2018

40913 blocks of size 262144. 28539 blocks available
smb: \> quit

2、系统目录

服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service[root@server ~]# setsebool -P samba_export_all_ro on    #打开后可以共享所有目录,比安全上下文级别高

客户端:

[root@client ~]# smbclient //172.25.254.196/mnt -U student
Enter student's password:
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls   #可以查看到系统目录里面的文件
.                                   D        0  Tue Jun  5 08:52:15 2018
..                                  D        0  Tue Jun  5 08:38:34 2018
file1                               N        0  Tue Jun  5 08:52:15 2018
file2                               N        0  Tue Jun  5 08:52:15 2018
file3                               N        0  Tue Jun  5 08:52:15 2018

40913 blocks of size 262144. 28538 blocks available
smb: \> quit

smb 权限管理

1、是否允许浏览:browseable = yes|no
2、是否可写:writable = yes|no
3、允许用户列表:write list = student
4、允许组列表(+或@表示组):write list = +student
5、指定超级用户,在可写情况下才可以执行动作:admin users = haha
6、描述:comment =
7、本地共享目录:path =
实验:
1、
服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service

客户端:

[root@client ~]# smbclient -L //172.25.254.196
Enter root's password:
Anonymous login successful
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]

Sharename       Type      Comment
---------       ----      -------
mnt             Disk      mnt file
IPC$            IPC       IPC Service (Samba Server Version 4.1.1)
Anonymous login successful
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]

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

Workgroup            Master
---------            -------

2、
服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service[root@server ~]# chmod 777 /test

客户端:

[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=student,password=haha
[root@client ~]# touch /mnt/test
[root@client ~]# ls /mnt/
haha1  haha2  haha3  test
[root@client ~]# umount /mnt

3、
服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service

客户端:

[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=student,password=haha
[root@client ~]# touch /mnt/test1
[root@client ~]# ls /mnt/
haha1  haha2  haha3  test  test1
[root@client ~]# umount /mnt
[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=haha,password=haha
[root@client ~]# touch /mnt/test2
touch: cannot touch ‘/mnt/test2’: Permission denied
[root@client ~]# umount /mnt

4、
服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service

客户端:

[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=student,password=haha
[root@client ~]# touch /mnt/test2
[root@client ~]# umount /mnt
[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=haha,password=haha
[root@client ~]# touch /mnt/test3
touch: cannot touch ‘/mnt/test3’: Permission denied
[root@client ~]# umount /mnt

服务端:

[root@server ~]# usermod -G student haha
[root@server ~]# id haha
uid=1001(haha) gid=1001(haha) groups=1001(haha),1000(student)

客户端:

[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=haha,password=haha
[root@client ~]# touch /mnt/test3
[root@client ~]# umount /mnt

5、
服务端:

[root@server ~]# vim /etc/samba/smb.conf

[root@server ~]# systemctl restart smb.service

客户端:

[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=student,password=haha
[root@client ~]# touch /mnt/test4
[root@client ~]# ls -l /mnt/test4
-rw-r--r-- 1 student student 0 Jun  5 09:33 /mnt/test4
[root@client ~]# umount /mnt
[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=haha,password=haha
[root@client ~]# touch /mnt/test5
[root@client ~]# ls -l /mnt/test5
-rw-r--r-- 1 root 1001 0 Jun  5 09:33 /mnt/test5
[root@client ~]# umount /mnt

smb 多用户挂载

man mount.cifs 可以查看挂载规则
客户端:

[root@client ~]# yum install cifs-utils -y
[root@client ~]# vim /root/smbpass   #根据挂载规则编写认证文件

[root@client ~]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.196/DIR /mnt
###
挂载,credentials=/root/smbpass文件指定的用户名、密码
sec=ntlmssp 认证方式(因为下载的samba是4.1的,所有认证方式是ntlmssp;查询方式rpm -ql | grep samba)
multiuser为多用户挂载
###
[root@client mnt]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3183100   7290800  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      80    484852   1% /dev/shm
tmpfs                   484932   12792    472140   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo      483670    2355    451824   1% /home
//172.25.254.196/DIR  10473900 3168556   7305344  31% /mnt
[root@client ~]# cd /mnt
[root@client mnt]# ls
haha1  haha2  haha3  test  test1  test2  test3  test4  test5
[root@client mnt]# useradd test1
[root@client mnt]# su - test1
[test1@client ~]$ cd /mnt
[test1@client mnt]$ ls   #切换到普通用户后不能查看mnt下的文件,必须指定用户挂载通过smb认证才可以查看
ls: reading directory .: Permission denied
[test1@client mnt]$ exit
logout
[root@client mnt]# cifscreds --help   #查看相关命令
cifscreds: unrecognized option '--help'
Usage:
cifscreds add [-u username] [-d] <host|domain>
cifscreds clear [-u username] [-d] <host|domain>
cifscreds clearall
cifscreds update [-u username] [-d] <host|domain>
[root@client mnt]# su - test1
Last login: Tue Jun  5 09:42:51 EDT 2018 on pts/0
[test1@client ~]$ cifscreds add -u haha 172.25.254.196   #添加haha用户挂载时需要输入密码,才可通过smb认证
Password:
[test1@client ~]$ ls /mnt/   #可以查看到mnt下的文件
haha1  haha2  haha3  test  test1  test2  test3  test4  test5

smb 匿名用户访问

linux 的匿名用户 Anonymous
windows 的匿名用户 guest
服务端:

[root@server ~]# vim /etc/samba/smb.conf


[root@server ~]# systemctl restart smb.service

客户端:

[root@client ~]# smbclient //172.25.254.196/DIR   #匿名访问,可以看到目录的内容
Enter root's password:
Domain=[HAHA] OS=[Unix] Server=[Samba 4.1.1]
smb: \> ls
.                                   D        0  Tue Jun  5 09:33:41 2018
..                                  D        0  Tue Jun  5 08:38:34 2018
haha1                               N        0  Tue Jun  5 08:38:47 2018
haha2                               N        0  Tue Jun  5 08:38:47 2018
haha3                               N        0  Tue Jun  5 08:38:47 2018
test                                N        0  Tue Jun  5 09:07:48 2018
test1                               N        0  Tue Jun  5 09:15:36 2018
test2                               N        0  Tue Jun  5 09:18:43 2018
test3                               N        0  Tue Jun  5 09:19:40 2018
test4                               N        0  Tue Jun  5 09:33:03 2018
test5                               N        0  Tue Jun  5 09:33:41 2018

40913 blocks of size 262144. 28537 blocks available
smb: \> quit
[root@client ~]# mount //172.25.254.196/DIR /mnt -o username=guest,password=""
[root@client ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/vda1             10473900 3183108   7290792  31% /
devtmpfs                469344       0    469344   0% /dev
tmpfs                   484932      80    484852   1% /dev/shm
tmpfs                   484932   12792    472140   3% /run
tmpfs                   484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-vo      483670    2355    451824   1% /home
//172.25.254.196/DIR  10473900 3168280   7305620  31% /mnt
[root@client ~]# umount /mnt
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: