linux 簡單記錄11 --使用 Samba 或 NFS 實現檔案共享
阿新 • • 發佈:2020-07-29
使用 Samba 或 NFS 實現檔案共享
--Samba 檔案共享服務;
--NFS(網路檔案系統);
--autofs 自動掛載服務。
1 Samba 檔案共享服務
Samba 服務程式現在已經成為在 Linux 系統與 Windows系統之間共享檔案的最佳選擇。
安裝
[root@iscsi ~]# yum install samba -y [root@iscsi ~]# cat /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 = @printadmin root force group = @printadmin create mask = 0664 directory mask = 0775
Samba 服務程式中的引數以及作用
[global] workgroup = MYGROUP #工作組名稱 server string = Samba Server Version %v #伺服器介紹資訊,引數%v 為顯示 SMB 版本號 log file = /var/log/samba/log.%m #定義日誌檔案的存放位置與名稱, 引數%m 為 來訪的主機名 max log size = 50 #定義日誌檔案的最大容量為 50KB security = user #安全驗證的方式,總共有 4 種 #share:來訪主機無需驗證口令;比較方便,但安全性很差 #user:需驗證來訪主機提供的口令後才可以訪問;提升了安全性 #server:使用獨立的遠端主機驗證來訪主機提供的口令(集中管理賬戶) #domain:使用域控制器進行身份驗證 passdb backend = tdbsam #定義使用者後臺的型別,共有 3 種 #smbpasswd:使用 smbpasswd 命令為系統使用者設定 Samba 服務程式的密碼 #tdbsam:建立資料庫檔案並使用 pdbedit 命令建立 Samba 服務程式的使用者 #ldapsam:基於 LDAP 服務進行賬戶驗證 load printers = yes #設定在 Samba 服務啟動時是否共享印表機裝置 cups options = raw #印表機的選項 [homes] comment = Home Directories #描述資訊 browseable = no #指定共享資訊是否在“網路上的芳鄰”中可見 writable = yes #定義是否可以執行寫入操作,與“read only”相反 [printers] #印表機共享引數
[root@iscsi ~]# mv /etc/samba/smb.conf /etc/samba/smb.conf.bak [root@iscsi ~]# cat /etc/samba/smb.conf.bak | grep -v "#" | grep -v ";" | grep -v "^$" > /etc/samba/smb.conf [root@iscsi ~]# cat /etc/samba/smb.conf [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
1.1 配置共享資源
Samba 服務程式的主配置檔案,包括全域性配置引數和區域配置引數。
全域性配置引數用於設定整體的資源共享環境,對裡面的每一個獨立的共享資源都有效。
區域配置引數則用於設定單獨的共享資源,且僅對該資源有效。
用於設定 Samba 服務程式的引數以及作用
[database] 共享名稱為 database comment = Do not arbitrarily modify the database file 警告使用者不要隨意修改資料庫 path = /home/database 共享目錄為/home/database public = no 關閉“所有人可見” writable = yes 允許寫入操作
--1 建立用於訪問共享資源的賬戶資訊。在 RHEL 7 系統中, Samba 服務程式預設使用的是使用者口令認證模式(user)。
pdbedit 命令用於管理 SMB 服務程式的賬戶資訊資料庫,格式為“pdbedit [選項] 賬戶”。
在第一次把賬戶資訊寫入到資料庫時需要使用-a 引數,以後在執行修改密碼、刪除賬戶等操作時就不再需要該引數了。
用於 pdbedit 命令的引數以及作用
-a 使用者名稱 建立 Samba 賬戶 -x 使用者名稱 刪除 Samba 賬戶 -L 列出賬戶列表 -Lv 列出賬戶詳細資訊的列表
[root@iscsi ~]# id samba id: samba: no such user [root@iscsi ~]# useradd samba [root@iscsi ~]# id samba uid=1012(samba) gid=1012(samba) groups=1012(samba) [root@iscsi ~]# pdbedit -a -u samba new password: ##輸入密碼 retype new password: ##確認密碼 Unix username: samba NT username: Account Flags: [U ] User SID: S-1-5-21-2997122023-287577459-3228560404-1000 Primary Group SID: S-1-5-21-2997122023-287577459-3228560404-513 Full Name: Home Directory: \\iscsi\samba HomeDir Drive: Logon Script: Profile Path: \\iscsi\samba\profile Domain: ISCSI 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: Tue, 14 Jul 2020 09:42:12 CST Password can change: Tue, 14 Jul 2020 09:42:12 CST Password must change: never Last bad password : 0 Bad password count : 0 Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
--2 建用於共享資源的檔案目錄。
[root@iscsi ~]# mkdir /home/database [root@iscsi ~]# chown -Rf samba:samba /home/database/
--3 設定 SELinux 服務與策略
# semanage fcontext -a -t samba_share_t /home/database # restorecon -Rv /home/database # getsebool -a | grep samba # setsebool -P samba_enable_home_dirs on
--4 編輯samba主配置檔案
[root@iscsi ~]# vim /etc/samba/smb.conf [root@iscsi ~]# cat /etc/samba/smb.conf [global] workgroup = MYGROUP log file=/var/log/samba/log.%m max log size 50 security = user passdb backend = tdbsam load printers = yes cups options = raw directory mask = 0775 [database] comment= do not arbitratily modify the database file path=/home/database public=no writable=yes [homes]引數為來訪使用者的家目錄共享資訊 [printers]引數為共享的印表機裝置。 --5 [root@iscsi ~]# systemctl restart smb [root@iscsi ~]# systemctl status smb ● smb.service - Samba SMB Daemon Loaded: loaded (/usr/lib/systemd/system/smb.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2020-07-20 10:04:37 CST; 7s ago Docs: man:smbd(8) man:samba(7) man:smb.conf(5) Main PID: 67555 (smbd) Status: "smbd: ready to serve connections..." CGroup: /system.slice/smb.service ├─67555 /usr/sbin/smbd --foreground --no-process-group ├─67557 /usr/sbin/smbd --foreground --no-process-group
1.2 Windows 訪問檔案共享服務 Samba共享伺服器 centos7 10.15.7.20 windows win10 10.17.60 在windows使用\\10.15.7.20,輸入使用者名稱密碼 samba 可以看到database資料夾,在該目錄下進行檢視、寫入、更名、刪除檔案等操作 [root@iscsi ~]# cd /home/database/ [root@iscsi database]# ll total 36 -rwxr--r-- 1 samba samba 28967 Jul 7 15:43 DNS.txt -rwxr--r-- 1 samba samba 6 Jul 20 10:08 new1.txt
1.3 Linux 訪問檔案共享服務 Samba共享伺服器 centos7 10.15.7.20 linux客戶端 centos7 10.15.7.22 [root@localhost ~]# yum install cifs-utils -y 在linux客戶端,寫入samba服務的使用者名稱、密碼、共享域的資訊 [root@localhost ~]# vim auth.smb username=samba password=samba domain=MYGROUP [root@localhost ~]# chmod 600 auth.smb [root@localhost ~]# mkdir /database [root@localhost ~]# vim /etc/fstab //10.15.7.20/database /database cifs credentials=/root/auth.smb 0 0 [root@localhost ~]# ll /root/auth.smb -rw-------. 1 root root 45 Jul 19 22:20 /root/auth.sm [root@localhost ~]# mount -a [root@localhost ~]# df -h //10.15.7.20/database 40G 6.2G 34G 16% /database [root@localhost ~]# cd /database/ [root@localhost database]# ll total 36 -rwxr--r--. 1 1012 1012 28967 Jul 7 03:43 DNS.txt -rwxr--r--. 1 1012 1012 6 Jul 19 22:08 new1.txt
2 NFS(網路檔案系統)--僅實現linux系統之間的共享
如果需要共享的檔案的主機都是linux,建議在客戶端部署NFS服務來共享檔案。
NFS(網路檔案系統)服務可以將遠端 Linux 系統上的檔案共享資源掛載到本地主機的目錄上,從而使得本地主機
(Linux 客戶端)基於 TCP/IP 協議,像使用本地主機上的資源那樣讀寫遠端 Linux 系統上的共享檔案。
[root@iscsi ~]# yum install nfs-utils -y #安裝nfs軟體包 --1 NFS 伺服器 centos 7 10.15.7.20 NFS 客戶端 centos 7 10.15.7.22 --2 建立目錄和資料夾 [root@iscsi ~]# mkdir /nfsfile [root@iscsi ~]# chmod -Rf 777 /nfsfile/ [root@iscsi ~]# echo "i love x" >/nfsfile/readme --3 修改配置檔案 用於配置 NFS 服務程式配置檔案的引數 ro 只讀 rw 讀寫 root_squash 當 NFS 客戶端以 root 管理員訪問時,對映為 NFS 伺服器的匿名使用者 no_root_squash 當 NFS 客戶端以 root 管理員訪問時,對映為 NFS 伺服器的 root 管理員 all_squash 無論 NFS 客戶端使用什麼賬戶訪問,均對映為 NFS 伺服器的匿名使用者 sync 同時將資料寫入到記憶體與硬碟中,保證不丟失資料 async 優先將資料儲存到記憶體,然後再寫入硬碟;這樣效率更高,但可能會丟失資料 [root@iscsi ~]# vim /etc/exports [root@iscsi ~]# more /etc/exports /nfsfile 10.15.7.*(rw,sync,root_squash) [root@iscsi ~]# systemctl restart rpcbind [root@iscsi ~]# systemctl start nfs-server --4 配置客戶端 showmount 命令中可用的引數以及作用 -e 顯示 NFS 伺服器的共享列表 -a 顯示本機掛載的檔案資源的情況 -v 顯示版本號 [root@iscsi ~]# showmount -e 10.15.7.20 #查詢nfs伺服器的遠端共享資訊 Export list for 10.15.7.20: /nfsfile 10.15.7.* 在 NFS 客戶端建立一個掛載目錄,使用 mount 命令並結合-t 引數,指定要掛載的檔案系統的型別,並在命令後面寫上伺服器的 IP 地址、 伺服器上的共享目錄以及要掛載到本地系統(即客戶端)的目錄。 [root@localhost ~]# mkdir /nfsfile [root@localhost ~]# mount -t nfs 10.15.7.20:/nfsfile /nfsfile [root@localhost ~]# vim /etc/fstab 10.15.7.20:/nfsfile /nfsfile nfs defaults 0 0 [root@localhost ~]# df -h //10.15.7.20/database 40G 6.2G 34G 16% /database 10.15.7.20:/nfsfile 40G 6.2G 34G 16% /nfsfile [root@localhost ~]# cd /nfsfile/ [root@localhost nfsfile]# ll total 4 -rw-r--r--. 1 root root 9 Jul 19 23:20 readme [root@localhost nfsfile]# cat readme i love x [root@localhost nfsfile]# echo "i'm xxiuerm luer" >> readme -bash: readme: Permission denied [root@localhost nfsfile]# touch 1.txt [root@localhost nfsfile]# echo "1234" > 1.txt [root@localhost nfsfile]# more 1.txt 1234 [root@iscsi ~]# cd /nfsfile/ [root@iscsi nfsfile]# ll total 8 -rw-r--r-- 1 nfsnobody nfsnobody 5 Jul 20 14:24 1.txt ###nfs客戶端建立的檔案 -rw-r--r-- 1 root root 15 Jul 20 11:32 readme ##nfs伺服器端建立的檔案 [root@iscsi nfsfile]# echo "222">> 1.txt