Setup Redhat7 NFS & NIS

Setup NFS Server

1. 安装NFS软件

1
2
# yum install -y nfs-utils
# yum install -y rpcbind

2. 共享目录 共享主机(权限)

1
2
3
4
5
6
7
# mkdir -p /share/common
# mkdir -p /share/proj
# mkdir -p /share/docs
# cat /etc/exports
/share/common 10.10.1.0/24(rw,sync,no_root_squash) #一个网段
/share/proj 10.10.1.99(rw,sync,no_root_squash) #单个主机
/share/docs *(ro,sync) #所有主机

3. 共享主机生效 查看共享主机

1
2
# exportfs -r
# exportfs -v

4. 防火墙放行服务

1
2
3
4
# firewall-cmd --permanent --zone=pubic --add-service=rpc-bind
# firewall-cmd --permanent --zone=pubic --add-service=mountd
# firewall-cmd --permanent --zone=pubic --add-service=nfs
# firewall-cmd --reload

5. 开启nfs-server服务

1
2
# systemctl enble nfs-server.service
# systemctl start nfs-server.serivce

6. 查看NFS共享信息

1
# showmount -e

Setup Client AutoFS (自动挂载NFS共享文件夹)

1. 安装autofs

1
# yum install -y autofs

2. 配置autofs

1
2
# cat /etc/auto.master.d/nfs.autofs
/- /etc/auto.master.d/autofs.map
  • NFS配置文件可以是/etc/auto.master,也可以新建/etc/auto.master.d/*.autofs
  • /- 表示共享文件采用绝对路径
  • 共享文件夹map由auto.master指定
    1
    2
    3
    4
    # cat /etc/auto.master.d/autofs.map
    /mnt/common -fstype=nfs,rw,soft,intr nfssvr100:/share/common
    /mnt/proj -fstype=nfs,rw,soft,intr nfssvr100:/share/proj
    /mnt/docs -fstype=nfs,rw,soft,intr nfssvr100:/share/docs
    也可以用相对路径,例如:
1
2
3
4
5
6
# cat /etc/auto.master.d/nfs.autofs
/mnt /etc/auto.master.d/autofs.map
# cat /etc/auto.master.d/autofs.map
common nfssvr100:/home/common
proj nfssvr100:/home/proj
docs nfssvr100:/home/docs

3. 查看NFS共享信息

1
# showmount -e nfssvr100

4. 启动autofs服务

1
2
# systemctl enable autofs
# systemctl start autofs

注意:完成上述自动挂载步骤后,可能在/mnt目录下看不到挂载成功,但是只要cd /mnt/common就可以看到了,即未被访问前挂载目录是隐藏的。

5. 查看挂载文件夹状态

1
# df -hT

Setup NIS Server

1. 安装NIS软件

1
# yum install -y yp-tools ypserv rpcbind xinetd ypxfrd

2. 配置/etc/hosts

1
2
3
4
# cat /etc/hosts
10.10.1.10 nissrv0
10.10.1.11 shanghai
10.10.1.12 hangzhou

3. 配置/etc/sysconfig/network

1
2
3
4
# cat /etc/sysconfig/network
HOSTNAME=nissrv0
NISDOMAIN=nisdomain0
# nisdomainname nisdomain0

4. 配置/etc/ypserv.conf

1
2
# cat /etc/ypserv.conf
* : * : * : none

5. 建立NIS用户

1
2
3
4
# useradd -d /home/nisusr/foo -m -s /bin/tcsh foo
# useradd -d /home/nisusr/bar -m -s /bin/tcsh bar
# echo "FOOPASSWORD" | passwd --stdin foo
# echo "BARPASSWORD" | passwd --stdin bar

6. 建立NIS数据库

1
2
3
4
5
6
7
# /usr/lib64/yp/ypinit -m
At this point, we have to construct a list of the hosts which will run NIS
servers. entry0 is in the list of NIS server hosts. Please continue to add
the names for the other hosts, one per line. When you are done with the
list, type a <control D>.
next host to add: entry0
next host to add: #press Ctrl+D

6. 开启NIS相关服务

1
2
# systemctl enable ypserv yppasswdd ypbind rpcbind xinetd ypxfrd
# systemctl start ypserv yppasswdd ypbind rpcbind xinetd ypxfrd

Setup NIS Client

1. 安装NIS软件

1
yum install yp-tools ypbind rpcbind

2. 配置/etc/hosts

1
2
3
4
# cat /etc/hosts
10.10.1.10 nissrv0
10.10.1.11 shanghai
10.10.1.12 hangzhou

3. 配置/etc/sysconfig/network

1
2
3
4
# cat /etc/sysconfig/network
HOSTNAME=shanghai
NISDOMAIN=nisdomain0
# nisdomainname nisdomain0

4. 配置/etc/yp.conf

1
2
# cat /etc/yp.conf
domain nisdomain0 server nissrv0

5. 配置/etc/nsswitch.conf

1
2
3
4
5
6
7
# cat /etc/nsswitch.conf
passwd: files nis sss
shadow: files nis sss
group: files nis sss
hosts: files nis dns myhostname
netgroup: files nis sss
automount: files nis sss

6. 配置authconfig

7. 开启NIS相关服务

1
2
# systemctl enable ypbind rpcbind
# systemctl start ypbind rpcbind

8. 检验NIS客户端

1
2
# ypwhich
# yptest