[root@samba1 ~]# yum install samba samba-client samba-common -yvim
由於只有真實存在的用戶才能在samba中創建,因此咱們新建兩個測試用戶安全
[root@samba1 ~]# useradd testbash
[root@samba1 ~]# id test服務器
uid=1001(test) gid=1001(test) groups=1001(test)session
[root@samba1 ~]# useradd bss測試
[root@samba1 ~]# id bssui
uid=1002(bss) gid=1002(bss) groups=1002(bss)spa
[root@samba1 ~]# smbpasswd -a test ##添加一個samba用戶3d
New SMB password:rest
Retype new SMB password:
Added user test.
[root@samba1 ~]# pdbedit -L ##查看samba用戶
test:1001:
[root@samba1 ~]# pdbedit -x test ##刪除samba用戶
[root@samba1 ~]# pdbedit -L
建上兩個用戶用來測試:test bss
Client機器:
[root@samba2 ~]# yum install samba-client -y
[root@samba2 ~]# smbclient -L //192.168.52.145 ##訪問失敗
Enter root's password:
session setup failed: NT_STATUS_LOGON_FAILURE
在服務端:
[root@samba1 ~]# getsebool -a | grep samba
samba_enable_home_dirs --> on ##將此項打開
client端
[root@samba2 ~]# smbclient -L //192.168.52.145 -U bss ##用創建的用戶查看信息
Enter bss's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
IPC$ IPC IPC Service (Samba 4.4.4)
bss Disk Home Directories
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
Server Comment
--------- -------
Workgroup Master
--------- -------
[root@samba2 ~]# smbclient //192.168.52.145/bss -U bss ##用用戶登錄,能夠查看信息目錄內容
Enter bss's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
. D 0 Thu Mar 14 18:08:34 2019
.. D 0 Thu Mar 14 18:08:34 2019
.mozilla DH 0 Mon Mar 11 19:06:49 2019
.bash_logout H 18 Tue Jul 12 23:17:09 2016
.bash_profile H 193 Tue Jul 12 23:17:09 2016
.bashrc H 231 Tue Jul 12 23:17:09 2016
18093056 blocks of size 1024. 14720464 blocks available
上傳文件:
###經過登錄的方式去上傳
[root@samba2 ~]# cd /mnt/ ##須要在文件所在目錄上傳
[root@samba2 mnt]# ls
[root@samba2 mnt]# touch file
[root@samba2 mnt]# ls
file
[root@samba2 mnt]# smbclient //192.168.52.145/bss -U bss
Enter bss's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
. D 0 Thu Mar 14 18:08:34 2019
.. D 0 Thu Mar 14 18:08:34 2019
.mozilla DH 0 Mon Mar 11 19:06:49 2019
.bash_logout H 18 Tue Jul 12 23:17:09 2016
.bash_profile H 193 Tue Jul 12 23:17:09 2016
.bashrc H 231 Tue Jul 12 23:17:09 2016
18093056 blocks of size 1024. 14720660 blocks available
smb: \> put file
putting file file as \file (0.0 kb/s) (average 0.0 kb/s) ##上傳成功
###用掛載的方式寫入文件:
Client端:
[root@samba2 mnt]# mount //192.168.52.145/bss /media/ -o username=bss,password=westos
[root@samba2 mnt]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18093056 3348660 14744396 19% /
devtmpfs 918960 0 918960 0% /dev
tmpfs 933644 144 933500 1% /dev/shm
tmpfs 933644 9196 924448 1% /run
tmpfs 933644 0 933644 0% /sys/fs/cgroup
/dev/sda1 2037760 172088 1865672 9% /boot
tmpfs 186732 16 186716 1% /run/user/0
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
//192.168.52.145/bss 18093056 3372484 14720572 19% /media
[root@samba2 mnt]# cd /media/
[root@samba2 media]# ls
file
[root@samba2 media]# touch file{1..6}
[root@samba2 media]# ls
file file1 file2 file3 file4 file5 file6
[root@samba2 media]# vim /etc/fstab ##設置開機自啓,但此種方式一旦服務方端啓動,會致使客戶端也沒法啓動
//192.168.52.145/bss /media cifs defaults,username=bss,password=westos 0 0
[root@samba2 /]# umount /media/ ##先卸載已掛載的目錄
[root@samba2 /]# mount -a ##經過此命令進行掛載,若是沒有報錯即成功
Ps:也能夠將掛載命令寫入/etc/rc.d/rc.local 此文件中,也能夠實現開機自啓,須要給予此文件執行權限
掛載命令: mount //192.168.52.145/bss /media/ -o username=bss,password=westos
共享文件:
服務端:
[root@samba1 /]# mkdir /bss
[root@samba1 /]# touch /bss/test9
[root@samba1 /]# semanage fcontext -a -t samba_share_t '/bss(/.*)?' #修改安全上下文
[root@samba1 /]# semanage fcontext -l | grep /bss
/bss(/.*)? all files system_u:object_r:samba_share_t:s0
[root@samba1 /]# restorecon -RvvF /bss/
Restorecon reset /bss context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0
[root@samba1 /]# vim /etc/samba/smb.conf ##添加模塊
[bss]
comment = bss dir
path = /bss
[root@samba1 /]# systemctl restart smb.service
客戶端:
[root@samba2 /]# smbclient //192.168.52.145/bss -U bss ##客戶登錄
Enter bss's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
. D 0 Thu Mar 14 19:28:12 2019
.. DR 0 Thu Mar 14 19:23:39 2019
test9 N 0 Thu Mar 14 19:28:12 2019
18093056 blocks of size 1024. 14713772 blocks available
共享系統目錄:
服務端:
[root@samba1 /]# vim /etc/samba/smb.conf ##添加系統目錄模塊
[mnt]
comment = systemctl share file
path = /mnt
[root@samba1 /]# systemctl restart smb.service
此時能夠在客戶端查詢,但登入後沒法操做
[root@samba2 /]# smbclient -L //192.168.52.145/mnt -U bss
Enter bss's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
DIR Disk bss dir
mnt Disk systemctl share file
IPC$ IPC IPC Service (Samba 4.4.4)
bss Disk Home Directories
由於是系統目錄,若是更改安全上下文,會影響其餘用戶使用,因此對samba服務開放一些權限;
在服務端:
[root@samba1 /]# setsebool -P samba_export_all_ro on
[root@samba1 /]# setsebool -P samba_export_all_rw on
[root@samba1 /]# getsebool -a | grep samba
samba_export_all_ro --> on ##開方讀寫權限
samba_export_all_rw --> on
再次在客戶端登錄:
[root@samba2 /]# smbclient //192.168.52.145/mnt -U test
Enter test's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
. D 0 Thu Mar 10 19:05:55 2016
.. DR 0 Thu Mar 14 19:23:39 2019
file N 0 Thu Mar 14 23:23:52 2019
18093056 blocks of size 1024. 14713952 blocks available
權限管理:
[root@samba1 mnt]# vim /etc/samba/smb.conf ##在本身共享的目錄中添加以下
writable = yes ##表明全部用戶可寫
Ps:須要注意本身共享目錄的可寫權限。
客戶端登錄後可對共享目錄中的內容進行刪改
[root@samba2 /]# smbclient //192.168.52.145/bss -U test
Enter test's password:
Domain=[SAMBA] OS=[Windows 6.1] Server=[Samba 4.4.4]
smb: \> ls
. D 0 Thu Mar 14 19:28:12 2019
.. DR 0 Thu Mar 14 19:23:39 2019
test9 A 0 Thu Mar 14 19:28:12 2019
18093056 blocks of size 1024. 14713264 blocks available
smb: \> rm test9
指定用戶可寫:
在服務端
[root@samba1 bss]# vim /etc/samba/smb.conf
在本身的共享目錄中添加以下策略
write list = @test ##僅寫test表明僅有test用戶能夠操做,加上@表明test用戶組的成員均可以操做。
在客戶端可使用不一樣用戶登入測試:
bss用戶:
test用戶:
指定用戶登錄:
[root@samba1 bss]# vim /etc/samba/smb.conf
在本身的共享目錄模塊下
valid users = test ##指定只有test用戶能夠登錄
valid users = +test ##指定只有test組的用戶能夠登錄
[root@samba1 bss]# systemctl restart smb.service
隱藏共享目錄:
[root@samba1 bss]# vim /etc/samba/smb.conf
browseable = no ##表明將此目錄隱藏
[root@samba1 bss]# systemctl restart smb.service
客戶端:
匿名用戶登錄:
[root@samba1 bss]# vim /etc/samba/smb.conf
map to guest = bad user ##其餘用戶映射到guest
guest ok = yes ##容許其餘用戶登錄
[root@samba1 bss]# systemctl restart smb.service
將普通用戶指定爲root用戶;
服務端:
[root@samba1 bss]# vim /etc/samba/smb.conf
admin users = bss ##指定bss爲超級用戶
[root@samba1 bss]# systemctl restart smb.service
客戶端測試:
在服務端查看這個文件:
咱們以bss用戶上傳的文件,在這裏倒是屬於root用戶,這時由於咱們將bss用戶指定成爲了超級用戶
多用戶掛載:
首先在客戶端卸掉與服務端共享目錄的掛載
[root@samba2 mnt]# yum install cifs-utils.x86_64 -y
[root@samba2 mnt]# vim /root/smbpass
##填寫用戶信息,此處用戶必須在samba服務器上存在
username=bss
password=westos
[root@samba2 mnt]# chmod 600 /root/smbpass
[root@samba2 mnt]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //192.168.52.145/bss /media/
[root@samba2 mnt]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 18093056 3349384 14743672 19% /
devtmpfs 918960 0 918960 0% /dev
tmpfs 933644 144 933500 1% /dev/shm
tmpfs 933644 9168 924476 1% /run
tmpfs 933644 0 933644 0% /sys/fs/cgroup
/dev/sda1 2037760 172088 1865672 9% /boot
tmpfs 186732 16 186716 1% /run/user/0
/dev/sr0 3704296 3704296 0 100% /run/media/root/RHEL-7.3 Server.x86_64
//192.168.52.145/bss 18093056 3379408 14713648 19% /media ##掛載成功
[root@samba2 mnt]# cd /media/
[root@samba2 media]# ls
file1 file2 file3 file4 LOL
[root@samba2 media]# touch pps ##超級用戶沒有問題
[root@samba2 media]# su – test ##切換到普通用戶,會發現沒法查看
[test@samba2 ~]$ cd /media
[test@samba2 media]$ ls
ls: reading directory .: Permission denied
[test@samba2 media]$ cifscreds add -u bss 192.168.52.145 ##客戶端的普通用戶test經過
Password:
[test@samba2 media]$ ls
file1 file2 file3 file4 LOL pps
其餘區用戶同理