天天看點

RHEL7-Samba共享測試

Samba使用2個程序

smb    ip之間的通信用smb  (tcp)      

nmb    主機名之間的通信用nmb (netbios協定)

端口

smb tcp   139  445

nmb udp   137  138

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

yum install samba*

firewall-cmd --permanent --add-service=samba # 添加防火牆政策

firewall-cmd --reload

systemctl restart smb  #啟動或重新開機smb服務。nmb服務可以不用啟動

systemctl enable smb

netstat -anplut | grep smb  #檢視端口 139 445

hosts allow = 192.168.100. EXCEPT 192.168.100.99

hosts deny  = 192.168.10.0/24 192.168.1.

同時出現,allow生效

workgroup = MYGROUP    windows的工作組名

server string = Samba Server Version %v

#對方看到的共享資訊,出于防止洩漏版本号的安全考慮,最好改掉

log file = /var/log/samba/log.%m

#%m代表用戶端ip,多個用戶端連接配接生成多個以用戶端ip結尾的日志

max log size = 50       

#日志大小50K

security = user    #設定共享的模式

#share 不用輸入使用者名/密碼 (注:rhel7.4中設定為share時報錯“WARNING: Ignoring invalid value 'share' for parameter 'security'”)

#user  伺服器用密碼檔案進行驗證,用戶端連接配接需要提供使用者名和密碼

#server 網絡中配置專門的伺服器認證

#domain 使用微軟的DC認證

測試環境說明:

LINUX系統:RHEL7.4

實體機系統:win10

虛拟機軟體:OracleVirtualbox5.1

RHEL1 IP:192.168.100.1 作為samba主伺服器

RHEL2 IP:192.168.100.2 作為客戶機

在RHEL1上

firewall-cmd  --permanent --add-service=samba

systemctl restart smb

netstat -anplut | grep smb

mkdir /share1 #建立測試目錄及檔案

mkdir /share2

mkdir /share3

touch /share1/111111

touch /share2/222222

touch /share3/333333

chmod o+rwx /share1    #生産中盡量采用setfacl賦權更安全

chmod o+rwx /share2

chcon -R -t samba_share_t  /share1 #設定安全上下文

chcon -R -t samba_share_t  /share2

chcon -R -t samba_share_t  /share3

getsebool -a | grep samba

#部分samba服務需打開samba_export_all_rw和samba_enable_home_dirs

# setsebool -P samba_export_all_rw  on

# setsebool -P samba_enable_home_dirs on

vim /etc/samba/smb.conf

sercurity = share    #更改 

[share1]        #用戶端連接配接後看到的檔案夾名

comment = Public Stuff    #用戶端連接配接後看到的描述

path = /share1        #本地的真實路徑

public = yes         #share模式,此處必須為yes

browseable = yes    #用戶端可見

在RHEL2上

yum install -y samba-client cifs-utils

smbclient  -L //192.168.100.1    #檢視伺服器共享目錄,無需輸入密碼

smbclient //192.168.100.1/share1    #無需輸入密碼即可進入

smb: \> ls

smb: \> get file1    #put上傳

如需寫入需要在RHEL1的配置檔案上打開writable = yes,并設定共享目錄/share1有o+w權限

批量下載下傳

smb: \>prompt

smb: \>mget file*

windows用戶端

設定虛拟機通過虛拟網絡與真機連接配接

\\192.168.100.1

net use * /del清除緩存

用戶端要使用使用者名、密碼登入,已經在伺服器存在的使用者名密碼/etc/passwd,用戶端使用的密碼可以和伺服器上的/etc/shadow不同。samba自己有一個檔案儲存使用者名密碼,必須public=no

useradd -s /sbin/nologin user1

useradd -s /sbin/nologin user2

useradd -s /sbin/nologin user3

smbpasswd -a user1 #建立samba使用者

smbpasswd -a user2

smbpasswd -a user3

-d         禁用samba使用者disable

-e         允許samba使用者enable

-x         删除samba使用者delete

修改配置檔案:

security = user        #更改

[share1]

comment = samba-share1

path = /share1

public = no

browseable = yes

writable = yes

[share2]

comment = samba-share2

path = /share2

writable = no

write list = user1

[share3]

