1. 程式人生 > >Linux SMB和NFS文件共享

Linux SMB和NFS文件共享

linux 基礎運維學習

一、smb文件共享

1.安裝和為smb添加用戶

1.1安裝smb軟件

yum install samba samba-common samba-client-y
systemctl start smb nmb
systemctl enable smb nmb

1.2為smb添加用戶

smb用戶必須時本地用戶
smbpasswd -a student
New SMB password:      #輸入smb當前用戶密碼
Retype new SMB password:  #確認密碼
 
pdbedit -L         #查看smb用戶信息
pdbedit -x smb用戶     #刪除smb用戶


1.3查看smb的共享目錄

查看自己的共享目錄,添加目錄後默認共享家目錄

[[email protected] ~]# smbclient  -L //172.25.254.231 -U student
Enter student‘s password:
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
 
         Sharename       Type     Comment
         ---------       ----     -------
         IPC$            IPC  IPC Service (Samba Server Version 4.2.3)
         student         Disk   Home Directories
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
 
         Server              Comment
         ---------            -------
 
         Workgroup            Master
         ---------            -------

1.4登陸smb 的共享目錄

[[email protected] ~]# smbclient //172.25.254.231/student  -U student
Enter student‘s password:
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
smb: \>
#此時你如果想看你的共享目錄就會出現如下錯誤,因為的selinux 開著
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
#這時你需要將selinux的samba_enable_home_dirs打開。
#同時還需要在配置文件中設定smb用戶可以訪問自己的家目錄
#就是/etc/samba/smb.conf中第30行 setsebool -P samba_enable_home_dirs on
[[email protected]
/* */ ~]#setsebool samba_enable_home_dirs on [[email protected] ~]# smbclient //172.25.254.231/student -U student Enter student‘s password: Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3] smb: \> ls . D 0 Thu Jul 10 19:06:52 2014 .. D 0 Thu Jul 10 18:19:09 2014 .bash_logout H 18 Wed Jan 29 07:45:18 2014 .bash_profile H 193 Wed Jan 29 07:45:18 2014 .bashrc H 231 Wed Jan 29 07:45:18 2014 .ssh DH 0 Thu Jul 10 18:19:10 2014 .config DH 0 Thu Jul 10 19:06:53 2014 10473900 blocks of size 1024. 7318236 blocksavailable

2. /etc/samba/smb.conf 的簡單配置

 
[[email protected] ~]# vim  /etc/samba/smb.conf
 
30 setsebool   -P samba_enable_home_dirs on
#開啟可以查看用戶的家目錄
49 setsebool  -P samba_export_all_roon      
#只讀共享
51 setsebool  -P samba_export_all_rwon     
#讀寫共享
##在selinux中設定smb用戶可以訪問自己的家目錄
89   workgroup = MYGROUP
#修改標簽名稱也就是下面的Domai的名稱
#[[email protected] ~]# smbclient  -L //172.25.254.231 -U student
#Enter student‘s password:
#Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
96  hosts allow =172.25.254.131
#只給172.25.254.131這個主機,其他用戶遠程登陸時會出下如下錯誤:
[[email protected] ~]# smbclient  //172.25.254.231/student  -U student
Enter student‘s password:
protocol negotiation failed:NT_STATUS_INVALID_NETWORK_RESPONSE

3.共享目錄的基本設定

3.1共享目錄的簡單設定步驟

vim /etc/samba/smb.conf
[haha]      共享名稱
comment = 對共享目錄的描述
path = 共享目錄的絕對路徑
workgroup = WESTOS
 
當共享目錄為用戶自建立目錄時
semanage fcontext -a -t samba_share_t ‘目錄名稱(/.*)?‘
restorecon -RvvF 目錄名稱
當共享目錄為系統建立目錄
setsebool -P samba_export_all_ro on       #只讀共享
setsebool -P samba_export_all_rw on      #讀寫共享

3.2共享目錄的實例

[[email protected] ~]# vim  /etc/samba/smb.conf
#下面為添加的共享目錄的代碼
[feitian]
#共享目錄的名稱,就是你掛載時需要填寫的:例如//172.25.254.231/feitian
comment = local directory  /westos
path = /westos
[[email protected] ~]# systemctl restart smb
#這時你在查看就會多了一個共享目錄當你登陸時會發現登陸之後,出現如下錯誤,還是因為selinux
[[email protected] ~]# smbclient -L //172.25.254.231 -U student
feitian      Disk      local directory /westos
[[email protected] westos]# smbclient //172.25.254.231/feitian  -U student
Enter student‘s password:       #輸入添加的用戶student的密碼
Domain=[MYGROUP] OS=[Windows 6.1]Server=[Samba 4.2.3]
smb: \> ls
NT_STATUS_ACCESS_DENIED listing \*
#修改smb共享目錄的安全上下文
[[email protected] ~]# semanage  fcontext -a -t  samba_share_t  ‘/westos(/.*)?‘
[[email protected] ~]# restorecon  -RvvF /westos
restorecon reset /westos contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file1 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file2 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
restorecon reset /westos/file3 contextunconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
#這時你登陸就會好了,別忘了/etc/samba/smb.conf中selinux需要打開。還要在主機上開啟selinux對smb服務的只讀或者讀寫權限。
[[email protected] ~]# mount//172.25.254.231/student   /mnt  -o username=student,password=student
[[email protected] ~]# df -h |grep//172.25.254.231
//172.25.254.231/student   10G 3.1G  7.0G  31% /mnt
#開機自動掛載
vim /etc/fstab
//172.25.254.231/feitian  /mnt  cifs defaults,username=studnet,password 0  0

