糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > 网络文件系统——上(samba NFS 实现网络共享文件)

网络文件系统——上(samba NFS 实现网络共享文件)

时间:2021-06-06 11:39:47

相关推荐

网络文件系统——上(samba NFS 实现网络共享文件)

目录

一、samba

1、samba安装和启用

2、创建samba服务共享目录

3、匿名用户无法访问内容,建立samba用户,samba用户访问成功

4、通过挂载方式,让用户访问

5、在客户端实现自动挂载和卸载(autofs+samba)

6、samba的访问控制及常用配置参数

7、samba的多用户挂载

二、NFS(Net File System)

1、安装

2、nfs配置,将共享目录共享给172.25.254.50,只读

3、nfs配置,将共享目录共享给172.25.254.50,可读写

4、自动挂载卸载nfs+autofs

5、指定用户身份

6、当挂载时使用超级用户,则沿用超级用户身份

一、samba

smb = Server Message Block windows系统共享文件时用到的协议smbcifs = Common Internet File System Linux系统共享文件时用到的协议cifs实际上smb和cifs是一回事

1、samba安装和启用

100为安装samba的服务器,50为测试主机

[root@westosa100 ~]# dnf install samba samba-common samba-client -y[root@westos_student50 Desktop]# dnf install samba samba-common samba-client -y[root@westosa100 ~]# systemctl enable --now smbCreated symlink /etc/systemd/system/multi-user.target.wants/smb.service → /usr/lib/systemd/system/smb.service.[root@westosa100 ~]# firewall-cmd --permanent --add-service=sambasuccess[root@westosa100 ~]# firewall-cmd --reload success

2、创建samba服务共享目录

[root@westosa100 ~]# mkdir /westosshare[root@westosa100 ~]# touch /westosshare/westosfile{1..5}[root@westosa100 ~]# semanage fcontext -a -t samba_share_t '/westosshare(/.*)?'[root@westosa100 ~]# restorecon -RvvF /westosshare/[root@westosa100 ~]# vim /etc/samba/smb.conf[westos_smb]comment = local westosdirpath = /westosshare[root@westosa100 ~]# systemctl restart smb.service [root@westos_student50 Desktop]# smbclient -L //172.25.254.100/westos_smbEnter SAMBA\root's password: Anonymous login successfulSharename TypeComment--------- -----------print$DiskPrinter Driverswestos_smbDisklocal westosdirIPC$ IPC IPC Service (Samba 4.11.2)SMB1 disabled -- no workgroup available

在50主机测试,共享成功

3、匿名用户无法访问内容,建立samba用户,samba用户访问成功

[root@westos_student50 Desktop]# smbclient //172.25.254.100/westos_smbEnter SAMBA\root's password: Anonymous login successfultree connect failed: NT_STATUS_ACCESS_DENIED[root@westosa100 ~]# id westosuid=1000(westos) gid=1000(westos) groups=1000(westos)[root@westosa100 ~]# smbpasswd -a westosNew SMB password:Retype new SMB password:Added user westos.[root@westosa100 ~]# pdbedit -Lwestos:1000:westos[root@westos_student50 Desktop]# smbclient //172.25.254.100/westos_smb -U westosEnter SAMBA\westos's password: Try "help" to get a list of possible commands.smb: \> ls.D 0 Sat Nov 27 14:43:31 .. D 0 Sat Nov 27 14:42:50 westosfile1N 0 Sat Nov 27 14:43:31 westosfile2N 0 Sat Nov 27 14:43:31 westosfile3N 0 Sat Nov 27 14:43:31 westosfile4N 0 Sat Nov 27 14:43:31 westosfile5N 0 Sat Nov 27 14:43:31 17814528 blocks of size 1024. 14078488 blocks available

4、通过挂载方式,让用户访问

[root@westos_student50 Desktop]# mount //172.25.254.100/westos_smb /mnt/ -o username=westos,password=westos[root@westos_student50 Desktop]# dfFilesystem 1K-blocksUsed Available Use% Mounted ondevtmpfs 1893192 0 1893192 0% /devtmpfs1921028 0 1921028 0% /dev/shmtmpfs1921028 18392 1902636 1% /runtmpfs1921028 0 1921028 0% /sys/fs/cgroup/dev/sda379127812 24645932 54481880 32% //dev/sda1 518816 220672 298144 43% /boot/dev/loop08238560 8238560 0 100% /var/www/html/sourcetmpfs384204 16 384188 1% /run/user/42tmpfs384204 24 384180 1% /run/user/0//172.25.254.100/westos_smb 17814528 3736040 14078488 21% /mnt[root@westos_student50 Desktop]# ls /mntwestosfile1 westosfile2 westosfile3 westosfile4 westosfile5

5、在客户端实现自动挂载和卸载(autofs+samba)

[root@westos_student50 Desktop]# dnf install autofs-1:5.1.4-40.el8.x86_64 -y[root@westos_student50 Desktop]# umount /mnt###将之前的挂载先卸掉[root@westos_student50 Desktop]# df Filesystem1K-blocksUsed Available Use% Mounted ondevtmpfs 1893192 0 1893192 0% /devtmpfs 1921028 0 1921028 0% /dev/shmtmpfs 1921028 18392 1902636 1% /runtmpfs 1921028 0 1921028 0% /sys/fs/cgroup/dev/sda3 79127812 23624204 55503608 30% //dev/sda1 518816 220672 298144 43% /boot/dev/loop0 8238560 8238560 0 100% /var/www/html/sourcetmpfs 384204 16 384188 1% /run/user/42tmpfs 384204 24 384180 1% /run/user/0[root@westos_student50 Desktop]# vim /etc/auto.master/smb /etc/auto.samba ####最终挂载点的上层目录自动挂载子策略文件[root@westos_student50 Desktop]# vim /etc/auto.sambawestos_smb-fstype=cifs,username=westos,password=westos ://172.25.254.100/westos_smb ##最终挂载点挂载参数 挂载资源[root@westos_student50 Desktop]# vim /etc/autofs.conftimeout = 3 ###默认300秒,资源闲置就会卸载,这里为了试验效果改为3s[root@westos_student50 Desktop]# systemctl restart autofs[root@westos_student50 Desktop]# cd /smb/westos_smb[root@westos_student50 westos_smb]# dfFilesystem 1K-blocksUsed Available Use% Mounted ondevtmpfs 1893192 0 1893192 0% /devtmpfs1921028 0 1921028 0% /dev/shmtmpfs1921028 18400 1902628 1% /runtmpfs1921028 0 1921028 0% /sys/fs/cgroup/dev/sda379127812 23622408 55505404 30% //dev/sda1 518816 220672 298144 43% /boot/dev/loop08238560 8238560 0 100% /var/www/html/sourcetmpfs384204 16 384188 1% /run/user/42tmpfs384204 24 384180 1% /run/user/0//172.25.254.100/westos_smb 17814528 3736032 14078496 21% /smb/westos_smb[root@westos_student50 westos_smb]# cd[root@westos_student50 ~]# dfFilesystem1K-blocksUsed Available Use% Mounted ondevtmpfs 1893192 0 1893192 0% /devtmpfs 1921028 0 1921028 0% /dev/shmtmpfs 1921028 18400 1902628 1% /runtmpfs 1921028 0 1921028 0% /sys/fs/cgroup/dev/sda3 79127812 23622408 55505404 30% //dev/sda1 518816 220672 298144 43% /boot/dev/loop0 8238560 8238560 0 100% /var/www/html/sourcetmpfs 384204 16 384188 1% /run/user/42tmpfs 384204 24 384180 1% /run/user/0

测试,进入设置的挂载点,实现了自动挂载,之后退出目录,等待三s,实现了自动卸载

6、samba的访问控制及常用配置参数

######当写到单独共享时之对此共享生效,当写到【GLOBAL】时对samba整体生效,下图是写到单独的共享目录westos_smb中的########

