Linux下的samba服務配置詳解
- 一、Samba介紹
- 二、Samba工具及特性
- 三、搭建環境介紹
- 四、Samba配置步驟
- 1.服務端操作
- 2.在客戶端操作
- 五、測驗用戶的權限情況
一、Samba介紹
1.SMB(Server Messages Block,資訊服務塊)是一種在局域網上共享檔案和列印機的一種通信協議,它為局域網內的不同計算機之間提供檔案及列印機等資源的共享服務,
2.Samba是在Linux和UNIX系統上實作SMB協議的一個免費軟體,由服務器及客戶端程式構成,
3.其他相關概念:
CIFS:common internet file system,微軟基于SMB發布,
NFS:網路檔案系統,英文Network File System(NFS),是由SUN公司研制的UNIX表示層協議(presentation layer protocol),能使使用者訪問網路上別處的檔案就像在使用自己的計算機一樣,
二、Samba工具及特性
1.相關包
①amba 提供smb服務
②samba-client 客戶端軟體
③samba-common 通用軟體
④cifs-utils smb客戶端工具
⑤samba-winbind 和AD相關
2.相關服務行程
smbd 提供smb(cifs)服務 TCP:139,445
nmbd NetBIOS名稱決議 UDP:137,138
3.相關工具及命令
主組態檔:/etc/samba/smb.conf
語法檢查: testparm [-v] [/etc/samba/smb.conf]
客戶端工具:smbclient,pdnedit -L(列出Samba用戶串列,讀取passdb.tdb資料庫檔案)
三、搭建環境介紹
1.使用作業系統為RHEL8.0
2.Samba用戶為三個(kitty,buer,alice),都可以瀏覽共享目錄/share/,kitty和buer可以寫入檔案共享目錄,
3.服務端hostname為control,客戶端hostname為node1
四、Samba配置步驟
1.服務端操作
①在server端安裝軟體包
[root@control ~]# yum -y install samba samba-client
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 0:00:36 ago on Sun 02 May 2021 08:10:41 PM CST.
Dependencies resolved.
================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================
Installing:
samba x86_64 4.9.1-8.el8 BaseOS 708 k
samba-client x86_64 4.9.1-8.el8 BaseOS 636 k
Installing dependencies:
samba-common-tools x86_64 4.9.1-8.el8 BaseOS 461 k
samba-libs x86_64 4.9.1-8.el8 BaseOS 177 k
Transaction Summary
================================================================================================================================================
Install 4 Packages
Total size: 1.9 M
Installed size: 5.6 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : samba-libs-4.9.1-8.el8.x86_64 1/4
Running scriptlet: samba-libs-4.9.1-8.el8.x86_64 1/4
Installing : samba-common-tools-4.9.1-8.el8.x86_64 2/4
Installing : samba-4.9.1-8.el8.x86_64 3/4
Running scriptlet: samba-4.9.1-8.el8.x86_64 3/4
Installing : samba-client-4.9.1-8.el8.x86_64 4/4
Running scriptlet: samba-client-4.9.1-8.el8.x86_64 4/4
Verifying : samba-4.9.1-8.el8.x86_64 1/4
Verifying : samba-client-4.9.1-8.el8.x86_64 2/4
Verifying : samba-common-tools-4.9.1-8.el8.x86_64 3/4
Verifying : samba-libs-4.9.1-8.el8.x86_64 4/4
Installed products updated.
Installed:
samba-4.9.1-8.el8.x86_64 samba-client-4.9.1-8.el8.x86_64 samba-common-tools-4.9.1-8.el8.x86_64 samba-libs-4.9.1-8.el8.x86_64
Complete!
②創建samba共享目錄/share,設定權限,并拷貝檔案到共享目錄
[root@control ~]# mkdir /share
[root@control ~]# ll /share
total 0
[root@control ~]# ll -ld /share
drwxr-xr-x. 2 root root 6 May 2 20:24 /share
[root@control ~]# chmod 777 /share
[root@control ~]# ll -ld /share
drwxrwxrwx. 2 root root 6 May 2 20:24 /share
[root@control ~]# cp /etc/passwd /share
[root@control ~]# cp /etc/group /share
[root@control ~]# ls /share
group passwd
③創建samba用戶
[root@control ~]# groupadd it
[root@control ~]# useradd -g it kitty
[root@control ~]# useradd alice
[root@control ~]# useradd buer
[root@control ~]# smbpasswd -a kitty
New SMB password:
Retype new SMB password:
Added user kitty.
[root@control ~]# smbpasswd -a alice
New SMB password:
Retype new SMB password:
Added user alice.
[root@control ~]# smbpasswd -a buer
New SMB password:
Retype new SMB password:
Added user buer.
#查看samba用戶串列
[root@control ~]# pdbedit -L
kitty:2034:
buer:2036:
alice:2035:
④編輯samba主組態檔
進入配置目錄/etc/samba/smb.conf
[share]
path = /share
browseable = yes
write list = @it,buer
hosts allow = 192.168.200.0/24
測驗組態檔語法
[root@control ~]# testparm /etc/samba/smb.conf
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
printcap name = cups
security = USER
workgroup = SAMBA
idmap config * : backend = tdb
cups options = raw
[homes]
browseable = No
comment = Home Directories
inherit acls = Yes
read only = No
valid users = %S %D%w%S
[printers]
browseable = No
comment = All Printers
create mask = 0600
path = /var/tmp
printable = Yes
[print$]
comment = Printer Drivers
create mask = 0664
directory mask = 0775
force group = @printadmin
path = /var/lib/samba/drivers
write list = @printadmin root
[share]
hosts allow = 192.168.200.0/24
path = /share
write list = @it buer
⑤啟動samba服務
[root@control ~]# systemctl start smb
[root@control ~]# systemctl start nmb
[root@control ~]# systemctl enable nmb
Created symlink /etc/systemd/system/multi-user.target.wants/nmb.service → /usr/lib/systemd/system/nmb.service.
[root@control ~]# systemctl enable smb
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
⑥防火墻放行
[root@control ~]# firewall-cmd --permanent --add-service=samba
success
[root@control ~]# firewall-cmd --reload
success
⑦配置selinux安全標簽
[root@control ~]# semanage fcontext -a -t samba_share_t '/share(/.*)?'
[root@control ~]# restorecon
restorecon restorecon_xattr
[root@control ~]# restorecon -vvFR /share
Relabeled /share from unconfined_u:object_r:default_t:s0 to system_u:object_r:samba_share_t:s0
Relabeled /share/passwd from unconfined_u:object_r:default_t:s0 to system_u:object_r:samba_share_t:s0
Relabeled /share/group from unconfined_u:object_r:default_t:s0 to system_u:object_r:samba_share_t:s0
2.在客戶端操作
①客戶端安裝相關軟體包
[root@node1 yum.repos.d]# yum -y install samba-client cifs-utils
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Repository AppStream is listed more than once in the configuration
Repository BaseOS is listed more than once in the configuration
AppStream 3.1 MB/s | 3.2 kB 00:00
BaseOS 2.7 MB/s | 2.7 kB 00:00
ansiable 0.0 B/s | 0 B 00:00
Failed to synchronize cache for repo 'ansiable', ignoring this repo.
Dependencies resolved.
================================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================================
Installing:
cifs-utils x86_64 6.8-2.el8 BaseOS 93 k
samba-client x86_64 4.9.1-8.el8 BaseOS 636 k
Transaction Summary
================================================================================================================================================
Install 2 Packages
Total size: 729 k
Installed size: 2.3 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : samba-client-4.9.1-8.el8.x86_64 1/2
Running scriptlet: samba-client-4.9.1-8.el8.x86_64 1/2
Installing : cifs-utils-6.8-2.el8.x86_64 2/2
Running scriptlet: cifs-utils-6.8-2.el8.x86_64 2/2
Verifying : cifs-utils-6.8-2.el8.x86_64 1/2
Verifying : samba-client-4.9.1-8.el8.x86_64 2/2
Installed products updated.
Installed:
cifs-utils-6.8-2.el8.x86_64 samba-client-4.9.1-8.el8.x86_64
Complete!
②使用smbclient工具測驗kitty和alice用戶是否可以查看共享檔案夾
[root@node1 yum.repos.d]# smbclient -L //192.168.200.150 -U kitty%redhat
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
share Disk
IPC$ IPC IPC Service (Samba 4.9.1)
kitty Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
SAMBA CONTROL
[root@node1 yum.repos.d]# smbclient -L //192.168.200.150 -U alice%redhat
Sharename Type Comment
--------- ---- -------
print$ Disk Printer Drivers
share Disk
IPC$ IPC IPC Service (Samba 4.9.1)
alice Disk Home Directories
Reconnecting with SMB1 for workgroup listing.
Server Comment
--------- -------
Workgroup Master
--------- -------
SAMBA CONTROL
③創建samba認證用戶檔案smbur,使用alice作為認證用戶
[root@node1 yum.repos.d]# echo "username=alice" >> /etc/samba/smbur.txt
[root@node1 yum.repos.d]# echo "password=redhat" >> /etc/samba/smbur.txt
④編輯/etc/fstab檔案,使掛載共享目錄永久生效
[root@node1 yum.repos.d]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Mar 19 22:21:55 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=b7190d80-906f-4b9d-9ab4-5a503ecaea2c / xfs defaults 0 0
UUID=525a30a7-d484-4ed5-9f38-f827f54e29ff /boot xfs defaults 0 0
UUID=e6cf8733-5eec-4942-9429-c3e9087b6ff0 swap swap defaults 0 0
//192.168.200.150/share /media cifs defaults,credentials=/etc/samba/smbur.txt,multiuser,sec=ntlmssp 0 0
[root@node1 yum.repos.d]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 970M 0 970M 0% /dev
tmpfs 984M 0 984M 0% /dev/shm
tmpfs 984M 9.1M 974M 1% /run
tmpfs 984M 0 984M 0% /sys/fs/cgroup
/dev/nvme0n1p3 18G 4.0G 14G 23% /
/dev/nvme0n1p1 495M 140M 356M 29% /boot
tmpfs 197M 4.0K 197M 1% /run/user/0
/dev/sr0 6.7G 6.7G 0 100% /mnt/cdrom
//192.168.200.150/share 13G 8.5G 4.1G 68% /media
五、測驗用戶的權限情況
1.檢查在/media目錄下以alice用戶掛載權限
[root@node1 yum.repos.d]# cd /media/
[root@node1 media]# touch file1
touch: cannot touch 'file1': Permission denied
[root@node1 media]# ls
group passwd
2.切換本地admin用戶,以kitty和buer這兩個samba用戶訪問測驗權限
[root@node1 yum.repos.d]# cd /media/
[root@node1 media]# touch file1
touch: cannot touch 'file1': Permission denied
[root@node1 media]# ls
group passwd
[root@node1 media]# useradd admin
[root@node1 media]# su - admin
[admin@node1 ~]$ cd /media
[admin@node1 media]$ cifscreds add -u kitty control
Password:
[admin@node1 media]$ ls
group passwd
[admin@node1 media]$ touch file1
[admin@node1 media]$ ls
file1 group passwd
[admin@node1 media]$ cifscreds update -u buer control
Password:
[admin@node1 media]$ ls
file1 group passwd
[admin@node1 media]$ touch file2
[admin@node1 media]$ ls
file1 file2 group passwd
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282715.html
標籤:其他
上一篇:java2實用教程小白入門(1)
