1.簡介
Samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,由服務器及客戶端程序構成。SMB(Server Messages Block,信息服務塊)是一種在局域網上共享文件和打印機的一種通信協議,它爲局域網內的不同計算機之間提供文件及打印機等資源的共享服務。SMB協議是客戶機/服務器型協議,客戶機通過該協議可以訪問服務器上的共享文件系統、打印機及其他資源。通過設置“NetBIOS over TCP/IP”使得Samba不但能與局域網絡主機分享資源,還能與全世界的電腦分享資源。
Samba提供了基於CIFS的四個服務:文件和打印服務、授權與被授權、名稱解析、瀏覽服務。前兩項服務由smbd提供,後兩項服務則由nmbd提供。 簡單地說,smbd進程的作用是處理到來的SMB軟件包,爲使用該軟件包的資源與Linux進行協商,nmbd進程使主機(或工作站)能瀏覽Linux服務器。
2.Samba安裝與配置
安裝samba應用
yum -y install samba samba-client
啓動Samba應用
systemctl start smb nmb
查看samba進程
[root@localhost home]# ps -ef | grep -E 'smb|nmb'
root 3284 1 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 3285 1 0 8月13 ? 00:00:02 /usr/sbin/nmbd --foreground --no-process-group
root 3289 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 3290 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 3292 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 11914 3284 0 8月13 ? 00:00:00 /usr/sbin/smbd --foreground --no-process-group
root 19570 2586 0 01:20 pts/0 00:00:00 grep --color=auto -E smb|nmb
查看samba端口
[root@localhost home]# netstat -tulpn
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 3337/smbd
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 3337/smbd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 907/nginx: master p
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1010/sshd
tcp6 0 0 :::445 :::* LISTEN 3337/smbd
tcp6 0 0 :::3306 :::* LISTEN 868/mysqld
tcp6 0 0 :::139 :::* LISTEN 3337/smbd
tcp6 0 0 :::80 :::* LISTEN 907/nginx: master p
udp 0 0 172.18.180.225:123 0.0.0.0:* 515/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 515/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 515/ntpd
udp 0 0 172.18.191.255:137 0.0.0.0:* 3340/nmbd
udp 0 0 172.18.180.225:137 0.0.0.0:* 3340/nmbd
udp 0 0 0.0.0.0:137 0.0.0.0:* 3340/nmbd
udp 0 0 172.18.191.255:138 0.0.0.0:* 3340/nmbd
udp 0 0 172.18.180.225:138 0.0.0.0:* 3340/nmbd
udp 0 0 0.0.0.0:138 0.0.0.0:* 3340/nmbd
udp 0 0 0.0.0.0:30203 0.0.0.0:* 714/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 714/dhclient
udp6 0 0 :::123 :::* 515/ntpd
udp6 0 0 :::50647 :::* 714/dhclient
修改samba的配置文件,以便windows系統能夠直接訪問共享文件,這裏注意,由於阿里雲或者運營商的137,138,139,445端口限制,這裏只做本地的測試,阿里雲的centos7需要做端口轉發(後續討論)
先備份配置文件
[root@localhost samba]# ll
總用量 24
-rw-r--r--. 1 root root 20 4月 13 23:34 lmhosts
-rw-r--r-- 1 root root 827 8月 13 00:17 smb.conf
-rw-r--r--. 1 root root 706 8月 12 19:55 smb.conf.bak
-rw-r--r--. 1 root root 11327 4月 13 23:34 smb.conf.example然後修改配置文件,
原始文件:
[root@localhost samba]# cat smb.conf.bak
# 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
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 = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
修改之後:
[root@localhost samba]# cat 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 = WORKGROUP
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[shared]
# 共享文件目錄描述
comment = Shared Directories
# 共享文件目錄
path = /home/shared/
# 是否允許guest訪問
public = no
# 指定管理用戶
#admin users = admin
# 可訪問的用戶組、用戶
#valid users = @admin
# 是否瀏覽權限
browseable = yes
# 是否可寫權限
writable = yes
# 文件權限設置
create mask = 0777
directory mask = 0777
force directory mode = 0777
force create mode = 0777
path = /home/shared/就是我們的samba共享文件路徑,注意給shared 777權限,
然後adduser shared_smb,再設置samba用戶和密碼,smbpasswd -a shared_smb
root@localhost home]# ll
總用量 4
drwx------. 14 lry lry 4096 8月 12 19:48 lry
drwxrwxrwx. 2 root root 19 8月 12 21:30 shared
drwx------. 3 shared_smb shared_smb 78 8月 12 21:30 shared_smb
[root@localhost home]# pdbedit -L
shared_smb:1001:
smbpasswd用法:
smbpasswd -a:向smbpasswd文件中添加用戶;
-c:指定samba的配置文件;
-x:從smbpasswd文件中刪除用戶;
-d:在smbpasswd文件中禁用指定的用戶;
-e:在smbpasswd文件中激活指定的用戶;
-n:將指定的用戶的密碼置空。
最後在本地win10測試:\\192.168.1.100\shared
查看連接狀態:smbstatus
像FTP客戶端一樣使用smbclient
[root@localhost home]# smbclient //192.168.0.1/tmp -U username%password