hosts allow 172.25.254.30 白名单hosts deny172.25.254.50 黑名单

#### samba的常用配置参数##### writable=yes ##可写 write list=westos ##指定用户可写 write list= +westos ##指定组成员可写write list=@westos##指定组成员可写valid users=lee ##指定访问用户,只有lee可以使用共享文件valid users= +lee|@lee##指定访问组 browseable=yes|no ##是否隐藏共享,隐藏后看不见共享文件,但可以使用map to guest=bad user ##写到全局设定中 guest ok=yes ##允许匿名用户访问 admin users=lee ##指定此共享的超级用户身份

测试就不做了,跟前面很多实验黑白名单,写权限等测试类似 ,思想是一样的

7、samba的多用户挂载

在客户端如果用普通的挂载方式 没有用过用户验证的人也可以访问samba服务 这显然是不合理的,因此使用多用户挂载的方式
实验前,先把匿名访问关掉,之后重启smb服务

[root@westos_student50 Desktop]# dnf install cifs-utils -y[root@westos_student50 Desktop]# vim /root/smbpassusername=westospassword=westos[root@westos_student50 Desktop]# mount -o credentials=/root/smbpass,sec=ntlmssp,multiuser //172.25.254.100/westos_smb /mnt/ #credentials=/root/smbpass 指定认证文件#sec=ntlmssp 指定认证类型#multiuser 支持多用户[root@westos_student50 Desktop]# su - westosLast login: Sun Nov 28 09:48:06 CST on pts/2[westos@westos_student50 ~]$ cifscreds add -u westos 172.25.254.100Password: [westos@westos_student50 ~]$ ls /mntls: cannot access '/mnt': Permission denied[westos@westos_student50 ~]$ cifscreds clearall[westos@westos_student50 ~]$ cifscreds add -u westos 172.25.254.100Password: [westos@westos_student50 ~]$ ls /mntwestosfile1 westosfile2 westosfile3 westosfile4 westosfile5

注:[westos@test/]$ cifscreds add-u lee 172.25.254.20 Key search failed:Key has expired ## 当遇到上面的报错信息时,##执行以下两条命令解决报错

[westos@test/]$ cifscreds add-u lee-d 172.25.254.20

Password:

[westos@test~]$ cifscreds clearall

二、NFS(Net File System)

用于linux linux,linux unix,unix unix 之间实现文件共享

[root@westosa100 ~]# dnf install nfs-utils[root@westosa100 ~]# systemctl enable --now nfs-serverCreated symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.[root@westosa100 ~]# firewall-cmd --permanent --add-service=rpc-bindsuccess[root@westosa100 ~]# firewall-cmd --permanent --add-service=mountdsuccess[root@westosa100 ~]# firewall-cmd --permanent --add-service=nfssuccess[root@westosa100 ~]# firewall-cmd --reloadsuccess[root@westosa100 ~]# showmount -e 172.25.254.100Export list for 172.25.254.100:[root@westos_student50 ~]# showmount -e 172.25.254.100Export list for 172.25.254.100:

1、安装

在50测试主机测试,共享成功

2、nfs配置,将共享目录共享给172.25.254.50,只读

[root@westosa100 ~]# vim /etc/exports ####共享配置文件,此目录更改后,exportfs -rv生效/mnt 172.25.254.50(ro)[root@westosa100 ~]# exportfs -rvexporting 172.25.254.50:/mnt[root@westosa100 ~]# chmod 777 /mnt/ #####更改权限,使共享目录可写[root@westos_student50 ~]# mount 172.25.254.100:/mnt /mnt/[root@westos_student50 ~]# ls /mnt/8-abstract-dark.xml circles-dark.xml desktop-backgrounds-default.xml westos.sql8-abstract-light.xml circles-light.xml hello-world-dark.xml[root@westos_student50 ~]# cd /mnt/[root@westos_student50 mnt]# touch nfstesttouch: cannot touch 'nfstest': Read-only file system

