Samba服务搭建

准备工作(服务器端):

  • 服务器端的IP地址为:192.168.234.11
  • 关闭防火墙
[root@wyz-11 ~]# systemctl stop firewalld
[root@wyz-11 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@wyz-11 ~]# setenforce 0
  • 修改selinux的状态为disabled
[root@wyz-11 ~]# vim /etc/selinux/config
SELINUX=disabled
  • DNS服务器禁用填公网IP
[root@wyz-11 ~]# vim /etc/resolv.conf	//修改DNS配置文件
# Generated by NetworkManager
#nameserver 8.8.8.8				//注释掉所有IP为公网地址的名称服务器
#nameserver 114.114.114.114
#nameserver 192.168.234.2
  • 安装Samba,且启动服务并设置开机自启
[root@wyz-11 ~]# yum install -y samba*
[root@wyz-11 ~]# systemctl start smb nmb
[root@wyz-11 ~]# systemctl enable smb nmb
Created symlink from /etc/systemd/system/multi-user.target.wants/smb.service to /usr/lib/systemd/system/smb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/nmb.service to /usr/lib/systemd/system/nmb.service.
[root@wyz-11 ~]# 

准备工作(客户端):

  • 客户端的IP地址为:192.168.234.22
  • 安装Samba-client
[root@wyz-22 ~]# yum install -y samba-client

1.搭建匿名用户共享服务器(任何用户都能登录,且只读)

服务器端

  • 创建共享目录/samba
[root@wyz-11 ~]# mkdir /samba
  • 修改配置文件
[root@wyz-11 ~]# vim /etc/samba/smb.conf		//samba服务器配置

[global]
		...
		load printers = yes
        cups options = raw
        map to guest = Bad User      //添加此行内容
...
[samba]							//添加共享
        comment = wyz 		//说明信息
        path = /samba 			//共享文件的路径       
        browseable = Yes 		//共享文件可见
        guest ok = Yes  		//所有人均可访问共享
        writable = No  			//读写权限  
        public = Yes  			//开启匿名访问

[root@wyz-11 ~]# testparm 			//检测配置文件是否修改有误
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[samba]"
Loaded services file OK.
Server role: ROLE_STANDALONE

客户端

  • 查看共享目录
[root@wyz-22 ~]# smbclient -L 192.168.234.11 -U "Bad User"
Enter SAMBA\Bad User's password:  		//匿名访问无需输入密码直接回车
OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	IPC$            IPC       IPC Service (Samba 4.6.2)
	samba           Disk      wyz
OS=[Windows 6.1] Server=[Samba 4.6.2]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------
	SAMBA                WYZ
[root@wyz-22 ~]#
  • 交互式数据访问
[root@wyz-22 ~]# smbclient //192.168.234.11/samba -U "Bad User"
Enter SAMBA\Bad User's password:  		//匿名访问无需输入密码直接回车
OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> 
  • 手动挂载
[root@wyz-22 ~]# mkdir /samba	//创建挂载位置
[root@wyz-22 ~]# mount -t cifs //192.168.234.11/samba /samba -o username="Bad User"     //将192.168.234.11的/samba目录挂载到本地的Samba目录
[root@wyz-22 ~]# df -Th
文件系统               类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root  xfs        47G  3.5G   44G    8% /
devtmpfs               devtmpfs  897M     0  897M    0% /dev
tmpfs                  tmpfs     912M     0  912M    0% /dev/shm
tmpfs                  tmpfs     912M  9.0M  903M    1% /run
tmpfs                  tmpfs     912M     0  912M    0% /sys/fs/cgroup
/dev/sda1              xfs      1014M  179M  836M   18% /boot
tmpfs                  tmpfs     183M   12K  183M    1% /run/user/42
tmpfs                  tmpfs     183M     0  183M    0% /run/user/0
/dev/sr0               iso9660   3.8G  3.8G     0  100% /mnt
//192.168.234.11/samba cifs       48G  1.1G   47G    3% /samba
[root@wyz-22 ~]# 

测试

  • 在服务器端创建文件,在客户端查看
