1. 程式人生 > >Linux下部署Samba服務環境的操作記錄

Linux下部署Samba服務環境的操作記錄

command 解決辦法如下 some var sco most get -c can

關於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]                                                    //這一段是新添加的內容
       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

Linux下部署Samba服務環境的操作記錄