只可以读,不可以写

3、nfs配置,将共享目录共享给172.25.254.50,可读写

[root@westosa100 ~]# vim /etc/exports/mnt 172.25.254.50(rw) ###改为可写[root@westosa100 ~]# exportfs -rvexporting 172.25.254.50:/mnt[root@westos_student50 mnt]# cd [root@westos_student50 ~]# umount /mnt/[root@westos_student50 ~]# mount 172.25.254.100:/mnt /mnt/[root@westos_student50 ~]# cd /mnt/[root@westos_student50 mnt]# touch nfstest[root@westos_student50 mnt]# ls8-abstract-dark.xml circles-dark.xml desktop-backgrounds-default.xml nfstest8-abstract-light.xml circles-light.xml hello-world-dark.xml westos.sql

卸载后重新挂载,可写可读

4、自动挂载卸载nfs+autofs

同7中samba+autofs

5、指定用户身份

[root@westosa100 ~]# vim /etc/exports/mnt 172.25.254.50(rw,anonuid=1000,anongid=1000) ###制定用户身份所有人所有组为westos,1000为westos的id[root@westosa100 ~]# exportfs -rvexporting 172.25.254.50:/mnt[root@westos_student50 mnt]# cd [root@westos_student50 ~]# umount /mnt/[root@westos_student50 ~]# id westosuid=1000(westos) gid=1000(westos) groups=1000(westos)[root@westos_student50 ~]# mount 172.25.254.100:/mnt /mnt/[root@westos_student50 ~]# cd /mnt/[root@westos_student50 mnt]# touch test2[root@westos_student50 mnt]# lltotal 28-rw-r--r-- 1 root root 823 Aug 13 8-abstract-dark.xml-rw-r--r-- 1 root root 829 Aug 16 8-abstract-light.xml-rw-r--r-- 1 root root 805 Aug 13 circles-dark.xml-rw-r--r-- 1 root root 811 Aug 13 circles-light.xml-rw-r--r-- 2 root root 1606 Feb 19 desktop-backgrounds-default.xml-rw-r--r-- 1 root root 829 Aug 13 hello-world-dark.xml-rw-r--r-- 1 nobody nobody 0 Nov 28 10:43 nfstest-rw-r--r-- 1 westos westos 0 Nov 28 11:01 test2-rw-r--r-- 1 root root 1931 Nov 21 09:37 westos.sql

查看westos的id测试

6、当挂载时使用超级用户,则沿用超级用户身份

[root@westosa100 ~]# vim /etc/exports[root@westosa100 ~]# cat /etc/exports/mnt172.25.254.50(rw,no_root_squash)[root@westosa100 ~]# exportfs -rvexporting 172.25.254.50:/mnt[root@westos_student50 mnt]# cd [root@westos_student50 ~]# umount /mnt/[root@westos_student50 ~]# mount 172.25.254.100:/mnt /mnt/[root@westos_student50 ~]# cd /mnt/[root@westos_student50 mnt]# touch test3[root@westos_student50 mnt]# lltotal 28-rw-r--r-- 1 root root 823 Aug 13 8-abstract-dark.xml-rw-r--r-- 1 root root 829 Aug 16 8-abstract-light.xml-rw-r--r-- 1 root root 805 Aug 13 circles-dark.xml-rw-r--r-- 1 root root 811 Aug 13 circles-light.xml-rw-r--r-- 2 root root 1606 Feb 19 desktop-backgrounds-default.xml-rw-r--r-- 1 root root 829 Aug 13 hello-world-dark.xml-rw-r--r-- 1 nobody nobody 0 Nov 28 10:43 nfstest-rw-r--r-- 1 westos westos 0 Nov 28 11:01 test2-rw-r--r-- 1 root root0 Nov 28 11:05 test3-rw-r--r-- 1 root root 1931 Nov 21 09:37 westos.sql

测试

如果觉得《网络文件系统——上(samba NFS 实现网络共享文件)》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。