Software >> OS >> Unix >> Linux >> RHEL >> 7 >> RHCSA >> How to configure AutoFS to mount user home directories from NFS server

 

 

## On the NFS server



[root@rhel7server1 home]# ls -alh /home
total 0
drwxr-xr-x.  5 root      root       49 Jun 29 11:11 .
dr-xr-xr-x. 17 root      root      224 Jun 11 12:41 ..
drwx------.  3 user1     user1      78 Jun 29 11:11 user1
drwx------.  3 user2     user2      78 Jun 29 11:11 user2

[root@rhel7server1 home]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)

[root@rhel7server1 home]# id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)


## create some files for each of these users at the NFS server side home dir

root@rhel7server1 ~]# su - user1

[user1@rhel7server1 ~]$ echo "created at server" > user1.txt

[user1@rhel7server1 ~]$ ll
total 4
-rw-rw-r--. 1 user1 user1 18 Jun 29 11:30 user1.txt

[root@rhel7server1 ~]# su - user2

[user2@rhel7server1 ~]$ echo "created at server" > user2.txt

[user2@rhel7server1 ~]$ ll
total 4
-rw-rw-r--. 1 user2 user2 18 Jun 29 11:30 user2.txt


## install nfs-utils package

[root@rhel7server1 ~]# yum -y install nfs-utils


## check selinux settings for NFS

[root@rhel7server1 ~]# getsebool -a | grep nfs_export
nfs_export_all_ro --> on
nfs_export_all_rw --> on



## if not set to on, set it, otherwise skip this step


[root@rhel7server1 ~]# setsebool -P nfs_export_all_ro=1 nfs_export_all_rw=1


## add firewall rules

[root@rhel7server1 ~]# firewall-cmd --permanent --add-service=nfs
success

[root@rhel7server1 ~]# firewall-cmd --reload
success


## enable and start the services

[root@rhel7server1 ~]# systemctl enable rpcbind

[root@rhel7server1 ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@rhel7server1 ~]# systemctl start rpcbind

[root@rhel7server1 ~]# systemctl start nfs-server


## define and export the dir to be shared

[root@rhel7server1 ~]#
vi /etc/exports

/etc/exports
/home   192.168.0.20(rw,no_root_squash)

[root@rhel7server1 ~]# exportfs -avr
exporting 192.168.0.20:/home



 

 


## On NFS Client side


## install nfs-utils and autofs packages

[root@rhel7client1 etc]# yum install -y nfs-utils autofs


## edit the autofs master configuration file



/etc/auto.master
/home /etc/home.map


## create the autofs map file

/etc/home.map
*  -fstype=nfs,rw,nosuid,soft  rhel7server1:/home/&


## enable and start NFS client

[root@rhel7client1 etc]# systemctl enable rpcbind

[root@rhel7client1 etc]# systemctl start rpcbind


## enable and start autofs

[root@rhel7client1 etc]# systemctl enable autofs

[root@rhel7client1 etc]#
systemctl start autofs


## Assuming that the same users exist in the client machine with same uid,gid as that in the NFS server,
## it will automount the home directories on first access
## check the home directory for user1, notice it is now mounted from NFS server


[root@rhel7client1 etc]# su - user1
Last login: Mon Jun 29 11:47:42 +08 2020 on pts/0

[user1@rhel7client1 ~]$ df /home/user1
Filesystem               1K-blocks  Used Available Use% Mounted on
rhel7server1:/home/user1  10475520 33024  10442496   1% /home/user1

[user1@rhel7client1 ~]$ ll
total 4
-rw-rw-r--. 1 user1 user1 18 Jun 29 11:30 user1.txt

[user1@rhel7client1 ~]$ cat user1.txt
created at server

[user1@rhel7client1 ~]$ echo "created at client" >> user1-clientside.txt

[user1@rhel7client1 ~]$ ll
total 8
-rw-rw-r--. 1 user1 user1 18 Jun 29 11:49 user1-clientside.txt
-rw-rw-r--. 1 user1 user1 18 Jun 29 11:30 user1.txt