samba服务可以实现在Linux上共享一个目录,可以让Windows用户访问。本节介绍samba共享服务器的搭建过程。
安装samba服务端和客户端:
[[email protected] ~]# yum install -y samba samba-client
通过实验来说明samba的使用方式:
实验1:共享一个目录,任何人都可以访问该目录,不需要用户名密码,只读
编辑配置文件:
[[email protected] ~]# vim /etc/samba/smb.conf
修改1:security = share
修改2:workgroup = WORKGROUP
修改3:在最后面添加一个共享模块:
[share1]
comment=share all
path=/tmp/share1
browseable=yes
public=yes
writable=no
创建共享目录随便写点东西:
[[email protected] ~]# mkdir /tmp/share1
[[email protected] tmp]# chmod 777 share1
[[email protected] ~]# cd /tmp/share1
[[email protected] share1]# echo "1111">1.txt
[[email protected] share1]# mkdir 222
[[email protected] share1]# chmod 777 222
启动samba:
[[email protected] ~]# /etc/init.d/smb start
启动 SMB 服务: [确定]
查看进程:
[[email protected] share1]# ps aux | grep smb
root 1484 0.0 0.1 26524 3484 ? Ss 00:18 0:00 smbd -D
root 1486 0.0 0.0 27040 1752 ? S 00:18 0:00 smbd -D
root 1495 0.0 0.0 5980 744 pts/0 S+ 00:22 0:00 grep smb
查看端口:
[[email protected] share1]# netstat -lnp | grep smb
tcp 0 0 0.0.0.0:139 0.0.0.0:* LISTEN 1484/smbd
tcp 0 0 0.0.0.0:445 0.0.0.0:* LISTEN 1484/smbd
tcp 0 0 :::139 :::* LISTEN 1484/smbd
tcp 0 0 :::445 :::* LISTEN 1484/smbd
验证Windows访问samba:
win + R 打开“运行”,输入\\192.168.11.160,确定,
进入共享目录share1,即可看到222目录和1.txt文件,尝试对222目录写操作失败,因为samba设置为不可写,即使777权限也不行。
验证Linux访问samba:
[[email protected] ~]# smbclient //192.168.147.139/share1
WARNING: The security=share option is deprecated
Enter root‘s password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-36.el6_8]
Server not using user level security and no password supplied.
smb: \> ls
. D 0 Sat Mar 11 00:20:12 2017
.. D 0 Sat Mar 11 00:19:18 2017
222 D 0 Sat Mar 11 00:20:12 2017
1.txt 5 Sat Mar 11 00:19:39 2017
35292 blocks of size 524288. 30482 blocks available
smb: \>
提示输入root密码,直接回车即可,ls即可看到共享目录内容。
也可以使用挂载的方式访问:
安装工具:
[[email protected] ~]# yum install -y cifs-utils
将共享目录挂载到指定目录:
[[email protected] ~]# mount -t cifs //192.168.147.139/share1 /opt/
password:直接回车
使用命令 df -h 即可看到挂载到的共享目录,同样是只读。
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cp1-lv_root 18G 1.5G 15G 10% /
tmpfs 947M 0 947M 0% /dev/shm
/dev/sda1 485M 31M 429M 7% /boot
//192.168.147.139/share1 18G 1.5G 15G 10% /opt
实验2:共享一个目录,需要用户名和密码,可读可写。
编辑配置文件:
[[email protected] ~]# vim /etc/samba/smb.conf
修改1:security = user
修改2:在最后面添加一个共享模块:
[share2]
comment=share for users
path=/tmp/share2
browseable=yes
writable=yes
public=no
创建共享目录share2,并随便写点东西:
[[email protected] ~]# mkdir /tmp/share2
[[email protected] ~]# chmod 777 /tmp/share2
[[email protected] ~]# cd /tmp/share2
[[email protected] share2]# echo "111">1.txt
[[email protected] share2]# mkdir 222
增加一个系统用户:
[[email protected] ~]# useradd smbuser1
增加一个同名的虚拟用户:
[[email protected] ~]# pdbedit -a smbuser1
new password:
retype new password:
Unix username: smbuser1
NT username:
Account Flags: [U ]
User SID: S-1-5-21-395755363-1230348726-1447660675-1000
Primary Group SID: S-1-5-21-395755363-1230348726-1447660675-513
Full Name:
Home Directory: \\cp3\smbuser1
HomeDir Drive:
Logon Script:
Profile Path: \\cp3\smbuser1\profile
Domain: CP3
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: never
Kickoff time: never
Password last set: 六, 11 3月 2017 00:57:49 CST
Password can change: 六, 11 3月 2017 00:57:49 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[[email protected] ~]#
重启samba:
[[email protected] ~]# /etc/init.d/smb restart
关闭 SMB 服务: [确定]
启动 SMB 服务: [确定]
验证Windows访问samba:
win + R 打开“运行”,输入\\192.168.147.139,确定,输入用户名密码即可登录。
在share2中新建文件成功,因为此时的共享设置的是可写。
验证Linux访问samba:
[[email protected] share2]# smbclient -Usmbuser1 //192.168.147.139/share2
Enter smbuser1‘s password:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 3.6.23-36.el6_8]
smb: \> ls
. D 0 Sat Mar 11 01:04:19 2017
.. D 0 Sat Mar 11 01:03:04 2017
新建文本文档.txt A 0 Sat Mar 11 01:04:19 2017
222 D 0 Sat Mar 11 01:03:34 2017
1.txt 4 Sat Mar 11 01:03:30 2017
35292 blocks of size 524288. 30480 blocks available
smb: \>
这时候的共享是可写的。
同样可以使用挂载的方式访问共享目录:
[[email protected] share2]# umount /opt
[[email protected] share2]# mount -t cifs -o username=smbuser1,password=14721236 //192.168.147.139/share2 /opt/
[[email protected] share2]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_cp1-lv_root 18G 1.5G 15G 10% /
tmpfs 947M 0 947M 0% /dev/shm
/dev/sda1 485M 31M 429M 7% /boot
//192.168.147.139/share2 18G 1.5G 15G 10% /opt