comment = samba-share3

path = /share3

write list = user1    #隻有user1授權寫入

valid users = user1   #隻有user1授權登入

systemctl restart smb #重新開機smb服務

#優先級valid users>writable>write list

#writable=yes表示所有使用者都有寫的權限

#write list生效時必須writable=no

#write list和valid users可采用user1 @shichangbu +shichangbu的格式,中間用空格分開

smbclient -L //192.168.100.1

smbclient -U user1 //192.168.100.1/share1    #輸入user1密碼可進入

用user1登入share1-3均可寫入

用user2登入share1可寫入,share2可登陸不可寫入,share3不可登入

windows用戶端測試

如果搭建了samba服務,無法寫入

1.檢測配置檔案writable   write list

2.檢測檔案系統是否有寫的權限   ugo  setfacl

3.檢測selinux

1.布爾值

2.上下文

4.防火牆

修改配置檔案:vim /etc/samba/smb.conf

[Golble]

config file = /etc/samba/smb.conf.%U    #增加,%U代表使用者名,以後有對應配置檔案的使用者将直接跳轉讀取自己的配置檔案

cp /etc/samba/smb.conf  /etc/samba/smb.conf.user3

vim /etc/samba/smb.conf.user3

删除config file = /etc/samba/smb.conf.%U

smbclient -L  //192.168.100.1    #不輸入密碼可檢視到所有共享資源

smbclient -L  //192.168.100.1 -U user3    #輸入user3的密碼隻可以看到自己的資源

smbclient -U user3 //192.168.100.1/share3

#之前user3對share3不可登入,現在可登陸可寫入,由于自己的配置檔案中沒有share1和share2,是以user3不能登入share1和share2

mount -t cifs -o username=user1,password=user1 //192.168.100.1/share1/ /mnt/share1/

#-t cifs可省略,系統會自動識别

ll /mnt/share1/

umount /mnt/share1/

mount -o username=user1 //192.168.100.1/share1/ /mnt/share1/    #輸入密碼可挂載

vim /etc/fstab

//192.168.100.1/share1    /mnt/share1   cifs  credentials=/etc/filename  0  0

vim /etc/filename

   user=user1

   pass=user1

或者:

//192.168.100.1/share1    /mnt/share1   cifs  username=user1%user1  0  0

或者

//192.168.100.1/share1    /mnt/share1   cifs  username=user1,password=user1   0  0

#普通挂載su到其他使用者可以直接使用挂載資源

pdbedit -L    #檢視哪些使用者可以挂載

rm -f /etc/samba/smb.conf.user3

valid users = user1 user2

useradd user1    #本地必須有和伺服器對應的使用者

useradd user2

useradd user3

在root使用者下執行挂載

mount -o multiuser,user=user1,sec=ntlmssp  //192.168.0.1/share1   /mnt/share1

#輸入密碼可挂載,也加入password=user1直接挂載

su - user1

ll /mnt/share1    #無權限

cifscreds add 192.168.100.1   #輸入RHEL1上user1的密碼,從samba伺服器擷取認證

ll /mnt/share1    #獲得RHEL1上user1的權限,可登陸,可寫入

su - user2

cifscreds add 192.168.100.1   #輸入RHEL1上user2的密碼,從samba伺服器擷取認證

ll /mnt/share1    #獲得RHEL1上user2的權限,可登陸,不可寫入

su - user3

cifscreds add 192.168.100.1   #輸入RHEL1上user3的密碼,從samba伺服器擷取認證

或者cifscreds update //192.168.100.1 #更新認證

ll /mnt/share1    #獲得RHEL1上user3的權限,不可登陸

在root使用者下cifscreds clearall可清除獲得的認證,su到其他使用者需重新擷取認證

多使用者各自有配置檔案的情況,可以建立使用者檔案,一次挂載多個使用者,

vim /sambauser.txt

username=user1

password=user1

username=user2

password=user2

mount -o multiuser,credentials=/sambauser.txt,sec=ntlmssp //192.168.100.1/share1 /mnt/share1

fstab自動挂載

//192.168.100.1/share1 /mnt/share1 cifs defaults,multiuser,credentials=/sambauser.txt,sec=ntlmssp     0 0

mount -a

繼續閱讀