nfs

来源:互联网 发布:注明的网络写手 编辑:程序博客网 时间:2024/06/10 20:00

                                                 nfs
1.NFS概念

  网络文件系统(NFS)是Unix系统和网络附加存储文件管理器常用的网络文件系统,允许多个客户端通过网络共享文件访问。它可用于提供对共享二进制
目录的访问,也可用于允许用户在同一工作组中从不同客户端访问其文件。NFS协议有多个版本:Linux支持版本4、版本3和版本2, 而大多数系统管理员
熟悉的是NFSv3。默认情况下,该协议并不安全,但是更新的版本(如NFSv4)提供了对更安全的身份验证的支持,甚至可以通过kerberos进行加密。


2.配置nfs服务

(1)安装及配置
[root@localhost ~]# yum install nfs-utils -y                      ##安装nfs服务
[root@localhost ~]# systemctl start nfs-server                    ##开启 nfs 服务
[root@localhost ~]# systemctl enable nfs-server
[root@localhost ~]# systemctl start firewalld                     ##打开 firewalld
[root@localhost ~]# firewall-cmd --permanent --add-service=ssh    ##允许 ssh 服务使用
success
[root@localhost ~]# firewall-cmd --permanent --add-service=nfs     ##允许 nfs 服务使用
success
[root@localhost ~]# firewall-cmd --permanent --add-service=rpc-bind
success
[root@localhost ~]# firewall-cmd --permanent --add-service=mountd  ##开启挂载
success
[root@localhost ~]# firewall-cmd --reload                          ##刷新火墙命令
success
[root@localhost ~]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0 eth1
  sources:
  services: dhcpv6-client mountd nfsrpc-bind ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:
   
[root@localhost ~]#


(2)共享目录数据同步

@@建立共享目录
[root@nfs-server ~]# mkdir /public                                   ##建立共享目录
[root@nfs-server ~]# touch /public/westos{1..3}                      ##给共享目录下建立三个文件
[root@nfs-server public]# chmod 777 /public                          ##共享文件/public 777权限
@@配置服务
[root@nfs-server ~]# vim /etc/exports                                ##编辑配置文件                          
[root@nfs-server ~]# exportfs -rv                                    ##对配置文件进行刷新,一定不能重启nfs服务 不然服务会卡死
exporting *:/public
[root@nfs-server ~]# vim /etc/exports
[root@nfs-server ~]# exportfs -rv
exporting 172.25.254.179:/public
[root@nfs-server ~]#

(3)指定用户共享目录 (rw)
[root@nfs-server ~]# vim /etc/exports
/public         *(sync)172.25.254.178(rw,sync)

[root@nfs-server ~]# exportfs -rv
exporting 172.25.254.178:/public
exporting *:/public

测试:
[root@desktop ~]# mount 172.25.254.178:/public /mnt
[root@desktop ~]# cd /mnt/
[root@desktop mnt]# ls
westos1  westos2  westos3
[root@desktop mnt]# touch file
[root@desktop mnt]# ls
file  westos1  westos2 westos3
[root@desktop mnt]# ll
total 0
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jun  322:03 file
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos1
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos2
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos3


(4)共享目录 (当客户端使用root挂载不转换用户)
[root@nfs-server ~]# vim /etc/exports
/public         *(sync)172.25.254.178(rw,sync,no_root_squash)

[root@nfs-server ~]# exportfs -rv
exporting 172.25.254.178:/public
exporting *:/public
[root@nfs-server ~]#
测试:
[root@desktop ~]# mount 172.25.254.179:/public /mnt
[root@desktop ~]# cd /mnt/
[root@desktop mnt]# ls
file  westos1  westos2 westos3
[root@desktop mnt]# touch file1
[root@desktop mnt]# ll
total 0
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jun  322:03 file
-rw-rw-r-- 1 root      root      0 Jun 3 22:10 file1
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos1
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos2
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos3
[root@desktop mnt]#


(5)共享目录(指定UID GID)
[root@nfs-server ~]# vim /etc/exports
/public        *(rw,sync,anonuid=1000,anongid=1000)
[root@nfs-server ~]# exportfs -rv
exporting *:/public
[root@nfs-server ~]#
测试:
[root@desktop ~]# mount 172.25.254.179:/public /mnt
[root@desktop ~]# cd /mnt/
[root@desktop mnt]# ls
file  file1  westos1 westos2  westos3
[root@desktop mnt]# touch file2
[root@desktop mnt]# ll
total 0
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Jun  322:03 file
-rw-rw-r-- 1 root      root      0 Jun 3 22:10 file1
-rw-rw-r-- 1 student   student   0 Jun 3 22:24 file2
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos1
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos2
-rw-r--r-- 1 root      root      0 Jun 3 21:53 westos3
[root@desktop mnt]#


3.利用kerberos保护nfs输出

[root@server ~]# yum install sssd krb5-workstation authconfig-gtk -y
[root@server ~]# authconfig-gtk
[root@server ~]# su -student
su: failed to execute tudent: No such file or directory
[root@server ~]# su - student
[student@server ~]$ su - ldapuser1
Password:
su: warning: cannot change directory to /home/guests/ldapuser1: No such file ordirectory
mkdir: cannot create directory '/home/guests': Permission denied
-bash-4.2$ exit
logout

[root@server ~]# wget http://172.25.254.254/pub/keytabs/server.keytab -O/etc/krb5.keytab
--2017-06-03 23:16:08-- http://172.25.254.254/pub/keytabs/server.keytab
Connecting to 172.25.254.254:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1242 (1.2K)
Saving to: ‘/etc/krb5.keytab’

100%[========================================>] 1,242       --.-K/s  in 0s     

2017-06-03 23:16:08 (123 MB/s) - ‘/etc/krb5.keytab’ saved [1242/1242]
[root@server3 ~]# ktutil
ktutil:  rkt /etc/krb5.keytab
ktutil:  list
slot KVNO Principal
---- ---- ---------------------------------------------------------------------
   1   3    host/server.example.com@EXAMPLE.COM
   2   3    host/server.example.com@EXAMPLE.COM
   3   3    host/server.example.com@EXAMPLE.COM
   4   3    host/server.example.com@EXAMPLE.COM
   5   3     host/server.example.com@EXAMPLE.COM
   6   3    host/server.example.com@EXAMPLE.COM
   7   3    host/server.example.com@EXAMPLE.COM
   8   3    host/server.example.com@EXAMPLE.COM
   9   3     nfs/server.example.com@EXAMPLE.COM
  10   3      nfs/server.example.com@EXAMPLE.COM
  11   3     nfs/server.example.com@EXAMPLE.COM
  12   3     nfs/server.example.com@EXAMPLE.COM
  13   3     nfs/server.example.com@EXAMPLE.COM
  14   3     nfs/server.example.com@EXAMPLE.COM
  15   3     nfs/server.example.com@EXAMPLE.COM
  16   3     nfs/server.example.com@EXAMPLE.COM
ktutil:  exit
[root@server3 ~]#
[root@server3 ~]# vim /etc/exports
[root@server3 ~]# exportfs -rv
exporting *:/public
[root@server ~]# systemctl start nfs-secure-server
[root@server ~]# systemctl enable nfs-secure-server
ln -s '/usr/lib/systemd/system/nfs-secure-server.service''/etc/systemd/system/nfs.target.wants/nfs-secure-server.service'
[root@server ~]#


 

原创粉丝点击