天天看點

linux-SMB檔案共享

################SMB檔案共享###############

用internet檔案系統(CIFS)也成為伺服器是使用于MicrosftWindows伺服器和用戶端的标準檔案和列印機共享系統資訊塊(SMB)

Samba服務可用于将Linux檔案系統作為CIFS/SMB網絡檔案共享進行共享,并将Linux列印機作為CIFS/SMB列印機共享進行共享

軟體包:

Samba-common  Samba的支援檔案

Samba-client  用戶端應用程式

Samba         伺服器應用程式

伺服器名稱:smb  nmb

服務端口:通常使用TCP/445進行所有連接配接。還使用UDP137、UDP138和TCP/139進行向後相容

主配置檔案:/etc/samba/smb.conf

*)安裝服務####################

[root@desktop ~]# yum search samba 

[root@desktop ~]# yum install samba-client.x86_64 samba-common.x86_64 samba.x86_64 -y 安裝smb服務

[root@desktop ~]# systemctl start smb   開啟服務

[root@desktop ~]# netstat -antlupe | grep smb檢視服務端口(兩個服務)

[root@desktop ~]# netstat -antlupe | grep nmb

linux-SMB檔案共享

[root@station ~]# smbclient -L //172.25.12.12   檢視(匿名)

Enter root's password:  回車

Anonymous login successful  匿名

linux-SMB檔案共享

*)建立smb使用者#######################

linux-SMB檔案共享

建立smb使用者,使用者必須是系統已有的使用者

[root@station ~]# pdbedit -L     檢視smb使用者

[root@station ~]#

[root@station ~]# smbpasswd -a student   建立smb使用者 

linux-SMB檔案共享

系統沒有的使用者,不能加入,如下

[root@station ~]# smbpasswd -a westos

linux-SMB檔案共享

[root@station ~]# smbclient -L //172.25.12.12 -U student 以使用者身份檢視

linux-SMB檔案共享

[root@station ~]# smbclient //172.25.12.12/student -U student    登入

linux-SMB檔案共享

*)實作挂載############################

-12主機---

[root@foundation12 yum.repos.d]# yum install samba-client -y 安裝服務

[root@desktop ~]# mount -o username=student,password=123 //172.25.12.12/student /mnt/

linux-SMB檔案共享

*)修改名稱########################

[root@station ~]# vim /etc/samba/smb.conf   編輯配置檔案

linux-SMB檔案共享
linux-SMB檔案共享

[root@station ~]# systemctl restart smb.service  重新開機服務

linux-SMB檔案共享

[root@station

更改成功!如下

linux-SMB檔案共享

*)指定誰登入smb#########################

[root@station ~]# vim /etc/samba/smb.conf  修改配置

linux-SMB檔案共享

如下10主機登入,250主機不能登入

linux-SMB檔案共享

*)共享自己的目錄#########################

情況1:

[root@station ~]# getenforce   檢視火牆狀态

[root@station ~]# mkdir /westos  建立目錄

linux-SMB檔案共享

Disabled

[root@station ~]# vim /etc/samba/smb.conf   修改配置檔案

linux-SMB檔案共享

[root@station ~]# systemctl restart smb.service 重新開機服務

如下,登入可看到自己的目錄!

linux-SMB檔案共享

情況二:

[root@desktop ~]# setenforce 1

[root@desktop ~]# getenforce   

Enforcing

則需要更改安全上下文

[root@desktop ~]# ls -Zd /westos/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos/

[root@desktop ~]# semanage fcontext -a -t samba_share_t '/westos(/.*)?'

[root@desktop ~]# restorecon -FvvR /westos/

restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0

restorecon reset /westos/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0

restorecon reset /westos/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0

restorecon reset /westos/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0

挂載

linux-SMB檔案共享

永久挂載

[root@desktop ~]# vim /etc/fstab

linux-SMB檔案共享

mount -a 使政策生效

*)匿名使用者登陸#######################

linux-SMB檔案共享

挂載:

[root@desktop ~]# mount //172.25.12.12/DATA /mnt/ -o username=guest,password="" 

   出現錯誤,由于版本不一樣

如下挂載:

[root@desktop ~]# mount //172.25.12.12/DATA /mnt/ -o username=guest  挂載

[root@desktop ~]# smbclient -L /172.25.12.12  匿名登入上

linux-SMB檔案共享
linux-SMB檔案共享

*)使用者可寫########################

linux-SMB檔案共享

如上不可寫!

[root@station ~]# vim /etc/samba/smb.conf

[root@station ~]# systemctl restart smb.service 

linux-SMB檔案共享

[root@station ~]# ls -ld /westos/

drwxr-xr-x 2 root root 42 Dec  4 16:18 /westos/   檢視權限

[root@station ~]# setfacl -m u:student:rwx /westos/  給使用者賦予讀寫執行權限

linux-SMB檔案共享

如下,使用者可寫了

linux-SMB檔案共享

[root@station ~]# useradd westos  建立使用者

[root@station ~]# smbpasswd -a westos  建立smb使用者

linux-SMB檔案共享

[root@station ~]# usermod -G student westos  将使用者機入S組

[root@station ~]# setfacl -m g:student:rwx /westos/  賦予組權限

linux-SMB檔案共享

326         write list = @student   @ / + 為該組

linux-SMB檔案共享

[root@station ~]# useradd admin  建立使用者名字為管理者 可不是真正的管理者 沒有管理者的權力

linux-SMB檔案共享

如下賦予他超戶身份

linux-SMB檔案共享

如下admin可寫了!

linux-SMB檔案共享

*)隐藏共享目錄######################

linux-SMB檔案共享

 browseable = no    預設為yes 隐藏此共享目錄(看不到,能用)

linux-SMB檔案共享

*)指定使用smb使用者#########################

linux-SMB檔案共享

                   vaild users = +student  隻有student 組用smb

                   vaild users = student   隻有student使用者用smb

[root@desktop  ~]# mount //172.25.12.12/DATA /mnt/ -o username=admin,password=123

mount error(13): Permission denied                              adimin不能實作挂載了

Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

--smb使用者改密碼------

[root@station ~]# smbpasswd student  

New SMB password:                 輸入新密碼

Retype new SMB password:      确認新密碼

*)認證##################################

linux-SMB檔案共享

[root@desktop mnt]# su - kiosk   非smb使用者

[kiosk@desktop ~]$ cd /mnt

[kiosk@desktop mnt]$ ls          可看到不安全

file  file1  file2  file3

SMB多使用者挂載

(客戶機)

linux-SMB檔案共享

[root@desktop ~]# yum install cifs-utils -y  安裝服務

[root@desktop ~]# vim /root/smbfile   編輯檔案

linux-SMB檔案共享

SMB多使用者認證

[root@desktop ~]# mount -o credentials=/root/smbfile,multiuser,sec=ntlmssp //172.25.12.12/DATA /mnt

[kiosk@desktop mnt]$ cifscreds add -u westos 172.25.12.12   以smb使用者身份檢視

Password:

[kiosk@desktop mnt]$ ls

file  file1  file2  file3  liu

[kiosk@desktop mnt]$ mkdir huan  建立目錄

linux-SMB檔案共享