samba服务

一:了解Samba服务

1:samba服务的概念

就是实现了多系统,就是windows,linux这些系统上实现了cifs/smb协议的自由软件,基于这个的出现,更容易的实现了linux和windows之间的文件共享和打印机的共享

在windows上叫cifs,在Linux上叫smb

2:samba的功能

2个核心的守护进程,nmbd,smbd

nmbd进程:NetBLOS名称服务器进程,主要就是让客户端能够通过域名解析来访问samba服务器,还有就是提供文件资源列表

smbd进程:SMB服务进程,管理samba服务器上的共享目录,打印机等,实现网络上的资源共享,监听的端口 139。445

3:samba的工作原理

1:协议的确定

就是客户端先去发送一个smb数据包,列出支持的所有的smb协议包,然后服务器列出支持的smb协议

2:建立连接

确认后协议的,客户端提交账户和密码,服务器确认后,并分配用户唯一的UID

3:访问共享资源

客户端会发送一个tree connect来访问

4:断开连接

服务器发送tree disconnect 

 

二:熟悉Samba服务的配置文件

1:配置文件/etc/samba/smb.conf

里面分为全局设置和局部设置

全局设置就是对整个服务的配置文件起作用的设置

1)全局设置

1
2
3
4
5
6
7
8
9
10
[global]
        workgroup = SAMBA
        security = user
 
        passdb backend = tdbsam
 
        printing = cups
        printcap name = cups
        load printers = yes
        cups options = raw 

重要的参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[global] 就是全局设置
workgroup =  SAMBA 就是工作组(一般不改这个配置,samba服务器加入的工作域的名称)
security = user 
设置安全的验证方式
user:就是允许系统用户访问samba服务器,需要提供用户名和密码,必须是samba账户
匿名方式:就是不需要输入账户和密码,设置为 security = user map to guest = bad user ,自动的将未知用户映射为guest用户
domain:就是windows
asa:就是windows的活动目录passdb backend 用于存储samba用户账号的相关信息的后台类型
取值的类型:
1:smbpasswd 存储在/var/lib/samba/private/smbpasswd这个文件中。客户端通过这个用户的信息就能访问samba服务器的资源
2:tdbsam 就是存放在 passdb.tdb这个文件里面
 
使用smbpasswd或者pdbedit来创建samba用户的账号和密码
 
格式如下:
smbpasswd -a 用户名 ---添加用户名
smbpasswd -x 用户名 ---删除用户名
 
pdbedit -a 用户名
            -x 用户名
            -L 列出Samba的账号
             Lv 就是列出详细的信息

  

 

 2)局部设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[public]
        comment = public directory
        path = /samba-share
        browseable = yes
        writable = yes
        valid users = @kk
        interfaces = 192.168.20.111/24
        hosts allow = 192.168.20.
 
常用的参数
[public]:为文件的共享名
comment:就是关于文件名的注释信息
browseable:就是在浏览的时候,是否显示信息
writable:是否具有写的权限
write list:哪些用户具有写的权限
valid users:能够登录samba服务器的用户或者组 @组名
interfaces:这个服务通过哪个网卡提供
hosts allow:能够访问samba服务器的网段

 

  

三:Samba服务的搭建

主要的流程:安装---

目的就是实现q7和q8这个用户能够通过linux和windows去访问samba服务器上的资源,拥有读写的权限

服务器上面的操作:

1:安装samba软件包

1
[root@controller samba]# yum -y install samba*

2:启动这个软件并且设置为开机自启

1
2
3
4
5
[root@controller samba]# systemctl start nmb && systemctl start smb
[root@controller samba]# systemctl enable nmb && systemctl enable smb
Created symlink /etc/systemd/system/multi-user.target.wants/nmb.service → /usr/lib/systemd/system/nmb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
[root@controller samba]#

3:创建samba用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@controller /]# smbpasswd -a q7
New SMB password:
Retype new SMB password:
Added user q7.
[root@controller /]#
 
[root@controller /]# smbpasswd -a q8
New SMB password:
Retype new SMB password:
Added user q8.
[root@controller /]# <br><br>    groupadd kk
    usermod -G kk q7
    usermod -G kk q8
    cat /etc/group | grep kk
 
#添加写的权限
[root@controller samba-share]# ll -d
drwxr-xr-x. 2 root kk 18 Nov 15 22:00 .
[root@controller samba-share]# chmod g+w ./
[root@controller samba-share]# ll -d
drwxrwxr-x. 2 root kk 18 Nov 15 22:00 .
[root@controller samba-share]#

  

  

4:配置samba服务的配置文件