//在服务器上创建文件
[root@wyz-11 ~]# touch /samba/text
[root@wyz-11 ~]# echo "Hello WYZ" >> /samba/text 
[root@wyz-11 ~]# 

//在客户端上查看
[root@wyz-22 ~]# ll /samba/
总用量 4
-rw-r--r--. 1 root root 10 416 15:04 text
[root@wyz-22 ~]# cat /samba/text 
Hello WYZ
[root@wyz-22 ~]# 
  • 由于服务器配置为只读,则客户端无法在其中创建文件和目录
[root@wyz-22 ~]# mkdir /samba/wyz
mkdir: 无法创建目录"/samba/wyz": 权限不够
[root@wyz-22 ~]# touch /samba/123
touch: 无法创建"/samba/123": 权限不够
[root@wyz-22 ~]# 

拓展项(客户端自动挂载)

//先卸载之前的手动挂载的目录
[root@wyz-22 ~]# umount /samba/
[root@wyz-22 ~]# vim /etc/fstab 
...
//192.168.234.11/samba /samba cifs defaults,username='BadUser' 0 0       //添加行

[root@wyz-22 ~]# mount -a
[root@wyz-22 ~]# df -Th
文件系统               类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root  xfs        47G  3.5G   44G    8% /
devtmpfs               devtmpfs  897M     0  897M    0% /dev
tmpfs                  tmpfs     912M     0  912M    0% /dev/shm
tmpfs                  tmpfs     912M  8.9M  903M    1% /run
tmpfs                  tmpfs     912M     0  912M    0% /sys/fs/cgroup
/dev/sda1              xfs      1014M  179M  836M   18% /boot
tmpfs                  tmpfs     183M   12K  183M    1% /run/user/42
tmpfs                  tmpfs     183M     0  183M    0% /run/user/0
/dev/sr0               iso9660   3.8G  3.8G     0  100% /mnt
//192.168.234.11/samba cifs       48G  1.1G   47G    3% /samba

2.搭建用户认证共享服务器

服务器端

  • 创建用户wyz,映射共享目录
[root@wyz-11 ~]# useradd -M wyz		//创建用户wyz
[root@wyz-11 ~]# smbpasswd -a wyz 	//为wyz用户创建smb共享密码
New SMB password:
Retype new SMB password:
Added user wyz.

[root@wyz-11 ~]# echo 'wyz = share' > /etc/samba/smbusers 	//映射wyz用户为share用户
[root@wyz-11 ~]# mkdir /share		//创建共享目录
[root@wyz-11 ~]# chown -R wyz.wyz /share/	//修改共享目录的所属
  • 修改配置文件
[root@wyz-11 ~]# vim /etc/samba/smb.conf		//samba服务器配置
[global]
        workgroup = SAMBA
        security = user
        username map = /etc/samba/smbusers      //添加此行内容
		...
		
[share]							//添加共享
        comment = This is samba //说明信息
        path = /share			//共享文件的路径       
        browseable = Yes 		//共享文件可见
        guest ok = Yes  		//所有人均可访问共享
        writable = Yes  		//读写权限
        write list = share		//允许写入的用户
        public = Yes  			//开启匿名访问

[root@wyz-11 ~]# testparm 			//检测配置文件是否修改有误
Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[share]"
Loaded services file OK.
Server role: ROLE_STANDALONE

客户端

  • 查看共享目录
[root@wyz-22 ~]# smbclient -L 192.168.234.11 -U share
Enter SAMBA\share's password: 
Domain=[WYZ] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Sharename       Type      Comment
	---------       ----      -------
	print$          Disk      Printer Drivers
	IPC$            IPC       IPC Service (Samba 4.6.2)
	share           Disk      This is samba
	wyz             Disk      Home Directories
Domain=[WYZ] OS=[Windows 6.1] Server=[Samba 4.6.2]

	Server               Comment
	---------            -------

	Workgroup            Master
	---------            -------
	SAMBA                WYZ
  • 交互式数据访问
[root@wyz-22 ~]# smbclient //192.168.234.11/share -U share
Enter SAMBA\share's password: 
Domain=[WYZ] OS=[Windows 6.1] Server=[Samba 4.6.2]
smb: \> 
  • 手动挂载
[root@wyz-22 ~]# mkdir /share		//创建本地挂载目录
[root@wyz-22 ~]# mount -t cifs //192.168.234.11/share /share/ -o username=share,password=1234  	//将192.168.234.11的/samba目录挂载到本地的Samba目录,用户为share密码为1234
[root@wyz-22 ~]# df -Th
文件系统               类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root  xfs        47G  3.5G   44G    8% /
devtmpfs               devtmpfs  897M     0  897M    0% /dev
tmpfs                  tmpfs     912M     0  912M    0% /dev/shm
tmpfs                  tmpfs     912M  8.9M  903M    1% /run
tmpfs                  tmpfs     912M     0  912M    0% /sys/fs/cgroup
/dev/sda1              xfs      1014M  179M  836M   18% /boot
tmpfs                  tmpfs     183M   12K  183M    1% /run/user/42
tmpfs                  tmpfs     183M     0  183M    0% /run/user/0
/dev/sr0               iso9660   3.8G  3.8G     0  100% /mnt
//192.168.234.11/share cifs       48G  1.1G   47G    3% /share

测试

  • 在服务器端创建文件,在客户端查看
//服务器端创建文件
[root@wyz-11 ~]# touch /share/text1
[root@wyz-11 ~]# echo 'HELLO YiZhen_W'>/share/text1
[root@wyz-11 ~]# cat /share/text1
HELLO YiZhen_W
[root@wyz-11 ~]# 

//客户端查看
[root@wyz-22 ~]# ll /share/
总用量 4
-rw-r--r--. 1 root root 15 416 16:26 text1
[root@wyz-22 ~]# cat /share/text1 
HELLO YiZhen_W
[root@wyz-22 ~]# 

//客户端创建文件,并查看文件属性
[root@wyz-22 ~]# touch /share/wyz1
[root@wyz-22 ~]# echo 'I am wyz'> /share/wyz1
[root@wyz-22 ~]# mkdir /share/wyz2
[root@wyz-22 ~]# cat /share/wyz1
I am wyz
[root@wyz-22 ~]# ll /share/
总用量 8
-rw-r--r--. 1 root root 15 416 16:26 text1	 //在服务器上创建的文件的所属都为root
-rw-r--r--. 1 1000 1000  9 416 16:31 wyz1	 //在客户端上创建的文件,由于客户端没有uid=1000,gid=1000的用户,所以显示所属为1000
drwxr-xr-x. 2 1000 1000  0 416 16:31 wyz2	 //在客户端上创建的目录,由于客户端没有uid=1000,gid=1000的用户,所以显示所属为1000
[root@wyz-22 ~]# 


//在服务器上查看客户端创建的文件
[root@wyz-11 ~]# ll /share/
total 8
-rw-r--r--. 1 root root 15 Apr 16 04:26 text1
-rw-r--r--. 1 wyz  wyz   9 Apr 16 04:29 wyz1
drwxr-xr-x. 2 wyz  wyz   6 Apr 16 04:29 wyz2

拓展项(客户端自动挂载)

//先卸载之前的手动挂载的目录
[root@wyz-22 ~]# umount /samba/
[root@wyz-22 ~]# vim /etc/fstab 
...
//192.168.234.11/share /share cifs defaults,username=share,password=1234 0 0    	//添加行

[root@wyz-22 ~]# mount -a
[root@wyz-22 ~]# df -Th
文件系统               类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root  xfs        47G  3.5G   44G    8% /
devtmpfs               devtmpfs  897M     0  897M    0% /dev
tmpfs                  tmpfs     912M     0  912M    0% /dev/shm
tmpfs                  tmpfs     912M  8.9M  903M    1% /run
tmpfs                  tmpfs     912M     0  912M    0% /sys/fs/cgroup
/dev/sda1              xfs      1014M  179M  836M   18% /boot
tmpfs                  tmpfs     183M   12K  183M    1% /run/user/42
tmpfs                  tmpfs     183M     0  183M    0% /run/user/0
/dev/sr0               iso9660   3.8G  3.8G     0  100% /mnt
//192.168.234.11/share cifs       48G  1.1G   47G    3% /share
[root@wyz-22 ~]# 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值