Samba是Linux中模拟Windows 文件/打印共享的服务器软件,在Linux / Windows 混合网络环境下是非常有用的。
安装Samba
@linuxidc:~$ sudo tasksel
选择 [*]Samba file server,按[tab]键跳到<ok>,敲回车。很简单,也可以 $ sudo apt-get install samba,再Ubuntu里没试过,应该也是一样的。
一. 创建共享目录,任何人可读、写、执行
@linuxidc:~$ sudo mkdir /opt/smbshare
@linuxidc:~$ sudo chmod 777 /opt/smbshare
@linuxidc:~$
二. 编辑配置文件
@linuxidc:~$ sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_backup
@linuxidc:~$ sudo vi /etc/samba/smb.conf
1. 找到[global]
workgroup = WORKGROUP
display charset = UTF-8
unix charset = UTF-8
dos charset = cp936
2. 搜寻这一行文字代码:
# security = user
用下面这两行取代
security = user
username map = /etc/samba/smbusers
3. 在最后一行添加:
[devshare]
comment = Shared Folder with username and password
path = /opt/smbshare
public = yes
writable = yes
valid users = dev
create mask = 0777
directory mask = 0777
force user = nobody
force group = nogroup
available = yes
browseable = yes
退出保存。
三. samba用户
1. 创建用户dev
@linuxidc:~$ sudo useradd dev
2. 给dev分配smb密码,注意,这个不是本机登录密码,本机登录密码没有设定。
@linuxidc:~$ sudo smbpasswd -a dev
New SMB password:
Retype new SMB password:
@linuxidc:~$
3. 编辑/etc/samba/smbusers
@linuxidc:~$ sudo vi /etc/samba/smbusers
添加下行:
dev = "Samba user."
退出保存。
如果防火墙UFW enable 了,则需要允许samba端口。
@linuxidc:~$ sudo ufw allow 139/tcp
Rules updated
Rules updated (v6)
@linuxidc:~$ sudo ufw allow 445/tcp
Rules updated
Rules updated (v6)
四. 测试samba服务器
1. 重启smbd
@linuxidc:~$ sudo service smbd restart
smbd start/running, process 9873
@linuxidc:~$
2. 服务器端测试:
@linuxidc:~$ sudo testparm
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[printers]"
Processing section "[print$]"
Processing section "[devshare]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions 【enter】
[global]
dos charset = cp936
display charset = UTF-8
server string = %h server (Samba, Ubuntu)
map to guest = Bad User
obey pam restrictions = Yes
pam password change = Yes
passwd program = /usr/bin/passwd %u
passwd chat = *Enter\snew\s*\spassword:* %n\n *Retype\snew\s*\spassword:* %n\n *password\supdated\ssuccessfully* .
username map = /etc/samba/smbusers
unix password sync = Yes
syslog = 0
log file = /var/log/samba/log.%m
max log size = 1000
dns proxy = No
usershare allow guests = Yes
panic action = /usr/share/samba/panic-action %d
[printers]
comment = All Printers
path = /var/spool/samba
create mask = 0700
printable = Yes
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/printers
browseable = No
[devshare]
comment = Shared Folder for DEV
path = /opt/smbshare
valid users = dev
force user = nobody
force group = nogroup
read only = No
create mask = 0777
directory mask = 0777
guest ok = Yes
@linuxidc:~$