samba简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成。SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源。

samba监听端口:

TCP UDP
139 137
445 138

tcp端口对应的服务是smbd服务,其作用是提供对服务器中文件,打印资源的共享访问。 udp端口对应的服务是nmbd服务,其作用是提供基于NetBOIS主机名称的解析。

进程 对应
nmbd 对应netbois
smbd 对应cifs协议

Samba 服务配置

服务端ip 客户端ip
192.168.47.11 192.168.47.12

关闭防火墙 [root@yanyinglai ~]# systemctl stop firewalld [root@yanyinglai ~]# setenforce 0

安装Samba服务程序 [root@yanyinglai ~]# yum install samba* 已加载插件:product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. 启动Samba服务程序 [root@yanyinglai ~]# systemctl start smb 设置Samba服务随系统启动而启动 [root@yanyinglai ~]# systemctl enable smb

创建映射共享目录

创建用户dd [root@yanyinglai ~]# useradd -M dd 为dd用户创建smb共享密码 [root@yanyinglai ~]# smbpasswd -a dd New SMB password: Retype new SMB password: Added user dd. 假设这里映射dd用户为share用户,那么就要在/etc/samba/smbusers文件中添加内容

[root@yanyinglai ~]# echo 'dd = share' > /etc/samba/smbusers [root@yanyinglai ~]# 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 username map = /etc/samba/smbusers ** // 添加此行内容** passdb backend = tdbsam

创建一个共享目录yan [root@yanyinglai ~]# mkdir /opt/yan [root@yanyinglai ~]# chown -R dd.dd /opt/yan/ [root@yanyinglai ~]# ll /opt/ 总用量 0 drwxr-xr-x. 2 dd dd 6 8月 7 19:30 yan drwxr-xr-x. 9 root root 254 7月 16 15:51 yanyinglai

配置共享

[root@yanyinglai ~]# cat >> /etc/samba/smb.conf <<EOF [yan] 共享名 comment = this is 描述信息,任意字符串 path = /opt/yan/ 共享目录路径

browseable = yes 指定该共享是否可以浏览

guest ok = yes 表示设置是否所有人均可访问共享目录 writable = yes 指定该共享路径是否可写 write list = share 表示设置允许写的用户和组

public = yes 表示设置是否允许匿名用户访问

EOF [root@yanyinglai ~]# tail -8 /etc/samba/smb.conf [yan] comment = this is path = /opt/yan/ browseable = yes guest ok = yes writable = yes write list = share public = yes

testparm 测试配置文件是否有语法错误,以及显示最终生效的配置

[root@yanyinglai ~]# testparm Load smb config files from /etc/samba/smb.conf rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384) Processing section "[homes]" Processing section "[printers]" Processing section "[print$]" Processing section "[yan]" Loaded services file OK. Server role: ROLE_STANDALONE

Press enter to see a dump of your service definitions 重新启动smb服务 重新启动smb服务 [root@yanyinglai ~]# systemctl restart smb 重新加载smb服务 [root@yanyinglai ~]# systemctl reload smb

在客户机查看samba服务器有哪些共享资源 yum查找smbclient软件包的绝对路径 [root@yanyinglai ~]# yum provides *bin/smbclient

[root@yanyinglai ~]# yum install /usr/bin/smbclient -y 在客户端查看samba服务器有哪些共享资源 [root@yanyinglai3 ~]# smbclient -L 192.168.47.11 -U share Enter SAMBA\share's password:

Sharename       Type      Comment
---------       ----      -------
print$          Disk      Printer Drivers
yan             Disk      this is
IPC$            IPC       IPC Service (Samba 4.6.2)
dd              Disk      Home Directories

Reconnecting with SMB1 for workgroup listing.

Server               Comment
---------            -------

Workgroup            Master
---------            -------

将samba服务器的共享资源yan挂载到客户机本地

[root@yanyinglai3 ~]# mkdir /opt/smb [root@yanyinglai3 ~]# mount -t cifs //192.168.47.11/yan /opt/smb -o username=share,password=123 [root@yanyinglai3 ~]# df-h -bash: df-h: 未找到命令 [root@yanyinglai3 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 17G 1.4G 16G 8% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.6M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 //192.168.47.11/yan 47G 5.0G 42G 11% /opt/smb 验证 在客户端上进入共享目录创建新文件 [root@yanyinglai3 ~]# cd /opt/smb [root@yanyinglai3 smb]# touch abc [root@yanyinglai3 smb]# mkdir dfc [root@yanyinglai3 smb]# ls abc dfc 在服务端查看共享的目录里面是否存在客户端创建的文件和目录 [root@yanyinglai ~]# cd /opt/yan/ [root@yanyinglai yan]# ls abc dfc


匿名共享

服务器ip 客户端ip
192.168.47.11 192.168.47.12

配置匿名共享时,还是需要关闭防火墙

[root@yanyinglai ~]# systemctl stop firewalld [root@yanyinglai ~]# setenforce 0 使用yum命令安装samba服务器 [root@yanyinglai ~]# yum install samba* [root@yanyinglai ~]# 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

创建一个共享目录,创建目录名为yan

[root@yanyinglai ~]# mkdir /opt/yan [root@yanyinglai ~]# chmod 777 /opt/yan/ [root@yanyinglai ~]# ll /opt/ 总用量 0 drwxrwxrwx. 2 root root 6 8月 7 20:28 yan drwxr-xr-x. 9 root root 254 7月 16 15:51 yanyinglai 配置共享 [root@yanyinglai ~]# cat >> /etc/samba/smb.conf <<EOF [yan] comment = yan path = /opt/yan browseable = yes guest ok = yes writable = yes public = yes EOF 启动smb服务: [root@yanyinglai ~]# systemctl start smb [root@yanyinglai ~]# systemctl restart smb 在客户端查看samba服务器有哪些共享资源 [root@yanyinglai3 ~]# smbclient -L192.168.47.11 -U 'Bad User' Enter SAMBA\Bad User's password:

Sharename       Type      Comment
---------       ----      -------
print$          Disk      Printer Drivers
yan             Disk      yan
IPC$            IPC       IPC Service (Samba 4.6.2)

Reconnecting with SMB1 for workgroup listing.

Server               Comment
---------            -------

Workgroup            Master
---------            -------

将samba服务器的共享资源yan挂载到客户机本地

[root@yanyinglai3 ~]# mkdir /opt/smb [root@yanyinglai3 ~]# mount -t cifs //192.168.47.11/yan /opt/smb -o username='Bad User' [root@yanyinglai3 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/mapper/centos-root 17G 1.4G 16G 8% / devtmpfs 478M 0 478M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.7M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 1014M 125M 890M 13% /boot tmpfs 98M 0 98M 0% /run/user/0 //192.168.47.11/yan 47G 5.0G 42G 11% /opt/smb 在客户端上进入共享目录创建文件或目录验证一下,并在服务器上查看客户端创建的文件 客户端 [root@yanyinglai3 ~]# cd /opt/smb [root@yanyinglai3 smb]# touch qwer [root@yanyinglai3 smb]# mkdir qazs 服务器 [root@yanyinglai ~]# cd /opt/yan/ [root@yanyinglai yan]# ls qazs qwer