Samba 服务学习与基本的共享配置

Samba 服务

Samba简介

Samba是在Linux和Unix上实现SMB协议的一个软件,由服务器和客户端组成。
SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。
SMB是C/S型结构协议。
Samba服务监听的端口有:
TCP:139 和445
UDP:137 和138
端口服务:
tcp端口相对应的服务是smbd服务,其作用是提供对服务器中文件、打印资源的共享访问。
udp端口相对应的服务是nmbd服务,其作用是提供基于NetBIOS主机名称的解析。

Samba进程

进程对应
nmbd对应netbios
smbd对应cifs协议
winbindd + ldap对应Windows AD活动目录

Samba用户

账户密码
都是系统用户 /etc/passwdSamba文件自有密码文件,通过smbpasswd -a username命令设置

smbpasswd命令:
-a 添加系统用户为Samba用户并设置密码
-d 禁用用户账号
-e 启用用户账号
-x 删除用户账号

Samba的配置文件

文件在/etc/samba/smb.conf

三大组成作用
[global]全局配置,此处的设置项对整个samba服务器都有效
[homes]宿主目录共享设置,此处用来设置Linux用户的默认共享,对应用户的宿主目录。当用户访问服务器中与自己用户名同名的共享目录时,通过验证后将会自动映射到该用户的宿主目录中
[printers]打印机共享设置

常用配置文件参数

参数作用
workgroup表示设置工作组名称
server string表示描述samba服务器
security表示设置安全级别,其值可为share、user、server、domain
passdb backend表示设置共享帐户文件的类型,其值可为tdbsam(tdb数据库文件)、ldapsam(LDAP目录认证)、smbpasswd(兼容旧版本samba密码文件)
comment表示设置对应共享目录的注释,说明信息,即文件共享名
browseable表示设置共享是否可见
writable表示设置目录是否可写
path表示共享目录的路径
guest ok表示设置是否所有人均可访问共享目录
public表示设置是否允许匿名用户访问
write list表示设置允许写的用户和组,组要用@表示,例如 write list = root,@root
valid users设置可以访问的用户和组,例如 valid users = root,@root
hosts deny设置拒绝哪台主机访问,例如 hosts deny = 192.168.72.1
hosts allow设置允许哪台主机访问,例如 hosts allow = 192.168.72.2
printable表示设置是否为打印机

实例

配置用户认证共享:

//在服务器上
[root@zlb9 ~]# yum -y install samba-*   //下载软件包
[root@zlb9 ~]# useradd -M p1   //创建系统用户
[root@zlb9 ~]# smbpasswd -a p1   //为用户设置密码
New SMB password:
Retype new SMB password:
Added user p1.

[root@zlb9 ~]# echo 'p1 = share' > /etc/samba/p1user   //把系统用户映射为共享
[root@zlb9 ~]# vim /etc/samba/smb.conf   //在配置文件中添加
[global]
        workgroup = SAMBA
        security = user
        username map = /etc/samba/p1user   //此行为添加的内容
        
//创建共享目录并设置权限        
[root@zlb9 ~]# mkdir /opt/qaz1
[root@zlb9 ~]# chown -R p1.p1 /opt/qaz1/
[root@zlb9 ~]# ll /opt/
total 0
drwxr-xr-x. 2 p1 p1 6 Jan 15 17:26 qaz1
[root@zlb9 ~]# 
//在这里配置共享
[root@zlb9 ~]# vim /etc/samba/smb.conf
[123]
        comment = this is p1
        path = /opt/qaz1
        browseable = yes
        guest ok = yes
        writable = yes
        write list = share
        public = yes
//启动服务并设置开机自启
[root@zlb9 ~]# systemctl enable smb nmb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/nmb.service to /usr/lib/systemd/system/nmb.service.
[root@zlb9 ~]# systemctl restart smb nmb
[root@zlb9 ~]# systemctl reload smb nmb

//在客户机上
[root@zlb10 ~]# yum -y install samba-client   //这个在客户机上必须安装
[root@zlb10 ~]#
//在客户机上查看服务器上有哪些共享服务
[root@zlb10 ~]# smbclient -L 192.168.26.9 -U share
Enter SAMBA\share's password:    //在这里的是要输入密码的
Domain=[ZLB9] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	123             Disk      this is p1
	IPC$            IPC       IPC Service (Samba 4.6.2)
	p1              Disk      Home Directories
Domain=[ZLB9] OS=[Windows 6.1] Server=[Samba 4.6.2]

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

	Workgroup            Master
	---------            -------
	SAMBA                ZLB9