4.samba的配置參數

#匿名用戶訪問
guest ok = yes
map to guest = bad user
 
#訪問控制
hosts allow =           #僅允許
hosts deny =           #僅拒絕
valid users =          #當前共享的有效用戶
valid users = westos    #當前共享的有效用戶為westos
valid users = @westos    #當前共享的有效用戶為westos組
valid users = +westos     #當前共享的有效用戶為westos組
 
#讀寫控制
所有用戶均可寫
chmod o+w /mnt
setsebool -P samba_export_all_rw on
vim /etc/samba/smb.conf
writable = yes
 
#設定指定用戶可寫
write list = student     #可寫用戶
write list = +student    #可寫用戶組
write list = @student      #也是可寫用戶組
admin users = westos     #共享的超級用戶指定

5.smb多用戶共享

在客戶端做如下操作

 [[email protected] ~]# vim /root/haha
username=student
password=student
[[email protected] ~]# chmod 600 /root/haha
[[email protected] ~]# yum install cifs-utils -y
[[email protected] ~]# cifscreds clear  -u student 172.25.254.231
Password:        ##smb用戶student的密碼
mount -o credentials=/root/haha,multiuser,sec=ntlmssp //172.25.254.231/haha  /mnt/
#credentials=/root/haha    指定掛載時所用到的用戶文件
#multiuser           支持多用戶認證
#sec=ntlmssp          認證方式為標準smb認證方式  
[[email protected] ~]# df -h|grep 172.25.254.231
//172.25.254.231/feitian   10G 3.1G  7.0G  31% /mnt

#共享用戶
cifscreds add -u westos  172.25.254.100
Password:         ##smb用戶westos的密碼
ls /mnt

二 、NFS 文件共享

1、NFS介紹

NFS是Network File System 的縮寫,主要功能是通過網絡讓不同的機器彼此之間共享文件或目錄,可以通過掛載(mount)的方式將NFS服務器端共享的數據文件目錄掛載到NFS客戶端本地中。NFS協議有多個版本:Linux支持版本4、版本3和版本2,而大多數系統管理員熟悉的是NFSv3。默認情況下,該協議並不安全,但是更新的版本(如NFSv4)提供了更安全的身份驗證支持,甚至可以通過kerberos進行加密

2、簡單的NFS不同主機之間的共享

在server上的簡但配置

 [[email protected] ~]# vim /etc/exports
 /westso/lala  172.25.254.231(rw,sync)
 #共享serve /westos/lala這個目錄,客戶端有讀和寫還有在共享目錄的建立刪除會直接寫入主機的內存中
 #exportfs -rv 直接刷新,不用重啟NFS服務

在client的簡但配置

[[email protected] ~]# showmount -e 172.25.254.231
Export list for 172.25.254.231:
/westos/lala 172.25.254.131
[[email protected] ~]# mount -t nfs 172.25.254.231:/westos /mnt
[[email protected] ~]# df -h|gerp 172.25.254.231
172.25.254.231:/westos/lala     9.2G  2.5G  6.3G  29% /mnt

設置開機自動掛載,這裏就不在說了,已經說太多變了

3、默認目錄自動掛載

3.1 安裝軟件

客戶端安裝軟件

[[email protected] ~]# yum install autofs -y
[[email protected]  ~]# systemctl restart autifs.server
[[email protected]  ~]# cd /net
[[email protected]  ~]# cd 172.25.254.231
[[email protected]  ~]# ls
 westos
[[email protected]  ~]# cd /westos/lala 
#這樣就可以看到他的共享目錄下面的內容,而且是自動掛載,退出目錄後默認5分鐘自動卸載。他可以修改
#在/etc/autofs.conf中可以修改。

4、 指定目錄的自動掛載

在客戶端修改配置文件

[[email protected] ~]# vim /etc/auto.master
#添加一行,/nfs是指共享目錄的第一層目錄,/etc/auto.nfs他是master的子文件
/westos  /etc/auto.nfs
[[email protected] ~]#vim /etc/auto.nfs
#共享目錄的第二層目錄
*   172.25.254.231:/&
#這個意思是前面的第二層目錄和第一層目錄是一致的,如果你掛載了lala就表示你 自動掛載的目錄為/westos/lala


本文出自 “13122323” 博客,請務必保留此出處http://13132323.blog.51cto.com/13122323/1956882

Linux SMB和NFS文件共享