# 安裝Samba
```
[root@localhost ~]# yum -y install samba samba-client
```
```
[root@localhost ~]# rpm -qi samba
Name : samba
Epoch : 0
Version : 4.6.2
Release : 12.el7_4
Architecture: x86_64
Install Date: Tue 05 Dec 2017 09:34:16 AM CST
Group : Unspecified
Size : 1932039
License : GPLv3+ and LGPLv3+
Signature : RSA/SHA256, Tue 28 Nov 2017 01:42:09 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM : samba-4.6.2-12.el7_4.src.rpm
Build Date : Tue 28 Nov 2017 12:25:36 AM CST
Build Host : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem <http://bugs.centos.org>
Vendor : CentOS
URL : http://www.samba.org/
Summary : Server and Client software to interoperate with Windows machines
Description :
Samba is the standard Windows interoperability suite of programs for Linux and
Unix.
```
```
[root@localhost ~]# cd /etc/samba/
[root@localhost samba]# mv smb.conf smb.conf.origin
[root@localhost samba]# vi smb.conf
[global]
workgroup = WORKGROUP
server string = Ted Samba Server %v
netbios name = TedSamba
security = user
map to guest = Bad User
passdb backend = tdbsam
[FileShare]
comment = share some files
path = /smb/fileshare
public = yes
writeable = yes
create mask = 0644
directory mask = 0755
[WebDev]
comment = project development directory
path = /smb/webdev
valid users = ted
write list = ted
printable = no
create mask = 0644
directory mask = 0755
```
注釋:
workgroup 項(xiàng)應(yīng)與 Windows 主機(jī)保持一致,這里是WORKGROUP
security、map to guest項(xiàng)設(shè)置為允許匿名用戶訪問
再下面有兩個(gè)section,實(shí)際為兩個(gè)目錄,section名就是目錄名(映射到Windows上可以看見)。
第一個(gè)目錄名是FileShare,匿名、公開、可寫
第二個(gè)目錄嗎是WebDev,限定ted用戶訪問
默認(rèn)文件屬性644/755(不然的話,Windows上在這個(gè)目錄下新建的文件會(huì)有“可執(zhí)行”屬性)
設(shè)置用戶組和用戶密碼
```
[root@localhost samba]# groupadd smb
[root@localhost samba]# useradd ted -g smb -s /sbin/nologin
[root@localhost samba]# smbpasswd -a ted
New SMB password:smb
Retype new SMB password:smb
Added user ted.
```
# 創(chuàng)建Samba服務(wù)器目錄
```
[root@localhost samba]# mkdir -p /smb/{fileshare,webdev}
[root@localhost samba]# chown nobody:nobody /smb/fileshare/
[root@localhost samba]# chown ted:smb /smb/webdev/
[root@localhost samba]# ls -l /smb/
total 0
drwxr-xr-x 2 nobody nobody 6 Dec 5 09:48 fileshare
drwxr-xr-x 2 ted smb 6 Dec 5 09:48 webdev
```
# 啟動(dòng)Samba服務(wù)
```
[root@localhost samba]# systemctl start smb
[root@localhost samba]# systemctl enable smb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
```
# 打開防火墻
```
[root@localhost samba]# firewall-cmd --permanent --add-port=139/tcp
success
[root@localhost samba]# firewall-cmd --permanent --add-port=445/tcp
success
[root@localhost samba]# systemctl restart firewalld
```
# 使用testparm測試的Samba設(shè)置是否正確無誤
```
[root@localhost samba]# testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[FileShare]"
Processing section "[WebDev]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters
[global]
netbios name = TEDSAMBA
server string = Ted Samba Server %v
map to guest = Bad User
security = USER
idmap config * : backend = tdb
[FileShare]
comment = share some files
path = /smb/fileshare
create mask = 0644
guest ok = Yes
read only = No
[WebDev]
comment = project development directory
path = /smb/webdev
create mask = 0644
valid users = ted
write list = ted
```
# 列出當(dāng)前機(jī)器上的文件共享情況
```
[root@localhost samba]# smbclient -L localhost
Enter WORKGROUProot's password:
OS=[Windows 6.1] Server=[Samba 4.6.2]
Sharename Type Comment
--------- ---- -------
FileShare Disk share some files
WebDev Disk project development directory
IPC$ IPC IPC Service (Ted Samba Server 4.6.2)
OS=[Windows 6.1] Server=[Samba 4.6.2]
Server Comment
--------- -------
Workgroup Master
--------- -------
```
# 寫入一個(gè)測試文件
```
[root@localhost samba]# touch /smb/fileshare/1.txt
[root@localhost samba]# echo HelloLinux /smb/fileshare/1.txt
```
# Windows測試
Win鍵+R鍵打開運(yùn)行器,輸入\192.168.9.134FileShare
成功可以打開FileShare,并且可以成功看到1.txt文件和內(nèi)容
打開WebDev,提示需要輸入用戶名和密碼
在我的電腦右鍵打開映射網(wǎng)絡(luò)驅(qū)動(dòng)器
將WebDev地址輸入
并輸入密碼驗(yàn)證可以打開,并測試寫入一個(gè)文件
# 返回服務(wù)器上查看webdev目錄果然出現(xiàn)該文件
···
[root[@localhost](https://my.oschina.net/u/570656) samba]# ls /smb/webdev/
hello.txt
# 停止服務(wù)
```
[root[@localhost](https://my.oschina.net/u/570656) samba]# systemctl disable smb
Removed symlink /etc/systemd/system/multi-user.target.wants/smb.service.
[root[@localhost](https://my.oschina.net/u/570656) samba]# systemctl stop smb
```
# Windows也會(huì)同樣斷開連接