[root@zlb10 ~]# 
//挂载共享目录
[root@zlb10 ~]# mount -t cifs //192.168.26.9/123 /opt/ -o username=share,password=1   
[root@zlb10 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   17G  1.1G   16G   7% /
devtmpfs               982M     0  982M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.6M  984M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1             1014M  125M  890M  13% /boot
tmpfs                  199M     0  199M   0% /run/user/0
/dev/sr0               3.8G  3.8G     0 100% /mnt
//192.168.26.9/123      17G  1.1G   16G   7% /opt
//载客户机上创建文件和目录
[root@zlb10 ~]# cd /opt/
[root@zlb10 opt]# touch test1
[root@zlb10 opt]# mkdir test2
[root@zlb10 opt]# 

//在服务器上可以验证
[root@zlb9 ~]# cd /opt/qaz1/
[root@zlb9 qaz1]# ll
total 0
-rw-r--r--. 1 p1 p1 0 Jan 15 09:55 test1
drwxr-xr-x. 2 p1 p1 6 Jan 15 17:55 test2
[root@zlb9 qaz1]# touch test3
[root@zlb9 qaz1]# ll
total 0
-rw-r--r--. 1 p1   p1   0 Jan 15 09:55 test1
drwxr-xr-x. 2 p1   p1   6 Jan 15 17:55 test2
-rw-r--r--. 1 root root 0 Jan 15 17:57 test3
[root@zlb9 qaz1]# 

配置匿名共享

//在服务端
[root@zlb9 ~]# vim /etc/samba/smb.conf
[global]
        workgroup = SAMBA
        security = user
        map to guest = Bad User
//创建共享目录给其权限      
[root@zlb9 ~]# mkdir /opt/qaz2
[root@zlb9 ~]# chmod 777 /opt/qaz2/
[root@zlb9 ~]# ll /opt/
total 0
drwxrwxrwx. 2 root root 6 Jan 15 18:06 qaz2
[root@zlb9 ~]# 
//配置共享
[root@zlb9 ~]# vim /etc/samba/smb.conf
[123]
        comment = this is niming
        path = /opt/qaz2
        browseable = yes
        guest ok = yes
        writable = yes
        public = yes 
[root@zlb9 ~]# systemctl restart smb nmb   //启动服务
[root@zlb9 ~]# systemctl enable smb nmb
[root@zlb9 ~]# systemctl reload smb nmb
[root@zlb9 ~]# 


//在客户端

[root@zlb10 ~]# smbclient -L 192.168.26.9 -U 'Bad User'   //查看服务器共享的资源
Enter SAMBA\Bad User's password:    //在这里由于是匿名用户,所以不用输入密码
OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	123             Disk      this is niming
	IPC$            IPC       IPC Service (Samba 4.6.2)
OS=[Windows 6.1] Server=[Samba 4.6.2]

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

	Workgroup            Master
	---------            -------
	SAMBA                ZLB9
[root@zlb10 ~]# mount -t cifs //192.168.26.9/123 /opt -o username='Bad User'
[root@zlb10 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   17G  1.1G   16G   7% /
devtmpfs               982M     0  982M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.6M  984M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1             1014M  125M  890M  13% /boot
/dev/sr0               3.8G  3.8G     0 100% /mnt
tmpfs                  199M     0  199M   0% /run/user/0
//192.168.26.9/123      17G  1.1G   16G   7% /opt

//验证
//客户机上
[root@zlb10 ~]# 
[root@zlb10 ~]# cd /opt/
[root@zlb10 opt]# ls
[root@zlb10 opt]# touch test3
[root@zlb10 opt]# mkdir test4
[root@zlb10 opt]# ll
total 0
-rw-r--r--. 1 nobody nobody 0 Jan 15 10:17 test3
drwxr-xr-x. 2 nobody nobody 0 Jan 15  2019 test4
[root@zlb10 opt]# 

//服务端上查看
[root@zlb9 ~]# ll /opt/qaz2/
total 0
-rw-r--r--. 1 nobody nobody 0 Jan 15 10:17 test3
drwxr-xr-x. 2 nobody nobody 6 Jan 15 18:17 test4
[root@zlb9 ~]# touch test5
[root@zlb9 qaz2]# ll
total 0
-rw-r--r--. 1 nobody nobody 0 Jan 15 10:17 test3
drwxr-xr-x. 2 nobody nobody 6 Jan 15 18:17 test4
-rw-r--r--. 1 root   root   0 Jan 15 18:19 test5
[root@zlb9 qaz2]# 

这里的是在Windows上实现共享的截图:
在这里插入图片描述

##题库

[root@zlb9 ~]# vim /etc/samba/smb.conf
[common]
        path = /common/
        browseable = yes
        valid users = natasha

[root@zlb9 ~]# mkdir /common
[root@zlb9 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.26.0/24 service name=samba accept' --permanent
success
[root@zlb9 ~]# firewall-cmd --add-rich-rule 'rule family=ipv4 source address=192.168.26.0/24 service name=samba-client accept' --permanent
success
[root@zlb9 ~]# firewall-cmd --reload
success
[root@zlb9 ~]# 
[root@zlb9 ~]# useradd natasha
[root@zlb9 ~]# smbpasswd -a natasha
New SMB password:
Retype new SMB password:
Added user natasha.
[root@zlb9 ~]# setfacl -m u:natasha:r-x /common/
[root@zlb9 ~]# 
[root@zlb9 ~]# useradd natasha
[root@zlb9 ~]# smbpasswd -a natasha
New SMB password:
Retype new SMB password:
Added user natasha.
[root@zlb9 ~]# setfacl -m u:natasha:r-x /common/
[root@zlb9 ~]# 

[root@zlb9 ~]# setsebool -P samba_enable_home_dirs on
[root@zlb9 ~]# chcon -Rt samba_share_t /common/
[root@zlb9 ~]# systemctl enable smb nmb
[root@zlb9 ~]# systemctl  restart smb nmb
[root@zlb9 ~]# 

//在客户端上验证
[root@zlb10 ~]# mount -t cifs -o username=natasha,password=tangkai //192.168.26.9/common /mnt/
[root@zlb10 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   17G  1.1G   16G   7% /
devtmpfs               982M     0  982M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.6M  984M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1             1014M  125M  890M  13% /boot
tmpfs                  199M     0  199M   0% /run/user/0
//192.168.26.9/common   17G  1.1G   16G   7% /mnt

[root@zlb10 ~]# cd /mnt/
[root@zlb10 mnt]# ls
[root@zlb10 mnt]# touch qwe
touch: cannot touch ‘qwe’: Permission denied
[root@zlb10 mnt]# ls
p3
[root@zlb10 mnt]# 

多用户Samba

[root@zlb9 ~]# mkdir /storage
[root@zlb9 ~]# chcon -Rt samba_share_t /storage/
[root@zlb9 ~]# vim /etc/samba/smb.conf
[share]
        path = /storage/
        browseable = yes
        valid users = Sarah,kitty
        writable = no
        write list = Kitty

[root@zlb9 ~]# useradd Sarah 
[root@zlb9 ~]# useradd Kitty
[root@zlb9 ~]# smbpasswd -a Sarah
New SMB password:
Retype new SMB password:
Added user Sarah.
[root@zlb9 ~]# smbpasswd -a Kitty
New SMB password:
Retype new SMB password:
Added user Kitty.

[root@zlb9 ~]# setfacl -m u:Sarah:r-x /storage/
[root@zlb9 ~]# setfacl -m u:Kitty:rwx /storage/
[root@zlb9 ~]# systemctl restart smb nmb


//在客户机上
[root@zlb10 ~]# mkdir /mnt/dev
[root@zlb10 ~]# vim /etc/fstab
//192.168.26.9/share /mnt/dev cifs multiuser,username=Sarah,password=tangkai,sec=ntlmssp 0 0

[root@zlb10 ~]# mount -a   //在这里挂载的时候注意把之前的/etc/fstab多余的删除
[root@zlb10 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/rhel-root   17G  1.1G   16G   7% /
devtmpfs               982M     0  982M   0% /dev
tmpfs                  993M     0  993M   0% /dev/shm
tmpfs                  993M  8.6M  984M   1% /run
tmpfs                  993M     0  993M   0% /sys/fs/cgroup
/dev/sda1             1014M  125M  890M  13% /boot
tmpfs                  199M     0  199M   0% /run/user/0
//192.168.26.9/share    17G  1.1G   16G   7% /mnt/dev

[root@zlb10 ~]# ls /home/
tom
[root@zlb10 ~]# su - tom
Last login: Tue Jan 15 19:20:27 CST 2019 on pts/0
[tom@zlb10 ~]$ cifscreds add -u Kitty 192.168.26.9
Password: 
[tom@zlb10 ~]$ cd /mnt/dev/
[tom@zlb10 dev]$ ls
[tom@zlb10 dev]$ touch o5
[tom@zlb10 dev]$ ls
o5
[tom@zlb10 dev]$ ll
total 0
-rw-r--r--. 1 1012 1012 0 Jan 15 19:24 o5
//在服务器上可以看到创建的文件同步了
[root@zlb9 ~]# id 1012
uid=1012(Kitty) gid=1012(Kitty) groups=1012(Kitty)
[root@zlb9 ~]# 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值