samba文件共享

samba文件共享

软件版本
samba4.10.16
节点IP系统功能CPU内存硬盘
node110.80.10.1centos7.9samba4核心8GB20GB

centos7下samba服务器搭建

node1

下载安装samba:

1
2
3
# yum install -y samba
# smbd -V
Version 4.10.16

修改hosts:

1
2
3
# vim /etc/hosts
# 尾行,添加配置
10.80.10.1 node1

创建samba账号,用户testuser,密码123456::

1
2
# useradd testuser
# smbpasswd -a testuser

修改samba配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# cp /etc/samba/smb.conf{,.bak}
# vim /etc/samba/smb.conf
[global]
hosts allow = 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16
workgroup = testuser
server string = Samba Server Version %v
log file = /var/log/samba/log.%m
max log size = 50
security = user
passdb backend = tdbsam
[testuser]
comment = private Directory
path = /home/testuser
valid users = @testuser
browseable = yes
public = no
writable = yes

启动samba,设置开机自启:

1
2
3
# systemctl start nmb smb
# systemctl enable nmb smb
# systemctl status nmb smb

win+r打开运行,打开\\10.80.10.1:

输入用户名和密码:

1
2
用户名:testuser
密码:123456

进入samba:

在testuser文件夹创建测试文件test.txt,内容为test:

在linux上查看:

1
2
3
# cd /home/testuser/
# cat test.txt
test

创建test2.txt测试文件:

1
2
# vim test2.txt
test centos7
1
2
# ls
test2.txt test.txt

samba服务器权限实战

node1

使用root写入的windows无法修改:

1
2
3
4
# ll
total 8
-rw-r--r-- 1 root root 13 12月 7 17:18 test2.txt
-rwxr--r-- 1 testuser testuser 4 12月 7 17:17 test.txt

windows可以打开但无法修改test2.txt:

修改test2.txt文件权限,windows可以修改:

1
2
3
4
# chown testuser:testuser test2.txt
# cat test2.txt
test centos7
test add

修改权限,使用root写入的文件windows无法打开:

1
2
# chown root:root test2.txt 
# chmod -r test2.txt

添加读权限:

1
# chmod +r test2.txt 

windows新增的文件默认是用户权限。linux上非用户权限的文件可能会无权限修改。

samba服务器读写用户区分

node1

创建samba读用户,密码123456:

1
2
# useradd read
# smbpasswd -a read

修改samba配置文件,添加read用户:

1
2
3
4
5
6
7
8
9
10
# vim /etc/samba/smb.conf
# 修改testuser部分
[testuser]
comment = private Directory
path = /home/testuser
valid users = @testuser,read
browseable = yes
public = no
writable = no
write list = @testuser
1
# systemctl restart nmb smb

cmd命令清除与samba的连接,找到对应连接,然后使用以下命令删除:

1
2
> net use
> net use \\10.80.10.1\IPC$ /del

重启samba:

1
2
# systemctl restart nmb smb
# systemctl status nmb smb

修改testuser家目录权限:

1
# chmod 755 /home/testuser/

win+r打开运行,打开\\10.80.10.1,使用read登录:

1
2
用户名:read
密码:123456

进入samba:

有读权限,没有写权限:

手动添加权限后samba也无法写入,但是linux可以使用read用户写入。