这里写自定义目录标题
- 本地环境:CentOS8 x64
- 内核版本:4.18.0-147.3.1.el8_1.x86_64
- 前提:Linux与Windows能ping通!!!
1. Linux–Windows文件共享
Samba是基于SMB协议,由客户端和服务器组成的共享软件,可实现Linux-Windows,Linux-Linux之间的文件共享。
1.1 软件的安装
yum install samba
CentOS-8 - AppStream 5.1 kB/s | 4.3 kB 00:00
CentOS-8 - AppStream 3.2 MB/s | 7.0 MB 00:02
CentOS-8 - Base 2.2 kB/s | 3.9 kB 00:01
CentOS-8 - Base 411 kB/s | 2.2 MB 00:05
CentOS-8 - Extras 2.0 kB/s | 1.5 kB 00:00
CentOS-8 - Extras 1.7 kB/s | 5.9 kB 00:03
Docker CE Stable - x86_64 10 kB/s | 3.5 kB 00:00
Docker CE Stable - x86_64 21 kB/s | 23 kB 00:01
Extra Packages for Enterprise Linux 8 - x86_64 2.9 kB/s | 8.5 kB 00:02
Extra Packages for Enterprise Linux 8 - x86_64 686 kB/s | 6.7 MB 00:09
依赖关系解决。
=====================================================================================================
软件包 架构 版本 仓库 大小
=====================================================================================================
安装:
samba x86_64 4.10.4-101.el8_1 BaseOS 739 k
安装依赖关系:
samba-common-tools x86_64 4.10.4-101.el8_1 BaseOS 469 k
samba-libs x86_64 4.10.4-101.el8_1 BaseOS 185 k
事务概要
=====================================================================================================
安装 3 软件包
总下载:1.4 M
安装大小:4.1 M
确定吗?[y/N]: y
下载软件包:
(1/3): samba-libs-4.10.4-101.el8_1.x86_64.rpm 391 kB/s | 185 kB 00:00
(2/3): samba-common-tools-4.10.4-101.el8_1.x86_64.rpm 552 kB/s | 469 kB 00:00
(3/3): samba-4.10.4-101.el8_1.x86_64.rpm 776 kB/s | 739 kB 00:00
-----------------------------------------------------------------------------------------------------
总计 827 kB/s | 1.4 MB 00:01
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : samba-libs-4.10.4-101.el8_1.x86_64 1/3
安装 : samba-common-tools-4.10.4-101.el8_1.x86_64 2/3
安装 : samba-4.10.4-101.el8_1.x86_64 3/3
运行脚本: samba-4.10.4-101.el8_1.x86_64 3/3
验证 : samba-4.10.4-101.el8_1.x86_64 1/3
验证 : samba-common-tools-4.10.4-101.el8_1.x86_64 2/3
验证 : samba-libs-4.10.4-101.el8_1.x86_64 3/3
已安装:
samba-4.10.4-101.el8_1.x86_64 samba-common-tools-4.10.4-101.el8_1.x86_64
samba-libs-4.10.4-101.el8_1.x86_64
完毕!
1.2 Samba配置文件的配置
cd /etc/samba/
[root@bogon samba]# ls
lmhosts smb.conf smb.conf.example
[root@bogon samba]# cp smb.conf smb.conf_bak
[root@bogon samba]# vim 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
[SambaHomes]
comment = MySMB
path = /home/smbShare
public = no
writable = yes
~
[global]中的这四个参数和配置文件中的[Printer]都是和打印机有关的,可以删掉,不影响
printing = cups
printcap name = cups
load printers = yes
cups options = raw
1.3 共享目录的配置
1.2中的目录是要真实存在的,所以我们要对该目录进行创建和配置,其中,配置主要是配置权限,以及SELinux安全上下文策略。
path = /home/smbShare 为避免麻烦,目录我们这里不在根目录/root 下创建。另外,配置文件中security = user我们选择的是user,所以要创建一个用于访问共享资源的账户信息
[root@bogon samba]# useradd xiaoli
[root@bogon samba]# id xiaoli
uid=1001(xiaoli) gid=1001(xiaoli) 组=1001(xiaoli)
[root@bogon samba]# pdbedit -a -u xiaoli
new password:
retype new password:
Unix username: xiaoli
NT username:
Account Flags: [U ]
User SID: S-1-5-21-3738100299-931708920-725309089-1000
Primary Group SID: S-1-5-21-3738100299-931708920-725309089-513
Full Name:
Home Directory: \\bogon\xiaoli
HomeDir Drive:
Logon Script:
Profile Path: \\bogon\xiaoli\profile
Domain: BOGON
Account desc:
Workstations:
Munged dial:
Logon time: 0
Logoff time: 三, 06 2月 2036 23:06:39 CST
Kickoff time: 三, 06 2月 2036 23:06:39 CST
Password last set: 六, 23 5月 2020 17:07:42 CST
Password can change: 六, 23 5月 2020 17:07:42 CST
Password must change: never
Last bad password : 0
Bad password count : 0
Logon hours : FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
[root@bogon samba]#
[root@bogon samba]# mkdir /home/smbShare
[root@bogon samba]# chown -Rf xiaoli:xiaoli /home/smbShare
[root@bogon samba]# sem
semanage semodule_expand semodule_package
semodule semodule_link semodule_unpackage
[root@bogon samba]# semanage fcontext -a -t samba_share_t /home/smbShare
[root@bogon samba]# restorecon -Rv /home/smbShare/
Relabeled /home/smbShare from unconfined_u:object_r:user_home_dir_t:s0 to unconfined_u:object_r:samba_share_t:s0
然后,设置SELinux服务于策略
[root@bogon samba]# getsebool -a |grep samba
samba_create_home_dirs --> off
samba_domain_controller --> off
samba_enable_home_dirs --> off
samba_export_all_ro --> off
samba_export_all_rw --> off
samba_load_libgfapi --> off
samba_portmapper --> off
samba_run_unconfined --> off
samba_share_fusefs --> off
samba_share_nfs --> off
sanlock_use_samba --> off
tmpreaper_use_samba --> off
use_samba_home_dirs --> off
virt_use_samba --> off
[root@bogon samba]# sets
setsebool setsid
[root@bogon samba]# setsebool samba_enable_home_dirs on
[root@bogon samba]#
最后,把Samba服务加入到防火墙规则里。
[root@bogon samba]# firewall-cmd --permanent --zone=public --add-service=samba
success
[root@bogon samba] firewall-cmd --reload
[root@bogon samba]# systemctl restart sm
smartcard.target smartd.service smb.service
[root@bogon samba]# systemctl restart smb.service
[root@bogon samba]# systemctl enable smb.service
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.
[root@bogon samba]#
现在,我们可以检验效果了 。在Windows中,按Win+E组合键打开资源管理器,输入\虚拟机ip,回车。
虚拟机IP可输入 ifconfig查看。
2. Linux–Linux文件共享
2.1 客户端软件安装配置
前提还是一样,用做Samba服务端的机器要和客户端能ping通。服务器安装好Samba。
2.1.1 客户端安装支持文件共享服务的软件包
[root@bogon samba]# yum install cifs-utils
Loaded plugins: langpacks, product-id, subscription-manager
rhel | 4.1 kB 00:00
Resolving Dependencies
--> Running transaction check
---> Package cifs-utils.x86_64 0:6.2-6.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
cifs-utils x86_64 6.2-6.el7 rhel 83 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 83 k
Installed size: 174 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : cifs-utils-6.2-6.el7.x86_64 1/1
Verifying : cifs-utils-6.2-6.el7.x86_64 1/1
Installed:
cifs-utils.x86_64 0:6.2-6.el7
Complete!
按Samba服务的用户名、密码、共享域的顺序将相关信息写入到一个认证文件中。且将这个认证文件的权限修改为600(仅root管理员才能够读写)。
[root@bogon ~]# vim auth.smb
username=xiaohong
password=passwd
domain=MYGROUP
[root@bogon ~]# chmod -Rf 600 auth.smb
接下来,在客户端上创建一个用于挂载共享资源目录,并将挂载信息写到/etc/fstab中,确保系统重启后依然有效。
[root@bogon samba]# mkdir/home/smbShareDict
[root@bogon samba]# echo "//172.25.0.11/SambaHomes /home/smbShareDict cifs credentials=/root/auth.smb 0 0" >> /etc/fstab
[root@bogon samba] mount -a
[root@bogon samba] df -h
注:如果这里挂载失败 ,报地址错误,就将服务端的Samba服务在防火墙中放行(~~) 在这里踩了坑 。。。
[root@bogon samba] firewall-cmd --permanent --zone=public --add-service=samba
此时,客户端已经成功挂载了Samba服务器的共享资源,已经可进入到挂载目录中了。