關於Linux和Windows系統之間的文件傳輸,很多人選擇使用FTP,相對較安全,但是有時還是會出現一些問題,比如上傳文件時,文件名莫名出現亂碼,文件大小改變等問題。相比較來說,使用Samba作為文件共享,就省事簡潔多了。Samba服務器通信協議(Server Messages Block)就是是為了解決局域網內的文件或打印機等資源的共享服務問題,讓多個主機之間共享文件變成越來越簡單。下面簡單介紹下,在Centos7下部署Samba服務的操作記錄(測試機192.168.10.204):
1)安裝Samba
[root@samba-server ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) [root@samba-server ~]# rpm -qa|grep samba [root@samba-server ~]# yum install -y samba
2)安全角度考慮,需要設置防火牆策略(不要關閉防火牆)
添加samba服務到防火牆策略中 [root@samba-server ~]# firewall-cmd --add-service samba --permanent success 重啟防火牆 [root@samba-server ~]# firewall-cmd --reload success 查看samba服務是否添加到防火牆中: [root@samba-server ~]# firewall-cmd --list-all|grep samba services: ssh dhcpv6-client samba 記住:一定要關閉selinux(否則會造成windows客戶機連接Samba失敗) [root@samba-server ~]# vim /etc/sysconfig/selinux ..... SELINUX=disabled [root@samba-server kevin]# setenforce 0 [root@samba-server kevin]# getenforce Permissive
3)配置Samba服務文件
[root@samba-server ~]# cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
[root@samba-server ~]# 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
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
[kevin] //這個是共享文件夾標識,表示登錄samba打開時顯示的文件夾名稱。配置了多少個共享文件夾標識,登錄samba時就會顯示多少文件夾。
comment = please do not modify it all will //comment是對該共享的描述,可以是任意字符串
path= /home/kevin //共享的路徑
writable = yes //是否寫入
public = no //是否公開
4)添加kevin賬號(如上配置中添加的內容)
設置為不予許登入系統,且用戶的家目錄為 /home/kevin(相當於虛擬賬號)的kevin賬號。 [root@samba-server ~]# useradd -d /home/kevin -s /sbin/nologin kevin
5)pdbedit 命令說明
pdbedit 命令用於管理Samba服務的帳戶信息數據庫,格式為:"pdbedit [選項] 帳戶" 第一次把用戶信息寫入到數據庫時需要使用-a參數,以后修改用戶密碼、刪除用戶等等操作就不再需要了。 pdbedit -L :查看samba用戶 pdbedit -a -u user:添加samba用戶 pdbedit -r -u user:修改samba用戶信息 pdbedit -x -u user:刪除samba用戶 samba服務數據庫的密碼也可以用 smbpasswd 命令 操作 smbpasswd -a user:添加一個samba用戶 smbpasswd -d user:禁用一個samba用戶 smbpasswd -e user:恢復一個samba用戶 smbpasswd -x user:刪除一個samba用戶
6)將kevin添加為samba用戶
[root@samba-server ~]# id kevin uid=1001(kevin) gid=1001(kevin) groups=1001(kevin) [root@samba-server ~]# pdbedit -a -u kevin new password: //設置kevin使用的samba賬號密碼,比如123456 retype new password: //確認密碼 Unix username: kevin NT username: Account Flags: [U ] User SID: S-1-5-21-33923925-2092173964-3757452328-1000 Primary Group SID: S-1-5-21-33923925-2092173964-3757452328-513 Full Name: Home Directory: \\samba-server\kevin HomeDir Drive: Logon Script: Profile Path: \\samba-server\kevin\profile Domain: SAMBA-SERVER Account desc: Workstations: Munged dial: Logon time: 0 Logoff time: Wed, 06 Feb 2036 23:06:39 CST Kickoff time: Wed, 06 Feb 2036 23:06:39 CST Password last set: Mon, 12 Mar 2018 18:07:58 CST Password can change: Mon, 12 Mar 2018 18:07:58 CST Password must change: never Last bad password : 0 Bad password count : 0 Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 接着修改samba用戶的家目錄權限 [root@samba-server ~]# chown -Rf kevin.kevin /home/kevin
7)啟動Samba服務
[root@samba-server ~]# systemctl start smb
[root@samba-server ~]# systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
[root@samba-server ~]# systemctl restart smb
[root@samba-server ~]# systemctl status smb
● smb.service - Samba SMB Daemon
Loaded: loaded (/usr/lib/systemd/system/smb.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-03-12 18:11:20 CST; 3s ago
Main PID: 977 (smbd)
Status: "smbd: ready to serve connections..."
CGroup: /system.slice/smb.service
├─977 /usr/sbin/smbd
├─978 /usr/sbin/smbd
├─979 /usr/sbin/smbd
└─980 /usr/sbin/smbd
Mar 12 18:11:19 samba-server systemd[1]: Starting Samba SMB Daemon...
Mar 12 18:11:19 samba-server systemd[1]: smb.service: Supervising process 977 which is not our child. We'll most likely not... exits.
Mar 12 18:11:20 samba-server smbd[977]: [2018/03/12 18:11:20.065982, 0] ../lib/util/become_daemon.c:124(daemon_ready)
Mar 12 18:11:20 samba-server systemd[1]: Started Samba SMB Daemon.
Mar 12 18:11:20 samba-server smbd[977]: STATUS=daemon 'smbd' finished starting up and ready to serve connections
Hint: Some lines were ellipsized, use -l to show in full.
8)開始測試
先往共享路徑/home/kevin里添加點內容
[root@samba-server kevin]# touch test1 test2 test3 [root@samba-server kevin]# mkdir a1 a2 a3 [root@samba-server kevin]# ls a1 a2 a3 test1 test2 test3
接着再windos 客戶機本地測試。”Win+E鍵"打開,在最上面的"網絡"地址欄輸入“\\192.168.10.204”,然后回車,輸入上面設置的samba賬號kevin及其密碼,就能共享到linux上的/home/kevin下的文件了



