使用Samba或NFS实现文件共享

//重启服务并加入开机启动项

[root@localhost ~]# systemctl restart smb.service
[root@localhost ~]# systemctl enable smb.service
Created symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.

//关闭iptables防火墙,添加到firewalld防火墙

[root@localhost ~]# iptables -F
[root@localhost ~]# iptables-save
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=samba
success
[root@localhost ~]# firewall-cmd --reload
success

//查看samba共享了哪些目录,可以使用smbclient命令来查看,-U:指定用户名称(用哪位用户挂载了Samba服务,就用哪位用户的身份进行查看);-L:列出共享清单

[root@localhost ~]# smbclient -U linuxprobe -L 192.168.95.128
Enter SAMBAlinuxprobe's password:


    Sharename       Type      Comment
    ---------       ----      -------
    database        Disk      Do not arbitrarily modify the database file
    IPC$            IPC       IPC Service (Samba 4.14.5)
SMB1 disabled -- no workgroup available

Linux挂载共享
//客户端操作
[root@localhost ~]# yum -y install cifs-utils
[root@localhost ~]# mkdir /database
[root@localhost ~]# mount -t cifs -o username=linuxprobe,password=Admin@123456,vers=2.0 //192.168.95.128/database /database
[root@localhost ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/sda3                  184G  5.6G  179G   4% /
devtmpfs                    16G     0   16G   0% /dev
tmpfs                       16G   88K   16G   1% /dev/shm
tmpfs                       16G  9.0M   16G   1% /run
tmpfs                       16G     0   16G   0% /sys/fs/cgroup
/dev/sda1                  297M  152M  146M  52% /boot
tmpfs                      3.2G  8.0K  3.2G   1% /run/user/0
//192.168.95.128/database   98G  5.4G   93G   6% /database

//设置开机自启
[root@localhost ~]# vim auth.smb
[root@localhost ~]# chmod 600 auth.smb
[root@localhost ~]# vim /etc/fstab
[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Mar 11 08:28:47 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=924eec88-bdb2-4b5e-b3d2-e1b2e041f475 /                       xfs     defaults        0 0
UUID=14a09840-c0fe-4287-b65f-82347fdfb46b /boot                   xfs     defaults        0 0
UUID=a140d994-6f24-4d0f-9517-5c04a9209187 swap                    swap    defaults        0 0
//192.168.95.128/database                 /database               cifs  credentials=/root/auth.smb        0 0
[root@localhost ~]# mount -a
[root@localhost ~]# cd /database/
[root@localhost database]# ls
redme.txt //Windows共享时写入的文件

NFS(网络文件系统)

//将服务程序添加到firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables-save
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=nfs
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=rpc-bind
success
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-service=mountd
success

//建立NFS文件共享的目录,并设置足够的权限
[root@localhost ~]# mkdir /nfsfile
[root@localhost ~]# chmod -R 777 /nfsfile/
[root@localhost ~]# echo "Welcome to linuxprobe.com" > /nfsfile/readme

NFS服务程序的配置文件为/etc/exports,默认情况下里面没有任何内容

[root@localhost ~]# vim /etc/exports
/nfsfile 192.168.95.*(rw,sync,root_squash)
//重启服务加入开机自启
[root@localhost ~]# systemctl restart rpcbind
[root@localhost ~]# systemctl enable rpcbind
[root@localhost ~]# systemctl restart nfs-server
[root@localhost ~]# systemctl enable nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.

//使用showmount命令查询NFS服务器的远程共享信息
-e:显示NFS服务器的共享列表
-a:显示本机挂载的文件资源的情况
-v:显示版本号

[root@localhost ~]# showmount -e 192.168.95.128
Export list for 192.168.95.128:
/nfsfile 192.168.95.*

//客户端操作
[root@localhost ~]# showmount -e 192.168.95.128
Export list for 192.168.95.128:
/nfsfile 192.168.95.*

[root@localhost ~]# mkdir /nfsfiles
[root@localhost ~]# mount -t nfs 192.168.95.128:/nfsfile /nfsfiles
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda3                184G  5.6G  179G   4% /
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G   88K   16G   1% /dev/shm
tmpfs                     16G  9.0M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/sda1                297M  152M  146M  52% /boot
tmpfs                    3.2G   12K  3.2G   1% /run/user/0
192.168.95.128:/nfsfile   98G  5.4G   93G   6% /nfsfiles

[root@localhost ~]# vim /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sat Mar 11 08:28:47 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=924eec88-bdb2-4b5e-b3d2-e1b2e041f475 /                       xfs     defaults        0 0
UUID=14a09840-c0fe-4287-b65f-82347fdfb46b /boot                   xfs     defaults        0 0
UUID=a140d994-6f24-4d0f-9517-5c04a9209187 swap                    swap    defaults        0 0
192.168.95.128:/nfsfile                   /nfsfiles               nfs     defaults        0 0

autofs自动挂载服务在用户需要使用该文件系统时采取动态挂载,节约网络资源和服务器的硬件资源
autofs主配置文件种写"挂载目录 子配置文件"的格式进行填写。挂载目录是设备挂载的上一级目录。子配置文件需要自定义
[root@localhost ~]# vim /etc/auto.master
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc   /etc/auto.misc
/media  /etc/iso.misc

//子配置文件中,应按照"挂载目录 挂载文件类型及权限 :设备名称"格式进行填写
[root@localhost ~]# vim /etc/iso.misc
ios -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
[root@localhost ~]# systemctl start autofs.service
[root@localhost ~]# systemctl enable autofs.service
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.