Configure a basic auto-mount function for your network file system (NFS).

Most Linux file systems are mounted at boot time and remain mounted while the system is running. The same is true for any remote file system that has been configured in fstab. However, sometimes you may want to mount remote file systems only on demand. For example, improving performance by reducing network bandwidth usage, or hiding or obtruding certain directories for security reasons. The AutoFS package provides this functionality. In this article, I’ll show you how to configure basic auto mount.

Let’s assume that an NFS server, Tree.mydatacenter.net, is up and running. Also assume a data directory called OurFiles and user directories for Carl and Sarah, both shared by the server.

Some best practices make this work better: Users on the server have the same user ID as accounts on any client workstation. In addition, your workstation and server should have the same domain name. Checking the relevant configuration files should be confirmed.

alan@workstation1:~$ sudo getent passwd carl sarah
[sudo] password for alan:
carl:x:1020:1020:Carl,,,:/home/carl:/bin/bash
sarah:x:1021:1021:Sarah,,,:/home/sarah:/bin/bash

alan@workstation1:~$ sudo getent hosts
127.0.0.1       localhost
127.0.1.1       workstation1.mydatacenter.net workstation1
10.10.1.5       tree.mydatacenter.net tree
Copy the code

As you can see, both the client workstation and NFS server are configured in the hosts file. I assume this is a basic home or even small office network that may lack a suitable internal domain name service (i.e., DNS).

Installing software Packages

You only need to install two packages: NFS-Common for NFS clients and Autofs, which provides automatic mount.

alan@workstation1:~$ sudo apt-get install nfs-common autofs
Copy the code

You can verify that autofs-related files are already in /etc:

alan@workstation1:~$ cd/etc; ll auto* -rw-r--r-- 1 root root 12596 Nov 19 2015 autofs.conf -rw-r--r-- 1 root root 857 Mar 10 2017 auto.master -rw-r--r-- 1 root root 708 Jul 6 2017 auto.misc -rwxr-xr-x 1 root root 1039 Nov 19 2015 auto.net* -rwxr-xr-x 1 root root  2191 Nov 19 2015 auto.smb* alan@workstation1:/etc$Copy the code

Configuration autofs

Now you need to edit a few of these files and add the auto-. home file. First, add the following two lines to the file auto-.master:

/mnt/tree  /etc/auto.misc
/home/tree  /etc/auto.home
Copy the code

Each line begins with the directory to which the NFS share is mounted. Continue creating these directories:

alan@workstation1:/etc$ sudo mkdir /mnt/tree /home/tree
Copy the code

Next, add the following line to the file auto-.misc:

ourfiles        -fstype=nfs     tree:/share/ourfiles
Copy the code

This line indicates that autofs will mount ourFiles shares that match auto-.misc in the auto-.master file. As shown above, these files will be in the/MNT /tree/ ourFiles directory.

Third, create the file auto. Home with the following line:

*               -fstype=nfs     tree:/home/&
Copy the code

This line indicates that autofs will mount user shares matching auto. Home in the auto. Master file. In this case, Carl’s and Sarah’s files will be in the directories /home/tree/Carl or /home/tree/Sarah, respectively. The asterisk * (called a wildcard) enables each user’s share to be mounted automatically upon login. The ampersand can also be used as a wildcard to represent a server-side user directory. Their home directories are mapped accordingly from the Passwd file. If you prefer the local host directory, you do not need to do this. Instead, users can use it as simple remote storage for specific files.

Finally, restart the Autofs daemon to recognize and load these configuration changes.

alan@workstation1:/etc$ sudo service autofs restart
Copy the code

Test autofs

If you change the directories listed in the file auto-.master and run the ls command, you won’t see anything immediately. For example, switch to the directory/MNT /tree. First, the output of ls shows nothing, but the OurFiles shared directory will be mounted automatically after running CD OurFiles. The CD command will also be executed and you will enter the newly mounted directory.

carl@workstation1:~$ cd /mnt/tree
carl@workstation1:/mnt/tree$ ls
carl@workstation1:/mnt/tree$ cd ourfiles
carl@workstation1:/mnt/tree/ourfiles$
Copy the code

To further verify that it is working properly, the mount command displays the details of the mounted share.

carl@workstation1:~$ mount

tree:/mnt/share/ourfiles on /mnt/tree/ourfiles typenfs4 (rw, relatime, vers = 4.0, rsize = 131072, wsize = 131072, namlen = 255, hard, proto = TCP, timeo = 600, retrans = 2, the SEC = sys, clientaddr = 10.10.1 22, local_lock = none, addr = 10.10.1.5)Copy the code

For Carl and Sarah, the /home/tree directory works the same way.

I find it useful to bookmark these directories in my file manager for quick access.


Via: opensource.com/article/18/…

By Alan formy-duval, lujun9972

This article is originally compiled by LCTT and released in Linux China