版权声明:欢迎转载,转载请注明出处! https://blog.csdn.net/miss1181248983/article/details/90751050
安装Samba
匿名共享
匿名共享无需使用用户名、密码登录Samba服务器
- 关闭selinux:
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config && setenforce 0
- 安装:
yum install -y samba samba-common samba-client
- 创建共享目录:
mkdir -p /samba/anonymous
chmod 777 /samba/anonymous
chown -R nobody:nobody /samba/anonymous
- 修改配置:
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
vim /etc/samba/smb.conf
[global]
workgroup = workgroup
server string = Samba Server %v
netbios name = test1
security = user #用户需要认证才能访问共享资源
map to guest = bad user #将匿名用户映射为nobody用户
dns proxy = no
[anonymous]
comment = This is a directory of anonymous. #注释说明
path = /samba/anonymous #共享路径
browseable = yes #可浏览
writeable = yes #可写
guest ok = yes #允许guest访问
read only = no #非只读
- 启动服务:
systemctl enable smb && systemctl start smb
systemctl enable nmb && systemctl start nmb
组成Samba运行的有两个服务,一个是SMB,另一个是NMB。SMB是Samba 的核心启动服务,主要负责建立 Linux Samba服务器与Samba客户机之间的对话, 验证用户身份并提供对文件和打印系统的访问,只有SMB服务启动,才能实现文件的共享,监听139 TCP端口;而NMB服务是负责解析用的,类似与DNS实现的功能,NMB可以把Linux系统共享的工作组名称与其IP对应起来,如果NMB服务没有启动,就只能通过IP来访问共享文件,监听137和138 UDP端口。
- firewalld放行samba:
firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload
如果没有关闭selinux,可以这样
chcon -t samba_share_t /samba/anonymous/
- Windows客户端访问:
Windows上hosts文件绑定samba服务器ip和主机名,例如192.168.30.128 test1
。
echo "This is a directory of anonymous." >> /samba/anonymous/README.txt
非匿名共享
非匿名共享需要使用用户名、密码登录Samba服务器
- 创建用户及组:
groupadd smbgrp
useradd -s /sbin/nologin smbusr
usermod -a smbusr -G smbgrp #将用户smbusr加入用户组smbgrp
smbpasswd -a smbusr #smaba添加用户smbusr并设置密码
New SMB password: #123456
Retype new SMB password:
Added user smbusr.
smbpasswd命令说明:
-a 添加
-x 删除
-d 禁用
-e 启用
- 创建共享目录:
mkdir /samba/security
chmod 777 /samba/security
chown -R smbusr:smbgrp /samba/security
- 修改配置:
vim /etc/samba/smb.conf #添加
[security]
path = /samba/security
valid users = @smbgrp #有效用户为smbgrp用户组
guest ok = no #拒绝guest访问
writeable = yes #可写
browseable = yes #可浏览
- 重启服务:
systemctl restart smb && systemctl restart nmb
- Windows客户端访问:
echo "This is a directory of security." >> /samba/security/README.txt
- Linux客户端访问:
yum install -y samba-client
smbclient -L 192.168.30.128 -U smbusr%123456
Sharename Type Comment
--------- ---- -------
anonymous Disk This is a directory of anonymous.
security Disk
IPC$ IPC IPC Service (Samba Server 4.8.3)
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
WORKGROUP TEST1
smbclient //samba服务器ip(主机名)/共享目录 -U 用户名%密码
smbclient //test1/security -U smbusr%123456
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Fri May 31 13:41:26 2019
.. D 0 Fri May 31 13:39:22 2019
README.txt N 33 Fri May 31 13:41:26 2019
18707456 blocks of size 1024. 16347544 blocks available
smb: \> !cat README.txt
This is a directory of security.
linux下samba客户端命令参考这里:在Linux下查看共享文件夹