samba
Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。
##实验一:搭建匿名用户共享服务器
主机名 | 类型 | IP地址 |
---|---|---|
aaa | 服务器 | 192.168.101.128/24 |
bbb | 客户机 | 192.168.101.129/24 |
关闭防火墙和selinux
[root@aaa ~]# systemctl stop firewalld.service
[root@aaa ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 三 2018-09-12 10:42:20 CST; 2s ago
Docs: man:firewalld(1)
Process: 805 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
Main PID: 805 (code=exited, status=0/SUCCESS)
[root@aaa ~]# getenforce
Enforcing
[root@aaa ~]# setenforce 0
[root@aaa ~]# getenforce
Permissive
服务器同理(我就不演示了)
安装Samba服务
[root@aaa ~]# yum -y install samba-*
创建共享文件夹
[root@aaa ~]# mkdir /tom
[root@aaa ~]# echo "hello world" > /tom/world
修改smb.conf
[root@aaa ~]# vim /etc/samba/smb.conf
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
map to guest = bad user #添加此项
printing = cups
printcap name = cups
load printers = yes
cups options = raw
valid users = %S, %D%w%S
browseable = No
read only = No
inherit acls = Yes
[share] #共享
comment = this is share file #注释
path = /tom #路径
browseable = yes #共享可见
guest ok = yes #可访问
writable = yes #可写
public = yes #允许匿名访问
重启Samba服务
[root@aaa ~]# systemctl restart smb
客户机验证服务,查看服务器上的共享资源
客户机也要安装samba服务,关闭防火墙
[root@bbb ~]# smbclient -L 192.168.101.128 -U 'bad user'
Enter SAMBA\bad user's password: #不需要填写密码
OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
share Disk this is share file
IPC$ IPC IPC Service (Samba 4.6.2)
OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
服务器共享资源挂载到客户机
[root@bbb ~]# mount -t cifs //192.168.101.128/share /opt/smb -o username='bad user'
[root@bbb ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17811456 1120664 16690792 7% /
devtmpfs 922408 0 922408 0% /dev
tmpfs 933524 0 933524 0% /dev/shm
tmpfs 933524 8812 924712 1% /run
tmpfs 933524 0 933524 0% /sys/fs/cgroup
/dev/sda1 1038336 146016 892320 15% /boot
tmpfs 186708 0 186708 0% /run/user/0
/dev/sr0 3963760 3963760 0 100% /mnt
//192.168.101.128/share 17811456 1124168 16687288 7% /opt/smb
挂载成功
验证实验
[root@bbb ~]# cd /opt/smb
[root@bbb smb]# ls
world
[root@bbb smb]# touch 123
[root@bbb smb]# echo "RNG"> /opt/smb/123
服务器
----------
[root@aaa /]# cd /tom/
[root@aaa tom]# ls
123 world
[root@aaa tom]# cat 123
RNG
实验二:搭建用户认证共享服务器
与实验一的环境一样,关闭防火墙、selinux,安装samba请参照实验一
创建访问用户,设置密码
[root@aaa ~]# useradd ow
[root@aaa ~]# smbpasswd -a ow
New SMB password:
Retype new SMB password:
Added user ow.
更改/opt/ow目录所属用户和所属组
[root@aaa ~]# chown -R ow.ow /opt/ow
[root@aaa ~]# ll -d /opt/ow/
drwxr-xr-x. 2 ow ow 6 9月 12 12:00 /opt/ow/
映射用户
[root@aaa ~]# echo 'ow = lol'> /etc/samba/dnf
[root@aaa ~]# cat /etc/samba/dnf
ow = lol
配置文件
[root@aaa ~]# vim /etc/samba/smb.conf
[global]
workgroup = SAMBA
security = user
username map =/etc/samba/dnf #添加项
passdb backend = tdbsam
map to guest = bad user
printing = cups
printcap name = cups
load printers = yes
cups options = raw
尾行添加
[share2]
comment = this is share file
path = /opt/ow
browseable = yes
guest ok = yes
writable = yes
public = yes
重启samba服务
[root@aaa ~]# systemctl restart smb
客户机查看共享资源
[root@bbb ~]# smbclient -L 192.168.101.128 -U lol
Enter SAMBA\lol's password:
Domain=[AAA] OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
share Disk this is share file
share2 Disk this is share file
IPC$ IPC IPC Service (Samba 4.6.2)
ow Disk Home Directories
Domain=[AAA] OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
客户机挂载
[root@bbb ~]# mount -t cifs //192.168.101.128/ow /opt/cf/ -o username=lol,password=123456
[root@bbb ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 17811456 1122496 16688960 7% /
devtmpfs 922408 0 922408 0% /dev
tmpfs 933524 0 933524 0% /dev/shm
tmpfs 933524 8784 924740 1% /run
tmpfs 933524 0 933524 0% /sys/fs/cgroup
/dev/sda1 1038336 146016 892320 15% /boot
tmpfs 186708 0 186708 0% /run/user/0
//192.168.101.128/ow 17811456 1123212 16688244 7% /opt/cf
验证
[root@bbb ~]# cd /opt/cf/
[root@bbb cf]# touch abc
[root@bbb cf]# echo "omg" > /opt/cf/abc
[root@aaa ~]# cat /opt/ow/abc
omg