实验环境:一台samba服务器,一台客户端(均为centos7操作系统)
一.samba服务器设置匿名用户,使用客户端进行访问
首先要让两台虚拟机网络ping通,这里采用自动获取的方式
[root@bogon ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 编辑samba服务器的网卡
[root@bogon ~]# systemctl restart network 重启网络服务
[root@bogon ~]# ifconfig ens33 查看ens33网卡IP
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.22.132 netmask 255.255.255.0 broadcast 192.168.22.255
inet6 fe80::d80:6234:3fc8:976c prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a3:8b:28 txqueuelen 1000 (Ethernet)
RX packets 32 bytes 3888 (3.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 43 bytes 4363 (4.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
切换到客户端同上开启网络服务,是同一网段
网络配置好之后,来安装samba服务,用yum来安装,可忽略依赖关系(这边我们提前搭建好的yum)
[root@bogon ~]# yum -y install samba 用yum安装samba服务
安装好samba服务之后,先创建一个目录/share
[root@bogon ~]# mkdir /share
之后先赋予/share目录最高权限777
[root@bogon /]# chmod 777 share
[root@bogon /]# ll -d share
drwxrwxrwx. 2 root root 6 Jun 18 03:24 share
现在进行编辑samba主配置文件/etc/samba/smb.conf
[root@bogon /]# vim /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = SAMBA
security = user
map to guest = Bad User 新添加的内容
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[homes]
comment = Home Directories
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = root
create mask = 0664
directory mask = 0775
[share1] 自定义名称
comment = share txt 对共享目录的注释、说明信息
path = /share 共享目录在服务器中对应的实际路径
public = yes 允许匿名访问
read only = no 拒绝读的权限
writable = yes 允许写入
启动smb服务
[root@bogon /]# systemctl start smb
[root@bogon /]# systemctl start nmb
[root@bogon /]# systemctl stop firewalld 关闭防火墙
[root@bogon /]# setenforce 0 降低沙盒级别
[root@bogon /]# cd share 切换到/share路径
[root@bogon share]# touch 1.txt 创建文件1.txt,以验证客户端匿名访问可下载文件
[root@bogon share]# ls 查看
1.txt
现在用客户端进行匿名访问
在当前路径下创建2.txt,以便上传
[root@bogon ~]# smbclient //192.168.22.132/share1 匿名访问samba方式
Enter root's password: 无密码
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls 查看内容
. D 0 Fri Jun 18 03:37:45 2021
.. DR 0 Fri Jun 18 03:24:52 2021
1.txt N 0 Fri Jun 18 03:37:45 2021
40137576 blocks of size 1024. 35518264 blocks available
smb: \> put 2.txt 上传当前客户端路径下创建的文件2.txt成功
putting file 2.txt as \2.txt (0.0 kb/s) (average 0.0 kb/s)
smb: \> ls
. D 0 Fri Jun 18 03:43:52 2021
.. DR 0 Fri Jun 18 03:24:52 2021
1.txt N 0 Fri Jun 18 03:37:45 2021
2.txt A 0 Fri Jun 18 03:43:52 2021
40137576 blocks of size 1024. 35518208 blocks available
smb: \> get 1.txt 下载samba服务器的文件1.txt成功
getting file \1.txt of size 0 as 1.txt (0.0 KiloBytes/sec) (average 0.0 KiloBytes/sec
smb: \> q 退出
samba服务匿名用户访问成功
二、samba服务指定用户访问
同上环境一样
[root@localhost ~]# mkdir /xk 创建目录
[root@localhost ~]# chmod 777 /xk 给该目录的权限
[root@localhost ~]# vim /etc/samba/smb.conf 编辑smb主配置文件
添加以下内容
[samba]
comment = samba.txt
path = /xk
public = no
read only = yes
valid users = xkxk,jljl 本地用户的名称
write list = xkxk 拥有读写权限的本地用户
[root@localhost ~]# useradd xkxk 添加xkxk用户
[root@localhost ~]# useradd jljl 添加jljl用户
[root@localhost ~]# passwd xkxk 激活用户
Changing password for user xkxk.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# passwd jljl 激活用户
Changing password for user jljl.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
将两个本地用户添加为共享用户
[root@localhost ~]# pdbedit -a -u xkxk
[root@localhost ~]# pdbedit -a -u jljl
然后开启smb服务
[root@localhost ~]# systemctl start smb
[root@localhost ~]# systemctl start nmb
切换到客户端验证
[root@localhost ~]# smbclient --user=xkxk //192.168.3.129/samba
现在用jljl用户来验证,只能下载不能上传
[root@localhost ~]# touch 3.txt 在当前路径创建3.txt
[root@localhost ~]# smbclient --user=jljl //192.168.3.129/samba
ok 实验完成。