Samba服务器配置(实例解析)

Samba服务器配置(实例解析)

一、在rhel 8 上配置samba共享服务器

① 查看samba包信息

[root@localhost ~]# yum info samba				#查看samba包信息
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:46:48 ago on Mon 21 Dec 2020 12:31:15 AM EST.
Available Packages
Name         : samba
Version      : 4.12.3
Release      : 12.el8.3
Architecture : x86_64
Size         : 840 k
Source       : samba-4.12.3-12.el8.3.src.rpm
Repository   : base
Summary      : Server and Client software to interoperate with Windows machines
URL          : https://www.samba.org
License      : GPLv3+ and LGPLv3+
Description  : Samba is the standard Windows interoperability suite of programs for Linux
             : and Unix.

② 安装Samba服务 设置开机自启

[root@localhost ~]# yum install -y samba 	   #安装samba服务
[root@localhost ~]# rpm -qa | grep samba	   #查看samba包
samba-client-libs-4.12.3-12.el8.3.x86_64
samba-common-4.12.3-12.el8.3.noarch
samba-libs-4.12.3-12.el8.3.x86_64
samba-common-tools-4.12.3-12.el8.3.x86_64
samba-4.12.3-12.el8.3.x86_64
samba-common-libs-4.12.3-12.el8.3.x86_64

[root@localhost ~]# systemctl start smb       	#启动samba服务
[root@localhost ~]# systemctl enable smb		#设置开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service /usr/lib/systemd/system/smb.service.
[root@localhost ~]# systemctl status smb		#查看服务正在运行
● smb.service - Samba SMB Daemon
   Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2020-12-21 01:22:27 EST; 21s ago
     Docs: man:smbd(8)
           man:samba(7)
           man:smb.conf(5)
 Main PID: 33908 (smbd)
   Status: "smbd: ready to serve connections..."
    Tasks: 4 (limit: 12180)
   Memory: 13.7M
   CGroup: /system.slice/smb.service
           ├─33908 /usr/sbin/smbd --foreground --no-process-group
           ├─33910 /usr/sbin/smbd --foreground --no-process-group
           ├─33911 /usr/sbin/smbd --foreground --no-process-group
           └─33912 /usr/sbin/smbd --foreground --no-process-group

Samba服务器配置(实例解析)

③ 建立测试共享目录添加相应的用户和组,添加对应的samba账号

[root@localhost ~]# mkdir /textshare											
[root@localhost ~]# mkdir /textshare/share						  #添加共享文件夹
[root@localhost ~]# touch /textshare/share/text_share.tar		  #添加共享测试文件

[root@localhost ~]# groupadd share							#添加用户组 share
[root@localhost ~]# useradd -g share share1					#添加 用户share1 和share2
[root@localhost ~]# useradd -g share share2
[root@localhost ~]# passwd share1					        # 给用户share1与share2 设立密码
Changing password for user share1.
New password: 												# passwd: 111111  (测试用)
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost ~]# passwd share2
Changing password for user share2.
New password: 												# passwd: 111111  (测试用)	
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@localhost ~]# smbpasswd -a share1						# 建立于系统中同名的samba账号
New SMB password:											# passwd: 111111  (测试用)
Retype new SMB password:
Added user share1.
[root@localhost ~]# smbpasswd -a share2										
New SMB password:											# passwd: 111111  (测试用)
Retype new SMB password:
Added user share2.

④ 修改smb.conf

[root@localhost ~]# vim /etc/samba/smb.conf
[share]										# 定义共享文件 目录要求
	comment = share						    # 注释说明 文件名为share
	path = /textshare/share					# 分享文件的路径(绝对路径地址)
	writable = yes							# 是否可以读写
	browseable = yes						# 指定该共享是否可以浏览 
	valid users = @share					# 允许访问该共享的用户 @share
											# invalid users = 禁止访问该共享的用户

Samba服务器配置(实例解析)

