# 安装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
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 项应与 Windows 主机保持一致,这里是WORKGROUP
security、map to guest项设置为允许匿名用户访问
再下面有两个section,实际为两个目录,section名就是目录名(映射到Windows上可以看见)。
第一个目录名是FileShare,匿名、公开、可写
第二个目录吗是WebDev,限定ted用户访问
默认文件属性644/755(不然的话,Windows上在这个目录下新建的文件会有“可执行”属性)
设置用户组和用户密码
```
[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.
```
# 创建Samba服务器目录
```
[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
```
# 启动Samba服务
```
[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设置是否正确无误
```
[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
```
# 列出当前机器上的文件共享情况
```
[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
--------- -------
```
# 写入一个测试文件
```
[root@localhost samba]# touch /smb/fileshare/1.txt
[root@localhost samba]# echo HelloLinux /smb/fileshare/1.txt
```
# Windows测试
Win键+R键打开运行器,输入\192.168.9.134FileShare
成功可以打开FileShare,并且可以成功看到1.txt文件和内容
打开WebDev,提示需要输入用户名和密码
在我的电脑右键打开映射网络驱动器
将WebDev地址输入
并输入密码验证可以打开,并测试写入一个文件
# 返回服务器上查看webdev目录果然出现该文件
···
[root[@localhost](https://my.oschina.net/u/570656) samba]# ls /smb/webdev/
hello.txt
# 停止服务
```
[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也会同样断开连接