文章目录
前言
网络文件系统提供文件共享的一种方式,极大的节省了内存资源。主要依托于samba和nfs服务
一、samba基本信息
服务启动脚本: | smb.service |
---|---|
主配置目录: | /etc/samba |
主配置文件: | /etc/smb.conf |
安全上下文: | samba_share_t |
二、samba客户端与服务端
1.服务端
需要下载的包: | samba,samba-common |
---|
2.客户端
需要下载的包: | samba-client |
---|
3.服务启用
samba服务启动:
systemctl enable --now smb
samba服务启用:
firewall-cmd --permanent --add-service=samba
firewall-cmd --reload
三、建立samba服务共享目录
%创建共享的时候系统selinux必须为开启状态,因为samba对selinux的要求比较高
mkdir /westosdir
touch /westosdir/westosfile{1..5}
semanage fcontext -a -t samba_share_t '/westosdir(/.*)?' %更改共享目录的安全上下文
restorecon -RvvF /westosdir/ %使上述更改生效
vim /etc/samba/smb.conf
[westosdir] %共享名称
comment = westosdir %共享说明
path = /westosdir %共享路径
systemctl restart smb
测试:
smbclient //172.25.254.20/westosdir -U westos %可以访问则表示成功
四、共享目录的挂载
samba服务不同于ftp服务的一点是samba服务所创建的共享目录可以直接挂载,如下所示:
[root@haha samba]# mount -o username=admin,password=westos //10.4.17.118/LEE /mnt %password是samba用户的密码
[root@haha samba]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 906392 0 906392 0% /dev
tmpfs 935412 0 935412 0% /dev/shm
tmpfs 935412 9768 925644 2% /run
tmpfs 935412 0 935412 0% /sys/fs/cgroup
/dev/mapper/rhel-root 7350272 4842756 2507516 66% /
/dev/vda1 506528 223056 283472 45% /boot
tmpfs 187080 1180 185900 1% /run/user/42
tmpfs 187080 24 187056 1% /run/user/0
//10.4.17.118/LEE 7350272 4842756 2507516 66% /mnt
[root@haha samba]# cd /mnt
[root@haha mnt]# ls %可以看到共享目录下的文件
file1
五、autofs+samba自动挂载
当切换到/mnt/samba挂载目录时,共享文件目录会自动挂载到该挂载点,该挂载点闲置时在规定时间内自动卸载,如下所示:
[root@haha Desktop]# dnf install autofs -y
[root@haha Desktop]# vim /etc/auto.master
8 /mnt /etc/auto.samba %/mnt为挂载点,后面的是挂载的配置文件,默认不存在,需要自己编写
[root@haha Desktop]# vim /etc/auto.samba
1 samba -fstype=cifs,username=lee,password=westos ://10.4.17.118/LEE
[root@haha Desktop]# systemctl restart autofs.service
[root@haha Desktop]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 906392 0 906392 0% /dev
tmpfs 935412 0 935412 0% /dev/shm
tmpfs 935412 9784 925628 2% /run
tmpfs 935412 0 935412 0% /sys/fs/cgroup
/dev/mapper/rhel-root 7350272 4809828 2540444 66% /
/dev/vda1 506528 223056 283472 45% /boot
tmpfs 187080 1180 185900 1% /run/user/42
tmpfs 187080 20 187060 1% /run/user/0
[root@haha Desktop]# cd /mnt
[root@haha mnt]# cd samba/
[root@haha samba]# df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 906392 0 906392 0% /dev
tmpfs 935412 0 935412 0% /dev/shm
tmpfs 935412 9784 925628 2% /run
tmpfs 935412 0 935412 0% /sys/fs/cgroup
/dev/mapper/rhel-root 7350272 4809992 2540280 66% /
/dev/vda1 506528 223056 283472 45% /boot
tmpfs 187080 1180 185900 1% /run/user/42
tmpfs 187080 20 187060 1% /run/user/0
//10.4.17.118/LEE 7350272 4809992 2540280 66% /mnt/samba
[root@haha samba]# cd
[root@haha ~]# vim /etc/autofs.conf
15 timeout = 300 %设置等待时间为300秒,可更改
[root@haha ~]# systemctl restart autofs.service
六、系统目录共享
当需要共享系统目录时,例如要共享/mnt目录时,此时不可以更改系统目录的安全上下文,否则会导致系统其他部分出问题,需要更改其bool值
[root@haha ~]# vim /etc/samba/smb.conf
315 [LEE]
316 comment = westosdir
317 path = /mnt
[root@haha ~]# 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@haha ~]# setsebool -P samba_export_all_ro on
[root@haha ~]# systemctl restart smb.service
[root@haha ~]# smbclient //10.4.17.118/LEE -U lee %能访问则表示成功
Enter MYGROUP\lee's password:
Try "help" to get a list of possible commands.
smb: \> ls
. D 0 Thu Dec 3 13:25:24 2020
.. D 0 Thu Dec 3 13:25:24 2020
20480 blocks of size 1024. 0 blocks available
smb: \> exit
七、NFS(Net File System)
是区别于samba服务的另一种网络文件共享方式,基本信息如下:
nfs-utils | 安装包 |
---|---|
nfs-server | 服务脚本 |
/etc/exports | 共享配置文件 |
nfs的启用:
systemctl start nfs-server
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --permanent --add-service=nfs
firewall-cmd --reload
访问方法:
[root@dns_node1 ~]# showmount -e 服务器Ip %出现下述说明时表示访问成功
Export list for 服务器Ip: