文章目录
1. 简述 Samba 服务
对于我而言,最简单的理解就是,我可以通过部署 Samba 服务来实现 Linux 端和 Windows 端的文件共享,可以免去对第三方软件工具的依赖,直接进行数据交互;尤其是将 Windows 端的一些数据向 Linux 端进行同步或者备份时,Samba 服务还是非常还用的。
2. 配置 Samba 服务端
步骤1
:关闭系统防火墙(也可启用防火墙放行相关端口)。
[root@mylinux ~]# systemctl stop firewalld.service && systemctl disable firewalld.service
[root@mylinux ~]# systemctl list-unit-files |grep firewalld && firewall-cmd --state
firewalld.service disabled
not running
步骤2
:关闭 SELINUX。
[root@mylinux ~]# cat /etc/selinux/config |grep -vE "^#|^$"
SELINUX=disabled //此项修改为 “disabled” 即可。
SELINUXTYPE=targeted
步骤3
:配置本地 YUM 源。
[root@mylinux ~]# mkdir /iso
[root@mylinux ~]# mkdir /mnt/cdrom
[root@mylinux ~]# mkdir /etc/yum.repos.d/repo_backup
[root@mylinux ~]# mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/repo_backup/
[root@mylinux ~]# vim /etc/yum.repos.d/local.repo
> > > local.repo 内容如下所示 > > >
[local]
name=local
baseurl=file:///mnt/cdrom
enabled=1
gpgcheck=0
> > > EOF > > >
[root@mylinux ~]# mount -o loop /iso/CentOS-7.6-x86_64-DVD-1810.iso /mnt/cdrom/
[root@mylinux ~]# yum clean all
[root@mylinux ~]# yum repolist
步骤4
:安装 samba 和 samba-client 软件包。
[root@mylinux ~]# yum search samba --showduplicates
[root@mylinux ~]# yum -y install samba samba-client
[root@mylinux ~]# rpm -qa |grep -i samba
samba-client-4.8.3-4.el7.x86_64
samba-libs-4.8.3-4.el7.x86_64
samba-client-libs-4.8.3-4.el7.x86_64
samba-common-tools-4.8.3-4.el7.x86_64
samba-common-libs-4.8.3-4.el7.x86_64
samba-4.8.3-4.el7.x86_64
samba-common-4.8.3-4.el7.noarch
步骤5
:创建共享目录并赋予最高权限。
[root@mylinux ~]# mkdir -p /samba_share/share_folder_1
[root@mylinux ~]# mkdir /samba_share/share_folder_2
[root@mylinux ~]# chmod -R 777 /samba_share/ //此处若不赋予权限,则通过 windows 访问共享目录时只能读取数据,而无法创建文件夹以及向文件中写入数据。
[root@mylinux ~]# ls -l /samba_share/
总用量 0
drwxrwxrwx 3 root root 41 6月 3 23:42 share_folder_1
drwxrwxrwx 3 root root 24 6月 3 23:38 share_folder_2
步骤6
:编辑 smb.conf 文件将目录共享出去。
[root@mylinux ~]# vim /etc/samba/smb.conf
> > > 要将刚刚创建的目录共享出去,需要在 smb.conf 文件末尾添加如下内容 > > >
[share_folder_1]
comment = This is a shared directory
path = /samba_share/share_folder_1
browseable = yes
writable = yes
[share_folder_2]
comment = This is a shared directory
path = /samba_share/share_folder_2
browseable = yes
writable = yes
> > > EOF > > >
[root@mylinux ~]# cat /etc/samba/smb.conf
# See smb.conf.example for a more detailed config file or
# read the smb.conf manpage.
# Run 'testparm' to verify the config is correct after
# you modified it.
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
#[homes]
# comment = Home Directories
# valid users = %S, %D%w%S
# browseable = No
# read only = No
# inherit acls = Yes
[printers]
comment = All Printers
path = /var/tmp
printable = Yes
create mask = 0600
browseable = No
[print$]
comment = Printer Drivers
path = /var/lib/samba/drivers
write list = @printadmin root
force group = @printadmin
create mask = 0664
directory mask = 0775
[share_folder_1]
comment = This is a shared directory
path = /samba_share/share_folder_1
browseable = yes
writable = yes
[share_folder_2]
comment = This is a shared directory
path = /samba_share/share_folder_2
browseable = yes
writable = yes
步骤7
:创建 samba 用户。
[root@mylinux ~]# useradd sambauser
[root@mylinux ~]# smbpasswd -a sambauser
New SMB password:
Retype new SMB password:
Added user sambauser.
步骤8
:重启 smb.service 和 nmb.service 服务并设置为开机自启动。
[root@mylinux ~]# systemctl enable smb.service nmb.service
[root@mylinux ~]# systemctl restart smb.service nmb.service
[root@mylinux ~]# systemctl status smb.service nmb.service //两个服务都应该为 “active” 状态。
3. 访问 Samba 服务端
步骤1
:从 windows 设备访问 samba 共享文件夹。
步骤2
:输入用户名和密码。
步骤3
:此时可以看到 Linux 服务端共享出的两个文件夹。
步骤4
:经测试可以在此共享文件夹下面创建、删除和修改文件,具体测试过程比较简单,不做过多描述。