0

LFCS filesystem & storage part 6

Welcome to post 25 of my 100 day challenge. Checkout my introduction for some background.

This is post nine of my LFCS series. This post is the sixth and final part of file system and storage. In it we will discuss mounting Networked File Systems (NFS) and troubleshooting file system issues. For the first five parts see post 5post 6post 7post 8 and post 9.

You can go back to the overview post for a brief introduction or take a look at post one for instructions on setting up the exam practice system which I will be using throughout this series. For the posts regarding the Linux Command Line see posts 2, 3 and 4.

This post is quite long so you might want to set a side some time to go through it properly.

LFCS filesystem & storage part 6

Mounting network file systems

What is NFS?

NFS is a distributed file system which allows a user to access files on a network as if they were accessing local storage. This means that many users on different computers can access the same resources. There is also the notion of a thin client where you have no files locally they are all stored remotely and all the client does when you login is mount the remote drive.

In this tutorial we are going to discuss how to setup an NFS server to share some files and connect to it from a client and view those files. I’m not covering how to setup an NFS server in this tutorial.

Setup the NFS server

Create the directory /nfstest on the server:

[centostest@nfsserver ~] mkdir nfstest
[centostest@nfsserver ~] sudo ln -s /home/centostest/nfstest /nfstest
[centostest@nfsserver ~] echo "A test file for NFS" > /nfstest/nfstestfile

Install NFS on the Server

[centostest@nfsserver ~] sudo yum install nfs-kernel-server nfs-common rpcbind

Add our test domain to idmapd.conf:

Under the line #Domain = localdomain add the domain name.
[root@nfsserver ~] vi /etc/idmapd.conf
Domain = nfstest.com

Confirm connectivity to client:

[centostest@nfsserver ~] ping -c1 nfsclient.nfstest.com
PING nfsclient.nfstest.com (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_req=1 ttl=61 time=5.51 ms
--- nfsclient.nfstest.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 5.519/5.519/5.519/0.000 ms

Configure the NFS Server
NFS exports are configured in the file /etc/exports. Each line begins with the absolute path of the directory to be exported, followed by a space separated list of allowed clients and any options required. We are going to use the following options:

Option Explanation
rw Allows users to perform reads and writes to the volume on the NFS server.
sync When changes have been written to storage the NFS server will reply to the client request.
[root@nfsserver ~] echo -e "\n# /nfstest access" >> /etc/exports
[root@nfsserver ~] echo "/nfstest nfsserver.nfstest.com(rw,sync,fsid=0)"
>> /etc/exports

Start the NFS server:

[root@nfsserver ~] service nfs-kernel-server start
[ ok ] Exporting directories for NFS kernel daemon....
[ ok ] Starting NFS kernel daemon: nfsd mountd.
[root@nfsserver ~] exportfs -a
[root@nfsserver ~] exit

Setup the NFS client

Confirm connectivity to server:

centostest@nfsclient ~] ping -c1 nfsserver.nfstest.com
PING nfsserver.nfstest.com (192.168.0.1) 56(84) bytes of data.
64 bytes from 192.168.0.1: icmp_req=1 ttl=61 time=5.51 ms
--- nfsserver.nfstest.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 5.519/5.519/5.519/0.000 ms

Install NFS on the client

Install the following on the client:

[root@nfsserver ~] yum install nfs-common rpcbind

Configure the NFS client

As you did with the Server you need to add in the Domain name to /etc/idmapd.conf:

[root@nfsserver ~] vi /etc/idmapd.conf
Domain = nfstest.com
[root@nfsserver ~] sudo service nfs-common restart

Setup the mount on the NFS client

Now we have set-up the client and server we now need to mount the server export to /mnt/nfstest on the client. We also need to setup permissions for the user to Read/Write to the share. In this method you can use -v with the mount command as it will make you aware of any issues during the process. We are also going to set some options up so that files with special bits set have them stripped off when the files are sent over to the NFS server volume.

[root@nfsclient ~] mkdir /mnt/nfstest
[root@nfsclient ~] echo -e "\n# /Mount to nfsserver.nfstest.com:/nfstest" >> /etc/fstab
[root@nfsclient ~] echo -e
"nfsserver.nfstest.com:/nfstest\t/mnt/nfstest\tnfs\tuser,rw,nosuid\t0\t0" >>
/etc/fstab
[root@nfsclient ~] mount -v nfsserver.nfstest.com:/nfstest
mount.nfs: timeout set for Wed May 06 14:10:12 2015
mount.nfs: trying text-based options
'vers=4,addr=192.168.0.1,clientaddr=192.168.0.2'
mount.nfs: mount(2): No such file or directory
mount.nfs: trying text-based options 'addr=192.168.0.1'
mount.nfs: prog 100003, trying vers=3, prot=6
mount.nfs: trying 192.168.0.1 prog 100003 vers 3 prot TCP port 2049
mount.nfs: prog 100005, trying vers=3, prot=17
mount.nfs: trying 192.168.0.1 prog 100005 vers 3 prot UDP port 37778

It is important that you set up the same user and group on both sides of this setup and that both user and group have the same UID and GID respectively as NFS will use these IDs to implement the permissions.

Now we test the the NFS system configured actually works

Confirm that the volume was mounted successfully:

[root@nfsclient ~] df -h | grep nfstest
nfsserver.nfstest.com:/nfstest 10G 1G 9G 10% /mnt/nfstest

Create a file nfsclienttest.txt at /mnt/nfstest on the client then connect to the server to see if it is there:

[root@nfsclient ~] echo "Test client" > /mnt/nfstest/nfsclienttest.txt
[root@nfsclient ~] cat /mnt/nfstest/nfsclienttest.txt
Test client
[root@nfsserver ~] cat /mnt/nfstest/nfsclienttest.txt
Test client

Now do the same from the server:

[root@nfsserver ~] echo "Test server" > /mnt/nfstest/nfsservertest.txt
[root@nfsserver ~] cat /mnt/nfstest/nfsservertest.txt
Test server
[root@nfssclient ~] cat /mnt/nfstest/nfsservertest.txt
Test client

Troubleshooting file system issues

We can use the fsck (File System Consistency checK) utility to check the health of a file system.The only caveat is that the partition/drive needs to be umounted so that it can check the file system for issues.

Below is a table of possible exit codes you can encounter when running this tool against your file systems.

Exit Code Explanation
0 No errors.
1 File system errors corrected.
2 You should reboot the system.
4 Errors have been left uncorrected on the file system.
8 There has been an operational error.
16 Syntax or usage error
32 User request has cancelled the fsck check.
128 Shared library error.

Lets take one of our partitions created earlier, unmount it and run fsck on it. We can use echo $? on the command line to output the exit code of fsck after it has run:

[root@centosexampractice ~]# fsck.ext4 /dev/sdb1
e2fsck 1.41.12 (17-May-2010)
/dev/sdb1: clean, 11/524288 files, 70297/2096474 blocks
[root@centosexampractice ~]# echo $?
0

When you start up your system and if the previous time it was running one of the file systems was not unmounted cleanly Linux will detect what is known as a dirty bit on the file system and will automatically start a check.

Tune in tomorrow for the first part of my revision article on LFCS Local System Administration where we will discuss creating backups and managing local user accounts.

Subscribe to my feed either by E-mail or by RSS to receive updates as they happen.

Can you improve on any of the tips I’ve discussed here? If you can let me know in the comments.

Jason Edwards