關於Linux和Windows系統之間的文件傳輸,不少人選擇使用FTP,相對較安全,可是有時仍是會出現一些問題,好比上傳文件時,文件名莫名出現亂碼,文件大小改變等問題。相比較來講,使用Samba做爲文件共享,就省事簡潔多了。Samba服務器通訊協議(Server Messages Block)就是是爲了解決局域網內的文件或打印機等資源的共享服務問題,讓多個主機之間共享文件變成愈來愈簡單。下面簡單介紹下,在Centos7下部署Samba服務的操做記錄(測試機192.168.10.204):python
1)安裝Sambalinux
[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服務文件vim
[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帳號(如上配置中添加的內容)windows
設置爲不予許登入系統,且用戶的家目錄爲 /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服務bash
[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)若是還須要分更多的組,就如上面的"產品風控組"同樣進行配置便可!