連接上后,就可以在windows和linux直接進行文件夾的共享操作了,可以讓里面放點測試文件



如果在windows客戶機上連接Samba出現網絡錯誤:Windows 無法訪問\\192.168.10.204\kevin,解決辦法如下:
查看上下文的安全關系 [root@samba-server ~]# semanage kevin -a -t samba_share_t /home/kevin/ -bash: semanage: command not found 如果系統出現上面的報錯 ,說明你系統里沒有安裝 semanage命令,下面開始安裝semanage: [root@samba-server ~]# yum provides /usr/sbin/semanage Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.0x.sg * epel: mirror.dmmlabs.jp * extras: mirror.0x.sg * updates: mirror.0x.sg policycoreutils-python-2.5-17.1.el7.x86_64 : SELinux policy core python utilities //這個是安裝包 Repo : base Matched from: Filename : [root@samba-server ~]# yum install -y policycoreutils-python 然后再執行一次,執行完成后,不要忘了刷新上下文關系 [root@samba-server ~]# semanage fcontext -a -t samba_share_t /home/kevin [root@samba-server ~]# restorecon -Rv /home/kevin 允許SElinux對於SMB用戶共享家目錄的布爾值 重啟Samba [root@samba-server ~]# systemctl restart smb
如何在windows本機訪問samba時用切換另一個用戶登錄
方法如下: 1)按鍵ctrl+r,打開"運行",輸入"cmd" 2)輸入命令"net use * /delete",接着輸入"Y",即先取消所有的net 連接 3)輸入切換賬號的命令"net use \\192.168.10.204\IPC$ grace@123 /user:grace",即表示切換到grace賬號(密碼為grace@123)
重置samba賬號密碼
[root@samba-server ~]# smbpasswd -a kevin //即重置kevin密碼
======================================================
清理windows下連接linux的samba服務緩存
在安裝配置linux服務器samba服務之初,samba服務難免會經過多次修改配置/重啟,在期間win的系統或許早已連接上了samba。samba修改配置修改后,特別是用戶權限,再次從win登錄就很容易出現因緩存的權限原因導致不允許訪問或者操作。
這時一般要等很久會清理緩存,另外重啟win也會清理。但這效率很低。用以下手動的方法可以實時清理。
清理步驟:
1)打開win的命令行(ctrl+R,輸入cmd)。
2)在命令行里輸入net use,就會打印出當前緩存的連接上列表。
3)根據列表,一個個刪除連接: net use 遠程連接名稱 /del;
或者一次性全部刪除:net use * /del。
這樣再次命令行輸入samba服務地址的時候,就會重新讓你輸入訪問的賬戶和密碼了。
======================================================
可以在一個samba環境下建立多個業務組的共享目錄
比如:
創建一個運維部門的samba共享磁盤,可以看到所有的共享內容;
創建一個產品風控組的samba共享磁盤,只能看到自己組的共享內容;
[root@samba ~]# cd /etc/samba/
[root@samba samba]# ls
lmhosts ops.smb.conf smb.conf smb.conf.bak smbusers chanpinfengkong.smb.conf
[root@samba samba]# diff smb.conf smb.conf.bak
103d102
< config file = /etc/samba/%U.smb.conf #使用config file時,當用戶訪問Samba服務器,只能看到自己,其他在smb.conf中定義的共享資源都無法看到。
[root@samba samba]# cat ops.smb.conf
[信息科技部-運維小窩]
comment = please do not modify it all will
path= /data/samba
public = no
valid users = wangshibo,linan,@samba
printable = no
write list = @samba
[root@samba samba]# cat chanpinfengkong.smb.conf
[產品風控組共享目錄]
comment = please do not modify it all will
path= /data/samba/產品風控組
public = no
valid users = xiaomin,haokun,@samba
printable = no
write list = @samba
useradd創建以上的幾個用戶,並設置好用戶家目錄
[root@samba ~]# useradd wangshibo -d /data/samba -s /sbin/nologin
[root@samba ~]# useradd linan -d /data/samba -s /sbin/nologin
[root@samba ~]# useradd xiaomin -d /data/samba/產品風控組 -s /sbin/nologin
[root@samba ~]# useradd haokun -d /data/samba/產品風控組 -s /sbin/nologin
[root@samba ~]# cat /etc/passwd
......
wangshibo:x:507:508::/data/samba:/sbin/nologin
lijinhe:x:508:509::/data/samba:/sbin/nologin
......
xiaomin:x:1006:1006::/data/samba/產品風控組:/sbin/nologin
haokun:x:1007:1007::/data/samba/產品風控組:/sbin/nologin
chanpinfengkong:x:1010:1010::/home/chanpinfengkong:/bin/bash
將這幾個用戶添加到samba里
[root@samba ~]# pdbedit -a -u wangshibo
[root@samba ~]# pdbedit -a -u linan
[root@samba ~]# pdbedit -a -u xiaomin
[root@samba ~]# pdbedit -a -u haokun
[root@samba ~]# pdbedit -L
wangshibo:507:
linan:510:
xiaomin:1006:
haokun:1007:
創建chanpinfengkong組,將xiaomin和haokun添加到這個組內
[root@samba ~]# useradd chanpinfengkong
[root@samba ~]# usermod -G chanpinfengkong xiaomin
[root@samba ~]# usermod -G chanpinfengkong haokun
創建samba共享目錄
[root@samba ~]# cd /data/
[root@samba data]# mkdir samba
[root@samba data]# mkdir samba/產品風控組
[root@samba data]# chown -R samba.samba samba
[root@samba data]# chmod -R 777 samba
[root@samba data]# setfacl -R -m g:chanpinfengkong:rwx samba/產品風控組
賦權腳本
[root@samba ~]# cat /opt/samba.sh
#!/bin/bash
while [ "1" = "1" ]
do
/bin/chmod -R 777 /data/samba
/usr/bin/setfacl -R -m g:chanpinfengkong:rwx /data/samba/產品風控組
done
[root@samba ~]# nohup sh -x /opt/samba.sh &
[root@samba ~]# ps -ef|grep samba.sh
root 62836 1 16 May09 ? 14-23:47:39 sh -x /opt/samba.sh
root 185455 117471 0 15:41 pts/2 00:00:00 grep samba.sh
如上配置后,登錄samba:
1)用wangshibo,linan賬號登錄samba,能看到"/data/samba"下面所有的共享內容。
2)用xiaomin,haokun賬號登錄samba,只能看到"/data/samba/產品風控組" 下面的共享內容
3)如果還需要分更多的組,就如上面的"產品風控組"一樣進行配置即可!
