Samba简介
在本教程中,将展示如何在CentOS 7上安装Samba并将其配置为独立服务器,以通过网络在不同操作系统之间提供文件共享。
环境准备
属性
Samba服务端
Samba客户端
节点
wencheng-smb
windows10
系统
CentOS Linux release 7.5.1804 (Minimal)
windows10 企业版
内核
3.10.0-862.el7.x86_64
windows10 企业版2019 x64
IP地址
172.16.70.185
172.16.70.50
SELinux
setenforce 0 | disabled
检查安装Samba。
[root@wencheng-smb ~]# rpm -qa | grep samba
[root@wencheng-smb ~]# yum install -y samba
[root@wencheng-smb ~]# smbd -V
Version 4.10.16
设置防火墙允许Samba策略。
[root@wencheng-smb ~]# firewall-cmd --add-service samba --permanent
[root@wencheng-smb ~]# firewall-cmd --zone=public --add-port=137/udp --permanent
[root@wencheng-smb ~]# firewall-cmd --zone=public --add-port=138/udpp --permanent
[root@wencheng-smb ~]# firewall-cmd --zone=public --add-port=139/tcp --permanent
[root@wencheng-smb ~]# firewall-cmd --zone=public --add-port=445/tcp --permanent
[root@wencheng-smb ~]# firewall-cmd --reload
[root@wencheng-smb ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client samba
ports: 139/tcp 137/tcp 138/tcp 445/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
情景一:配置匿名访问, 任何人都可以访问的共享目录(允许上传、删除、修改文件)。
[root@wencheng-smb ~]# mv /etc/samba/smb.conf /etc/samba/smb.conf_bak
[root@wencheng-smb ~]# vim /etc/samba/smb.conf
[global]
workgroup = SAMBA # 设定Samba Server所要加入的工作组或者域
security = user # 安全性等级share<user<domain
map to guest = Bad User # 允许匿名用户访问
log file = /var/log/samba/log.%m # 日志文件路径,宏%m表示客户端主机的Netbios名
max log size = 50 # 每个log日志的最大容量为50K ,单位是kb ,0代表不做限制
display charset = UTF8
[public]
comment = Share Directories # 共享的描述
path = /data/samba/shares/public # 共享目录的路径
public = yes # 开启共享
writeable = yes # 开启写权限,默认只读权限
create mask = 0640
directory mask = 0755
[root@wencheng-smb ~]# testparm # 测试smb.conf语法的正确性
Load smb config files from /etc/samba/smb.conf
Unknown parameter encountered: "max log siza"
Ignoring unknown parameter "max log siza"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions # 直接回车
[global]
log file = /var/log/samba/log.%m
map to guest = Bad User
max log size = 50
security = USER
workgroup = SAMBA
idmap config * : backend = tdb
[public]
comment = Public Directories
create mask = 0640
writeable = yes
path = /data/samba/shares/public
read only = No
[root@wencheng-smb ~]# mkdir -p /data/samba/shares/public
[root@wencheng-smb ~]# mkdir /data/samba/shares/public/public{1..3}
[root@wencheng-smb ~]# touch /data/samba/shares/public/public{4..6}
[root@wencheng-smb ~]# ls /data/samba/shares/public/
public1 public2 public3 public4 public5 public6
[root@wencheng-smb ~]# chown nobody.nobody /data/samba/shares/public/
[root@wencheng-smb ~]# ls -ld /data/samba/shares/public/
drwxr-xr-x. 5 nobody nobody 96 May 25 16:07 /data/samba/shares/public/
[root@wencheng-smb ~]# systemctl start smb.service nmb.service
[root@wencheng-smb ~]# systemctl enable smb.service nmb.service
[root@wencheng-smb ~]# netstat -nutpl | grep -E 'smbd|nmbd'
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 130587/smbd
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 130587/smbd
tcp6 0 0 :::445 :::* LISTEN 130587/smbd
tcp6 0 0 :::139 :::* LISTEN 130587/smbd
udp 0 0 172.16.70.255:137 0.0.0.0:* 130591/nmbd
udp 0 0 172.16.70.185:137 0.0.0.0:* 130591/nmbd
udp 0 0 0.0.0.0:137 0.0.0.0:* 130591/nmbd
udp 0 0 172.16.70.255:138 0.0.0.0:* 130591/nmbd
udp 0 0 172.16.70.185:138 0.0.0.0:* 130591/nmbd
udp 0 0 0.0.0.0:138 0.0.0.0:* 130591/nmbd
Win10客户端测试连接。
Win + R 组合键,调出运行窗口 。
输入Samba服务器IP。
再次按 Win + R 组合键,调出运行窗口,并输入:gpedit.msc 命令,确定或回车,可以快速打开本地组策略编辑器。
本地组策略编辑器窗口中,依次展开到:计算机配置 - 管理模板 - 网络 - Lanman 工作站,找到并双击打开“启用不安全的来宾登录”选项,并启用。
再次访问Samba服务器IP。
可执行写入,删除操作。
情景二:配置用户可以访问多个部门的共享目录。
[root@wencheng-smb samba]# cat smb.conf
[global]
workgroup = SAMBA
server string = WenCheng Samba Server Version %v
netbios name = WenChengSamba
security = user
log file = /var/log/samba/log.%m
max log size = 50
map to guest = Bad User
display charset = UTF8
[public]
comment = Public Directories
path = /data/samba/shares/public
public = yes
writeable = no # 关闭匿名用户写权限
create mask = 0644
directory mask = 0755
write list = samba # 只允许samba管理员用户写
[devops]
comment = Devops Directories
path = /data/samba/shares/devops
public = no
writable = yes
create mask = 0640
directory mask = 0750
admin users = devops
valid users = @devops,samba
write list = @devops,samba
[market]
comment = Market Directories
path = /data/samba/shares/market
public = no
writable = yes
create make = 0640
directory mask = 0750
admin users = market
valid users = @market,samba
write list = @market,samba
共享目录说明:
public :所有匿名用户只可读,samba管理员用户可读可写
devops : 只允许@devops,@samba组用户可读可写
market : 只允许@devops,@samba组用户可读可写
[root@wencheng-smb samba]# testparm
Load smb config files from /etc/samba/smb.conf
Unknown parameter encountered: "display charset"
Ignoring unknown parameter "display charset"
Unknown parameter encountered: "create make"
Ignoring unknown parameter "create make"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
[global]
log file = /var/log/samba/log.%m
map to guest = Bad User
max log size = 50
netbios name = WENCHENGSAMBA
security = USER
server string = WenCheng Samba Server Version %v
workgroup = SAMBA
idmap config * : backend = tdb
[public]
comment = Public Directories
create mask = 0640
guest ok = Yes
path = /data/samba/shares/public
write list = samba
[devops]
admin users = devops
comment = Devops Directories
create mask = 0644
path = /data/samba/shares/devops
read only = No
valid users = @devops @samba
write list = @devops @samba
[market]
admin users = market
comment = Market Directories
path = /data/samba/shares/market
read only = No
valid users = @market @samb
[root@wencheng-smb ~]# systemctl restart smb nmb
[root@wencheng-smb ~]# useradd -M -s /sbin/nologin samba # Samba管理员
[root@wencheng-smb ~]# useradd -M -s /sbin/nologin devops # devops部门管理员
[root@wencheng-smb ~]# useradd -M -s /sbin/nologin market # market部门管理员
[root@wencheng-smb ~]# useradd -M -s /sbin/nologin -g devops user1 # devops部门用户
[root@wencheng-smb ~]# useradd -M -s /sbin/nologin -g market user2 # market部门用户
[root@wencheng-smb ~]# id samba
uid=1000(samba) gid=1000(samba) groups=1000(samba)
[root@wencheng-smb ~]# id devops
uid=1001(devops) gid=1001(devops) groups=1001(devops)
[root@wencheng-smb ~]# id market
uid=1002(market) gid=1002(market) groups=1002(market)
[root@wencheng-smb ~]# id user1
uid=1003(user1) gid=1001(devops) groups=1001(devops)
[root@wencheng-smb ~]# id user2
uid=1004(user2) gid=1002(market) groups=1002(market)
[root@wencheng-smb ~]# mkdir -p /data/samba/shares/{public,devops,market}
[root@wencheng-smb ~]# mkdir /data/samba/shares/public/public{1..3}
[root@wencheng-smb ~]# mkdir /data/samba/shares/devops/devops{1..3}
[root@wencheng-smb ~]# mkdir /data/samba/shares/market/market{1..3}
[root@wencheng-smb ~]# touch /data/samba/shares/market/market{4..6}
[root@wencheng-smb ~]# touch /data/samba/shares/devops/devops{4..6}
[root@wencheng-smb ~]# touch /data/samba/shares/public/public{4..6}
[root@wencheng-smb ~]# ls /data/samba/shares/*
/data/samba/shares/devops:
devops1 devops2 devops3 devops4 devops5 devops6
/data/samba/shares/market:
market1 market2 market3 market4 market5 market6
/data/samba/shares/public:
public1 public2 public3 public4 public5 public6
[root@wencheng-smb ~]# chown samba.samba /data/samba/shares/
[root@wencheng-smb ~]# chown devops.devops /data/samba/shares/devops/
[root@wencheng-smb ~]# chmod 775 /data/samba/shares/devops/
[root@wencheng-smb ~]# chown market.market /data/samba/shares/market/
[root@wencheng-smb ~]# chmod 775 /data/samba/shares/market/
[root@wencheng-smb ~]# chown nobody.nobody /data/samba/shares/public/
[root@wencheng-smb ~]# chmod 775 /data/samba/shares/public/
[root@wencheng-smb ~]# setfacl -m u:samba:rwx /data/samba/shares/*
[[root@wencheng-smb ~]# ls -ld /data/samba/shares/
drwxr-xr-x. 5 samba samba 48 May 28 10:09 /data/samba/shares/
[root@wencheng-smb ~]# ls -ld /data/samba/shares/*
drwxrwxr-x+ 5 devops devops 143 May 28 17:02 /data/samba/shares/devops
drwxrwxr-x+ 5 market market 143 May 28 17:02 /data/samba/shares/market
drwxrwxr-x+ 5 nobody nobody 96 May 28 10:06 /data/samba/shares/public
[root@wencheng-smb ~]# getfacl /data/samba/shares/*
getfacl: Removing leading '/' from absolute path names
user::rwx
user:samba:rwx
group::rwx
mask::rwx
other::r-x
user::rwx
user:samba:rwx
group::rwx
mask::rwx
other::r-x
user::rwx
user:samba:rwx
group::rwx
mask::rwx
other::r-x
将现有的用户添加到组。
usermod [-G] [GroupName] [UserName]
将现有的用户添加到多个组
usermod [-G] [GroupName1,GroupName2] [UserName]
将现有用户从组中移除。
gpasswd [-d] [UserName] [GroupName]
将现有多个用户添加到组。
gpasswd [-M] [UserName1,UserName2] [GroupName]
pdbedit命令解析。
pdbedit 命令用于管理Samba服务的帐户信息数据库,格式为:"pdbedit [选项] 帐户"
第一次把用户信息写入到数据库时需要使用-a参数,以后修改用户密码、删除用户等等操作就不再需要了。
pdbedit -L :查看samba用户
pdbedit -a -u user:添加samba用户
pdbedit -r -u user:修改samba用户信息
pdbedit -x -u user:删除samba用户
samba服务数据库的密码也可以用 smbpasswd 命令 操作
smbpasswd -a user:添加一个samba用户
smbpasswd -d user:禁用一个samba用户
smbpasswd -e user:恢复一个samba用户
smbpasswd -x user:删除一个samba用户
[root@wencheng-smb ~]# pdbedit -a -u samba
Unknown parameter encountered: "display charset"
Ignoring unknown parameter "display charset"
new password: # 登录samba密码
retype new password: # 再次确认输入
Unix username: samba
NT username:
Account Flags: [U ]
User SID: S-1-5-21-2219792021-1567261761-1612575661-1000
Primary Group SID: S-1-5-21-2219792021-1567261761-1612575661-513
Full Name:
Home Directory: \\wenchengsamba\samba
HomeDir Drive:
Logon Script:
Profile Path: \\wenchengsamba\samba\profile
Domain: WENCHENGSAMBA
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 23:06:39 CST
Kickoff time: Wed, 06 Feb 2036 23:06:39 CST
Password last set: Fri, 28 May 2021 12:24:14 CST
Password can change: Fri, 28 May 2021 12:24:14 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@wencheng-smb ~]# pdbedit -L # 查看samba用户
Unknown parameter encountered: "display charset"
Ignoring unknown parameter "display charset"
samba:1000:
devops:1001:
market:1002:
user1:1003:
user2:1004:
Win10测试连接Samba共享目录。
“public”文件夹允许所有用户可直接打开,但无法上传,删除操作。
访问“devops”文件夹需账号密码。
Win10本机访问samba时用切换另一个用户登录。
方法如下:
1)按键ctrl+r,打开"运行",输入"cmd"
2)输入命令"net use * /delete",接着输入"Y",即先取消所有的net 连接
“devops”文件夹只允许devops用户及其组的用户(user1),samba管理员上传,删除操作。
“market”文件夹只允许market用户及其组的用户(user2),samba管理员上传,删除操作。
“pbulic”文件夹只允许samba管理员上传,删除操作。
至此,已实现Samba + Windows 文件共享。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章