samba 通過簡單配置就能夠實現Linux系統與Windows系統之間的文件共享工作,也可實現Linux與Linux之間的文件共享。
在配置samba前,有個小建議:虛擬機的ip地址最好配置成靜態的,動態也可以哦。
samba服務程序的配置方法
第一步:在虛擬機中安裝samba
[root@localhost ~]# yum install -y samba
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package samba.x86_64 0:4.10.16-13.el7_9 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================
Package Arch Version Repository Size
=======================================================================================
Installing:
samba x86_64 4.10.16-13.el7_9 updates 719 k
Transaction Summary
=======================================================================================
Install 1 Package
Total download size: 719 k
Installed size: 2.2 M
Downloading packages:
samba-4.10.16-13.el7_9.x86_64.rpm | 719 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : samba-4.10.16-13.el7_9.x86_64 1/1
Verifying : samba-4.10.16-13.el7_9.x86_64 1/1
Installed:
samba.x86_64 0:4.10.16-13.el7_9
Complete!
[root@localhost ~]#
第二步:查看samba服務程序主配置文件,並將主配置文件多余的刪去,只留下下面部分
(修改配置文件時使用命令 vim /etc/samba/smb.conf)
[root@localhost ~]# cat /etc/samba/smb.conf
[global]
workgroup = MYGROUP
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
[root@localhost ~]#
第三步:通過修改配置文件配置共享資源,手動添加下面信息
[root@localhost ~]# cat /etc/samba/smb.conf
[global]
workgroup = MYGROUP
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[database]
comment =Do not arbitrarily modify the database file
path = /home/database
public = no
writable = yes
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
[root@localhost ~]#
第四步:重啟samba服務
[root@localhost ~]#systemctl restart smb
[root@localhost ~]#
第五步:創建用於訪問共享資源的賬戶信息
Samba服務程序的數據庫要求賬戶必須在當前系統中已經存在,否則日后創建文件時將導致文件的權限屬性混亂不堪,由此引發錯誤。
pdbedit命令用於管理SMB服務程序的賬戶信息數據庫,格式為“pdbedit [選項] 賬戶”。在第一次把賬戶信息寫入到數據庫時需要使用-a參數,以后在執行修改密碼、刪除賬戶等操作時就不再需要該參數了
[root@localhost ~]# useradd wangju
[root@localhost ~]# id wangju
uid=1000(wangju) gid=1000(wangju) groups=1000(wangju)
[root@localhost ~]# pdbedit -a -u wangju
new password:此處輸入該賬戶在Samba服務數據庫中的密碼
retype new password:再次輸入密碼進行確認
Unix username: wangju
NT username:
Account Flags: [U ]
User SID: S-1-5-21-507407404-3243012849-3065158664-1000
Primary Group SID: S-1-5-21-507407404-3243012849-3065158664-513
Full Name: wangju
Home Directory: \\localhost\wangju
HomeDir Drive:
Logon Script:
Profile Path: \\localhost\wangju\profile
Domain: LOCALHOST
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: Wed, 06 Feb 2036 10:06:39 EST
Kickoff time: Wed, 06 Feb 2036 10:06:39 EST
Password last set: Mon, 13 Mar 2017 04:22:25 EDT
Password can change: Mon, 13 Mar 2017 04:22:25 EDT
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
第六步:創建用於共享資源的文件目錄,並且設置文件權限
修改完畢后執行restorecon命令,讓應用於目錄的新SELinux安全上下文立即生效
[root@localhost ~]# mkdir /home/database
[root@localhost ~]# chown -Rf wangju:wangju /home/database
[root@localhost ~]# semanage fcontext -a -t samba_share_t /home/database
[root@localhost ~]# restorecon -Rv /home/database
restorecon reset /home/database context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:samba_share_t:s0
第七步:設置SELinux服務與策略,使其允許通過Samba服務程序訪問普通用戶家目錄
執行getsebool命令,篩選出所有與Samba服務程序相關的SELinux域策略,根據策略的名稱選擇出正確的策略條目進行開啟即可
[root@localhost ~]# getsebool -a | grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
use_samba_home_dirs --> off
virt_sandbox_use_samba --> off
virt_use_samba --> off
[root@localhost ~]# setsebool -P samba_enable_home_dirs on
第八步:重啟smb服務,並清空iptables防火牆,檢驗配置效果
[root@localhost ~]# systemctl restart smb
[root@localhost ~]# systemctl enable smb ( 開機自啟動)
ln -s '/usr/lib/systemd/system/smb.service' '/etc/systemd/system/multi-user.target.wants/smb.service'
[root@localhost ~]# iptables -F
[root@localhost ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
第九步:Windows掛載共享
要在Windows系統中訪問共享資源,只需在Windows的“運行”命令框中輸入兩個反斜杠,然后再加上虛擬機中的IP地址即可
可以采用簡單的方式查看虛擬機的ip地址,取第一個地址
[root@localhost ~]# hostname -I
192.168.146.133 192.168.122.1
確定后會打開如下界面
輸入賬戶與密碼,就可看到共享界面的文檔了。
可以在共享文檔里面新建文件,可分別在Windows系統和Linux系統中查看
Windows系統中的效果如下圖
在Linux系統中的效果如下圖
第十步:Linux掛載共享
(從這一步開始,所有的配置都是在客戶端配置的)
Samba服務程序也可以實現Linux系統之間的文件共享,設置Samba服務程序所在主機(即Samba共享服務器)和Linux客戶端使用的IP地址,然后在客戶端安裝支持文件共享服務的軟件包(cifs-utils)。
[root@localhost ~]# yum install cifs-utils
Loaded plugins: langpacks, product-id, subscription-manager
rhel | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package cifs-utils.x86_64 0:6.2-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
cifs-utils x86_64 6.2-6.el7 rhel 83 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 83 k
Installed size: 174 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : cifs-utils-6.2-6.el7.x86_64 1/1
Verifying : cifs-utils-6.2-6.el7.x86_64 1/1
Installed:
cifs-utils.x86_64 0:6.2-6.el7
Complete!
第十一步:配置Linux客戶端
按照Samba服務的用戶名、密碼、共享域的順序將相關信息寫入到一個認證文件中,為了保證不被其他人隨意看到,最后把這個認證文件的權限修改為僅root管理員才能夠讀寫[root@localhost ~]# vim auth.smb
username=wangju
password=wangju
domain=MYGROUP
[root@localhost ~]# chmod -Rf 600 auth.smb
第十二步:Linux客戶端上創建一個用於掛載Samba服務共享資源的目錄
把掛載信息寫入到/etc/fstab文件中,以確保共享掛載信息在服務器重啟后依然生效
[root@localhost ~]# mkdir /database
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 4 19:26:23 2017
#
# 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
#
/dev/mapper/rhel-root / xfs defaults 1 1
UUID=812b1f7c-8b5b-43da-8c06-b9999e0fe48b /boot xfs defaults 1 2
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/cdrom /media/cdrom iso9660 defaults 0 0
//192.168.146.133/database /database cifs credentials=/root/auth.smb 0 0
[root@localhost ~]# mount -a
第十三步:進入到掛載目錄/database后就可以看到Windows系統訪問Samba服務程序時留下來文件了(即文件feifei.txt)
[root@localhost ~]# cat /database/feifei.txt
I like you but just like you
嘻嘻,文章結束啦!!!