題目要求:在 server0 上配置 SAMBA 服務
- l 您的 samba 服務器必須是 STAFF 工作組的一個成員
- l 只有網段 172.25.0.0/16 內的客戶端可以訪問 /common 共享
- l common 必須是可以瀏覽的
- l 用戶 floyd 必須能夠讀取共享中的內容,如果需要的話,驗證密碼是 flectrag
知識點小貼士:
Samba是在Linux和UNIX系統上實現SMB協議的一個免費軟件,由服務器及客戶端程序構成。SMB(Server Messages Block,信息服務塊)是一種在局域網上共享文件和打印機的一種通信協議,它為局域網內的不同計算機之間提供文件及打印機等資源的共享服務。SMB協議是客戶機/服務器型協議,客戶機通過該協議可以訪問服務器上的共享文件系統、打印機及其他資源。通過設置“NetBIOS over TCP/IP”使得Samba不但能與局域網絡主機分享資源,還能與全世界的電腦分享資源。
解題步驟:
(1)安裝samba, samba-client
<code>[root@server0 ~]# yum install -y samba samba-client/<code>
(2)enable 服務,使其開機自啟動。
<code>[root@server0 yum.repos.d]# systemctl enable smb.service nmb.service
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
ln -s '/usr/lib/systemd/system/nmb.service' '/etc/systemd/system/multi-user.target.wants/nmb.service'/<code>
(3)由於是網絡服務,防火牆需要放行samba服務,然後重新加載防火牆。
<code>[root@server0 yum.repos.d]# firewall-cmd --add-service=samba --permanent
success
[root@server0 yum.repos.d]# firewall-cmd --reload
Success/<code>
(4)檢查STAFF組是否存在。如果不存在則需要創建。
<code>[root@server0 yum.repos.d]# gerep STAFF /etc/group
bash: gerep: command not found...
Similar command is: 'egrep'/<code>
註釋:在/etc/group中沒有查到,說明組不存在。
(5)創建一個系統層面的組。
<code>[root@server0 yum.repos.d]# groupadd -r STAFF/<code>
註釋:參數 -r, --system 代表 create a system account
(6)創建用戶rob
檢查用戶floyd是否存在。
用戶不存在,那麼需要新創建用戶,在創建floyd時一定要檢查desktop0上是否已經存在用戶floyd、,如存在,那麼需要對齊用戶的uid, 不然會被扣分。
<code>[root@server0 yum.repos.d]# id STAFF
id: STAFF: no such user
[root@server0 yum.repos.d]# useradd floyd -s /sbin/nologin/<code>
創建用戶,並對齊用戶id
將用戶floyd加入STAFF組中。
<code>[root@server0 yum.repos.d]# useradd floyd -s /sbin/nologin [root@server0 yum.repos.d]# usermod -G STAFF floyd/<code>
註釋:-G, --groups GROUPS 表示 new list of supplementary GROUPS,這一步也可以不做,但是在實踐中最好是要做的,這樣讓設置更加的完整。
(7)創建目錄
<code>[root@server0 yum.repos.d]# mkdir /common/<code>
(8)將共享的文件夾所屬組改為STAFF
<code>[root@server0 yum.repos.d]# chown :STAFF -R /common//<code>
註釋:
-R, --recursive 表示 operate on files and directories recursively,即處理指定目錄以及其子目錄下的所有文件
(9)改變目錄的權限
<code>[root@server0 yum.repos.d]# chmod 2775 /common//<code>
註釋:這裡設置了沾滯位的權限,SUID對應八進制數字“4”、SGID對應八進制數字“2”:如使用“chmod 4755”設置SUID權限,使用“chmod 2755”設置SGID權限,使用“chmod 6755”同時設置SUID和SGID權限
(10)設置目錄的訪問控制列表
<code>[root@server0 yum.repos.d]# setfacl -m floyd:rX /common//<code>
註釋:因為是對目錄設置,所有這裡用了大寫的X,如果是對文件設置那麼需要用x
-m, --modify=acl 表示 modify the current ACL(s) of file(s)
(11)修改SELinux的上下文標籤
<code>[root@server0 yum.repos.d]# semanage fcontext -a -t samba_share_t '/common(/.*)?'/<code>
註釋:'/common(/.*)?' 不要忘記放在單引號裡面。如果記不住(/.*)?, 那麼可以執行命令semanage fcontext -l ,就會列出很多,可以從中複製一個。
檢查是否設置成功
<code>[root@server0 yum.repos.d]# semanage fcontext -l | grep common
/common(/.*)? /<code>
生效安全上下文
<code>[root@server0 yum.repos.d]# restorecon -vFR /common/
restorecon reset /common context unconfined_u:object_r:default_t:s0->system_u:object_r:samba_share_t:s0/<code>
註釋:
-F Force reset of context 強制重設上下文
-R recursively 遞歸操作
-v verbose 顯示修改過程
需要使用restorecon 命令將設置好的SELinux 安全上下文立即生效。在使用restorecon 命令時,可以加上-Rv 參數對指定的目錄進行遞歸操作,以及顯示SELinux 安全上下文的修改程。
(12)修改samba配置
<code>[root@server0 yum.repos.d]# vim /etc/samba/smb.conf/<code>
查找並修改workgroup
滾動到最後,找到如下Share Definitions部分,然後添加如下設置。
<code>[common]
comment = share to floyd
path = /common
browseable = yes
writable = no
hosts allow=172.25.0. .example.com/<code>
註釋:
[common] 分享時對外可見的目錄名稱,不一定實際存在
comment = share to floyd 描述,可以不加這句,沒影響
path = /common 本地對外共享的實際路徑
browseable = yes
writable = no
hosts allow=172.25.0. .example.com
題目要求只有example.com域內的客戶端可以訪問common共享,注意172.25.0. 和 .example.com之間需要空格隔開。且172.25.0. 後面有一個點.example.com前面有一個點
(13)重啟服務
<code>[root@server0 yum.repos.d]# systemctl restart smb nmb/<code>
(14)根據題目要求,創建samba用戶floyd密碼為flectrag
<code>[root@server0 yum.repos.d]# smbpasswd -a floyd
New SMB password:
Retype new SMB password:
Added user floyd./<code>
本題所有設置已經完成,下面去desktop0上進行驗證。
(1)在desktop0上安裝samba-client,驗證需要安裝samba-client。
<code>[root@desktop0 ~]# yum install -y samba-client/<code>
(2)驗證是否可以看到server0上共享的目錄
<code>[root@desktop0 ~]# smbclient -L //server0 -U rob/<code>
可以看到server0上配置的共享,說明配置成功。
<code> [root@desktop0 ~]# yum -y install samba-client
Loaded plugins: langpacks
Package samba-client-4.1.1-31.el7.x86_64 already installed and latest version
Nothing to do/<code>
<code>[root@desktop0 ~]# smbclient -L //server0 -U floyd
Enter floyd's password:
Domain=[STAFF] OS=[Unix] Server=[Samba 4.1.1]
Sharename Type Comment
--------- ---- -------
common Disk share to floyd
IPC$ IPC IPC Service (Samba Server Version 4.1.1)
floyd Disk Home Directories
Domain=[STAFF] OS=[Unix] Server=[Samba 4.1.1]
Server Comment
--------- -------
SERVER0 Samba Server Version 4.1.1
Workgroup Master
--------- -------
STAFF SERVER0
[root@desktop0 ~]#/<code>
常見錯誤解決:linux報錯:yum 命令報錯 “ There are no enabled repos ” —— yum repolist 為 0
<code>[root@server0 ~]# yum install -y samba samba-client
Loaded plugins: langpacks
There are no enabled repos.
Run "yum repolist all" to see the repos you have.
You can enable repos with yum-config-manager --enable <repo>
[root@server0 ~]# yum repolist all
Loaded plugins: langpacks
repolist: 0/<repo>/<code>
(1)yum list可以查出數來,證明ISO系統鏡像是關聯了的且掛載成功:
(2)yum repolist顯示倉庫的時候是0
<code>[root@server0 ~]# yum repolist
Loaded plugins: langpacks
repolist: 0/<code>
(3)總結原因:問題應該出在了.repo文件上了。
<code>[root@server0 ~]# cd /etc/yum.repos.d/
[root@server0 yum.repos.d]# ll
total 0
[root@server0 yum.repos.d]#/<code>
無*.repo文件
(4)創建repo文件,可以從desktop0上覆制,創建一個文件:
<code>[root@server0 yum.repos.d]# vim /etc/yum.repos.d/classroom.example.com_content_rhel7.0_x86_64_dvd.repo/<code>
<code>
[classroom.example.com_content_rhel7.0_x86_64_dvd]
name=added from: http://classroom.example.com/content/rhel7.0/x86_64/dvd
baseurl=http://classroom.example.com/content/rhel7.0/x86_64/dvd
enabled=1/<code>
(5)再次執行yum repolist all命令:
<code>[root@server0 yum.repos.d]# yum repolist all
Loaded plugins: langpacks
classroom.example.com_content_rhel7.0_x86_64_dvd | 4.1 kB 00:00
(1/2): classroom.example.com_content_rhel7.0_x86_6 | 134 kB 00:00
(2/2): classroom.example.com_content_rhel7.0_x86_6 | 3.4 MB 00:00
repo id repo nam status
classroom.example.com_content_rhel7.0_x86_64_dvd added fr enabled: 4,305
repolist: 4,305/<code>
(6)成功安裝軟件包:
<code>[root@server0 yum.repos.d]# yum -y install samba samba-client
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.1.1-31.el7 will be installed
---> Package samba-client.x86_64 0:4.1.1-31.el7 will be installed
--> Finished Dependency Resolution/<code>
閱讀更多 神小威 的文章