1
2
3
4
5
6
7
8
9
[public]
        comment = public directory
        path = /samba-share
        browseable = yes
        writable = yes
        valid users = @kk
 
#public 这个就是共享文件共享出去的共享名
browseable:是否允许被浏览

5:检查samba服务的配置文件的格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@controller /]# testparm
Load smb config files from /etc/samba/smb.conf
Loaded services file OK.
Weak crypto is allowed
Server role: ROLE_STANDALONE
 
Press enter to see a dump of your service definitions
 
# Global parameters
[global]
    printcap name = cups
    security = USER
    workgroup = SAMBA
    idmap config * : backend = tdb
    cups options = raw
 
 
[homes]
    browseable = No
    comment = Home Directories
    inherit acls = Yes

6:重启nmb和smb服务

1
2
3
[root@controller /]# systemctl restart nmb
[root@controller /]# systemctl restart smb
[root@controller /]#

7:关闭selinux和防火墙

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@controller samba-share]# systemctl stop firewalld
[root@controller samba-share]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@controller samba-share]# setenforce 0
[root@controller samba-share]# getenforce
Permissive
[root@controller samba-share]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
 
Nov 15 21:19:54 controller systemd[1]: Starting firewalld - dynamic firewall daemon...
Nov 15 21:19:55 controller systemd[1]: Started firewalld - dynamic firewall daemon.
Nov 15 21:19:55 controller firewalld[1054]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure co>
Nov 15 22:08:58 controller systemd[1]: Stopping firewalld - dynamic firewall daemon...
Nov 15 22:08:59 controller systemd[1]: firewalld.service: Succeeded.
Nov 15 22:08:59 controller systemd[1]: Stopped firewalld - dynamic firewall daemon.
[root@controller samba-share]#

    

客户端上面的操作:

1:安装samb-client软件包

1
[root@client yum.repos.d]# yum -y install smb-client  

linux访问的操作:

1)访问共享资源的列表

1
2
3
4
5
6
7
8
9
10
11
[root@client yum.repos.d]# smbclient -L 192.168.20.111 -U q7
Enter SAMBA\q7's password:
 
    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    public          Disk      public directory
    IPC$            IPC       IPC Service (Samba 4.13.3)
    q7              Disk      Home Directories
SMB1 disabled -- no workgroup available
[root@client yum.repos.d]#

2)登陆samba服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@client yum.repos.d]# smbclient //192.168.20.111/public -U q7
Enter SAMBA\q7's password:
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Wed Nov 15 22:00:40 2023
  ..                                  D        0  Wed Nov 15 21:57:25 2023
  flag                                N        0  Wed Nov 15 22:00:40 2023
 
        16250880 blocks of size 1024. 11336596 blocks available
smb: \> mkdir 11
smb: \> ls
  .                                   D        0  Wed Nov 15 22:12:26 2023
  ..                                  D        0  Wed Nov 15 21:57:25 2023
  flag                                N        0  Wed Nov 15 22:00:40 2023
  11                                  D        0  Wed Nov 15 22:12:26 2023
 
        16250880 blocks of size 1024. 11336548 blocks available
smb: \>

通过使用 --help可以知道smbclient有哪些命令

 

3)挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#先安装cifs-utils软件包
[root@client ~]# yum -y install cifs-utils
 
#挂载
[root@client mnt]# mount -o username=q7 //192.168.20.111/public /mnt
 
#查看挂载点
[root@client mnt]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  946M     0  946M   0% /dev
tmpfs                   tmpfs     976M     0  976M   0% /dev/shm
tmpfs                   tmpfs     976M  9.2M  967M   1% /run
tmpfs                   tmpfs     976M     0  976M   0% /sys/fs/cgroup
/dev/mapper/rhel-root   xfs        16G  5.2G   11G  34% /
/dev/nvme0n1p1          xfs       495M  215M  281M  44% /boot
tmpfs                   tmpfs     196M  1.2M  194M   1% /run/user/42
tmpfs                   tmpfs     196M     0  196M   0% /run/user/0
/dev/sr0                iso9660   9.5G  9.5G     0 100% /media
//192.168.20.111/public cifs       16G  4.7G   11G  31% /mnt
[root@client mnt]#

  

 

 

windows访问操作:

1
2
在网络上输入
\\192.168.20.111就能够访问了 

并且拥有权限去修改文件

 

四:问题

就是如果服务的配置文件名没有问题,一直看不到这个服务的相关的信息,那就是selinux没有或者防火墙这些操作没有关闭,就没有显示

 

 

五:selinux开启的状态下,访问

题目:指定用户有写的权限,有的用户没有写的权限

步骤如下:

服务端的配置文件

 