comment 注释说明
path 分享资源的完整路径名称,除了路径要正确外,目录的权限也要设对(绝对路径)
browseable 是yes/否no在浏览资源中显示共享目录,若为否则必须指定共享路径才能存取
printable 是yes/否no允许打印
hide dot ftles 是yes/否no隐藏隐藏文件
public 是yes/否no 公开共享,若为否则进行身份验证(只有当security = share 时此项才起作用)
guest ok 是yes/否no 公开共享,若为否则进行身份验证(只有当security = share 时此项才起作用)
read only 是yes/否no 以只读方式共享当与writable发生冲突时也writable为准
writable 是yes/否no可写 不以只读方式共享 当与read only发生冲突时,无视read only
vaild users 设定只有此名单内的用户才能访问共享资源(拒绝优先)(用户名/@组名)
invalid users 设定只有此名单内的用户不能访问共享资源(拒绝优先)(用户名/@组名)
read list 设定此名单内的成员为只读(用户名/@组名)
write list 若设定为只读时,则只有此设定的名单内的成员才可作写入动作(用户名/@组名)
create mask 建立文件时所给的权限
directory mask 建立目录时所给的权限
force group 指定存取资源时须以此设定的群组使用者进入才能存取(用户名/@组名)
force user 指定存取资源时须以此设定的使用者进入才能存取(用户名/@组名)
allow hosts 设定只有此网段/IP的用户才能访问共享资源
allwo hosts 网段 except IP
deny hosts 设定只有此网段/IP的用户不能访问共享资源
allow hosts 本网段指定IP指定IP
deny hosts 指定IP本网段指定IP

⑤ 修改共享目录本地系统权限

[root@localhost textshare]# chmod 777 /textshare/share/ -R
[root@localhost textshare]# chown share1:share /textshare/share/ -R
[root@localhost textshare]# chown share2:share /textshare/share/ -R
[root@localhost textshare]# ll
total 0
drwxrwxrwx. 2 share2 share 28 Dec 21 01:32 share

⑥ 修改context值 放行防火墙

[root@localhost textshare]# chcon -t samba_share_t /textshare/share/ -R
[root@localhost textshare]# firewall-cmd --permanent --add-service=samba
success
[root@localhost textshare]# firewall-cmd --reload  						#重新加载防火墙
success
[root@localhost textshare]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client samba ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@localhost textshare]# systemctl restart smb nmb --now       	     #重启服务

二、linux测试连接

①安装samba服务端 和支持文件共享服务的软件包(cifs-utils)

  yum install samba-client -y
  yum install cifs-utils.x86_64 -y

②使用smbclient 访问服务器

Ⅰ、测试

Samba服务器配置(实例解析)

Ⅱ、连接

[root@localhost ~]# smbclient //192.168.98.128/share -U share2%111111
Try "help" to get a list of possible commands.
smb: \> ls
  .                                   D        0  Mon Dec 21 03:04:30 2020
  ..                                  D        0  Mon Dec 21 01:58:02 2020
  text_share.tar                      N        0  Mon Dec 21 01:32:51 2020
  nmb.txt                             N        0  Mon Dec 21 01:58:12 2020
  123.txt                             A        0  Mon Dec 21 03:04:28 2020

		17811456 blocks of size 1024. 13365092 blocks available
smb: \> mkdir testdir					## 新建目录测试
smb: \> ls								## 新建成功
  .                                   D        0  Mon Dec 21 03:26:46 2020
  ..                                  D        0  Mon Dec 21 01:58:02 2020
  text_share.tar                      N        0  Mon Dec 21 01:32:51 2020
  nmb.txt                             N        0  Mon Dec 21 01:58:12 2020
  123.txt                             A        0  Mon Dec 21 03:04:28 2020
  testdir                             D        0  Mon Dec 21 03:26:46 2020

		17811456 blocks of size 1024. 13365092 blocks available
smb: \> exit

③ Linux端 挂载共享目录

[root@localhost ~]# mkdir -p /mnt/smb_share
[root@localhost ~]# mount -t cifs //192.168.98.128/share /mnt/smb_share/ -o username=share1
Password for share1@//192.168.98.128/share:  ******
[root@localhost ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
devtmpfs                952M     0  952M   0% /dev
tmpfs                   981M     0  981M   0% /dev/shm
tmpfs                   981M   18M  963M   2% /run
tmpfs                   981M     0  981M   0% /sys/fs/cgroup
/dev/mapper/rhel-root    17G  4.3G   13G  26% /
/dev/sda1              1014M  210M  805M  21% /boot
tmpfs                   196M  1.2M  195M   1% /run/user/42
tmpfs                   196M  4.6M  192M   3% /run/user/0
//192.168.98.128/share   17G  4.3G   13G  25% /mnt/smb_share
上一篇:Gym 103306J John in the Amusement Park(dp)


下一篇:【题解】[Codeforces Gym 101234 I] Tree Game | 20210930 模拟赛 树(tree)【贪心】