1
2
3
4
5
6
7
8
[qcy]
        comment = qcy directory
        path = /samba-share
        write list = q7
        interfaces = 192.168.10.111/24 192.168.20.111/24
        hosts allow = 192.168.10.
        browseable = yes
        public = yes

 

selinux的设置

1
2
3
4
setsebool -P samba_domain_controller on  #设置域名解析
setsebool -P samba_enable_home_dirs on #设置登录家目录
chcon -t samba_share_t /samba-share/   # 设置共享文件夹的上下文类型
setsebool -P samba_export_all_rw on    # 设置读写权限

    

这样就实现了用户A有读写的权限,用户B没有读写的权限

 

总结:

在selinux关闭的状态下

用户A有写的权限,但是用户B也有写的权限,不符合

writable = no的话 试一试

没有权限,,,就是用户a测试一下,有权限,用户b测试一下,用户b是没有的权限

 

在selinux开启的状态下

samba配置文件里面,有writable = yes的话

用户A有写的权限,用户B也有写的权限

 

没有writable = yes的话

用户A有写的权限,用户B没有写的权限,符合

 

所以一般控制哪些人能够访问权限的话,就使用write list这个参数能够控制这个权限

 

 

实现多用户的能够访问

就是已经使用了一个用户挂载了,再来使用其他的用户来访问,访问的时候,只有自己的权限,没有别人的权限

就是客户端登录到一个用户,去访问samba服务器的时候用的是samba的用户账号 ,

客户端的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#永久挂载,multiuser就是多用户挂载,sec是安全策略
//192.168.10.111/qcy   /mnt    cifs    username=q7,password=000000,multiuser,sec=ntlmssp 0 0
 
[root@client mnt]# mount -a
 
切换到普通用户,使用samba服务的账号去访问samba服务器
cifscreds add -u www controller
[q7@client mnt]$ ls
111  flag
[q7@client mnt]$ mkdir 444
mkdir: cannot create directory ‘444’: Permission denied
[q7@client mnt]$
 
发现权限被拒绝了

 

就实现了多用户去访问同一个文件夹,但是只有自己用户的权限,

就是做了一个自动挂载后,然后登录samba其他用户账号去访问这个文件夹,就是实现了多用户的访问

手动访问也行,麻烦,

 

就是使用cifscreds这个命令将用户和密码写入到内核中去,临时拥有登陆用户的权限,只能使用普通用户,root用户不行,会没有权限的

 

挂载总结:

手动挂载也行,比较麻烦,实现多用户的访问

自动挂载比较方便,就是挂载了之后,,输入命令,实现了多用户的访问

  

 cifscreds add -u samba用户的账号 服务器域名,就实现了多用户的能够去访问 

 

权限的总结

该服务的权限,selinux的权限,samba配置文件的权限,共享文件夹的权限这三个有关

selinux的权限对于samba服务

1
2
3
4
5
6
7
8
9
10
11
# root用户打开此布尔值
[root@controller ~]# setsebool -P samba_domain_controller on
 
# 是否允许访问用户的家目录
[root@controller ~]# setsebool -P samba_enable_home_dirs on
 
# 设置samba共享的文件夹的上下文类型
chcon -t samba_share_t /path/to/directory
 
# 设置该文件读写的权限
setsebool -P samba_export_all_rw on

  

 

防火墙的操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#永久生效
[root@controller /]# firewall-cmd --permanent --add-service=samba
success
[root@controller /]# firewall-cmd --reload
success
[root@controller /]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160 ens192
  sources:
  services: cockpit dhcpv6-client samba ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
[root@controller /]#

  

 /etc/samba/smb.conf的常见设置

1
2
3
4
5
6
7
8
9
[share]
        comment = share directory
        path = /samba-share
        public = yes
        browseable = yes
        write list = q1
        valid users = q1,q2
        hosts allow = 192.168.10.
        interfaces = 192.168.10.110/24 192.168.20.110/24

 

就是已经挂载了samba的共享文件,然后切换到别的用户使用samba用户的去访问这个共享文件夹(具有使用哪个用户去访问的权限)

切换到普通用户,使用samba服务的账号去登录服务器,创建的文件夹或者文件的属主和属组都为都是那个登陆samba服务的账号
这个就是原理
必须是普通用户使用cifscreds命令,root用户不行,会都没有权限

就是永久挂载之后,需要使用不同的权限,就可以使用multiuser这个挂载技术
可以临时切换到新的共享用户(无需重新挂载),使用cifscreds命令提交新的身份凭据。切换到之后拥有相对应的权限

 

 

  

  

 

 

 

  

  

  

 

posted @   q_7  阅读(151)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示