Thursday 3 February 2005

Mounting a filesystem automatically using 'autofs'

There was a drawback in Linux when compared to other OSes in that, you cannot read the contents of a filesystem as easily as you do in other OSes like Windows without executing the mount commands (this is especially true for removable media like a Floppy, CDROM or remote filesystems like an NFS share). For example, if you want to read the contents of a floppy, you have to mount the floppy into a directory using the mount command. And after you have read the data , and before you pull out the floppy from the floppy drive, you have to remember to unmount the floppy using umount command. If by any chance, you have forgotten to unmount the floppy, there is a good chance that your data will be corrupted.
But now you have an easy way out of the hassle of mounting and unmounting your filesystem and using it like you use in windows. This is achieved by the autofs daemon.
Autofs is a standard feature in all major linux distributions. It is installed by default. Here I will explain how to configure autofs in RedHat distribution. The configuration is the same in other distributions other than the chkconfig and service scripts which are a RedHat feature.
First check if the autofs daemon is configured to start at boot time by running the command:
# chkconfig autofs --list
autofs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
As you can see from the above listing that autofs is OFF on my system. So I turn it ON using the command:
# chkconfig autofs on
# chkconfig autofs --list
autofs 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Now the autofs daemon is configured to start automatically when you boot up your machine.
The main configuration files for autofs is /etc/auto.master. This file has the syntax as follows:
Each mount point is given on a seperate line. For eg:
# File: /etc/auto.master
/mnt /etc/auto.misc
Where, /mnt is the mount point and it points to another file called autofs.misc (the file can have any name but following precedence, it should start with the name auto), which contains the details of how and what to mount.Suppose I have to make autofs automount my floppy whenever I change into my /mnt/floppy directory, and unmount it automatically when I come out of the /mnt/floppy directory, I just include the following line into my /etc/auto.misc file.
# File: /etc/auto.misc
floppy -fstype=auto,rw,users :/dev/fd0
Each line in the file is divided into 3 sections seperated by one or more spaces.They are :
  • A key - The key can be any single word but preferably the name of the device or the name of the sub-directory below the mount point specified in the auto.master file.
  • The mount options for the filesystem associated with the key. In the listing of my auto.misc file, I have set it to mount with read/write permission and autodetect the type of filesystem - whether ext3 or vfat etc - and who all can mount the filesystem (see man mount for more options).
  • The path of the device file associated with the filesystem. It should be preceeded by a colon (:).
Now I save and exit the /etc/auto.misc file. The last thing to do is to restart the autofs daemon to re-read the /etc/auto.master file.
# service autofs restart
Note: Each time any changes are made to the /etc/auto.master file, you have to restart the autofs daemon to bring the changes to effect. But you need not restart autofs if you change the contents of auto.misc file or the contents of any other files listed in the /etc/auto.master file. Now I test my setup by inserting a floppy into the floppy drive and execute the following command:
# cd /mnt/floppy
Success!! The floppy gets mounted automatically and I am able to list the contents of the floppy.Now I come out of the directory. And after a period of inactivity (default is 60 sec), the floppy is automatically unmounted.
You can likewise set the remote NFS share (at IP address 192.168.0.1) to mount using autofs. For example, I could append a line in /etc/auto.misc file as follows:
# File: /etc/auto.misc
nfsshare -fstype=auto,soft,intr 192.168.0.1:/remote_nfs_share
cdrom -fstype=iso9660,ro :/dev/cdrom
.. and so on.

Tuesday 1 February 2005

DHCP Server configuration

Suppose you are in charge of a network of say 100 computers all in a single broadcast domain. There are two ways of configuring the IP addresses of these machines. One is the static method ; assigning an IP address to each of the 100 machines manually which can be quite tedious. The other easier and better method, is to use DHCP (Dynamic Host Configuration Protocol) to let a computer acting as the server assign the IP address to each of the 100 machines automatically. Infact, it is so easy that nowadays any computer which forms a part of a large network is assigned its IP address dynamically. This is also true when you connect to the internet via a dial-up or DSL modem - in which case, your computer is technically a part of the large network of your ISP and your ISP assigns your computer an IP address from its address pool automatically.

There are two popular methods of assigning IP addresses. They are BOOTP and DHCP. Both wait and hear for computers in the network to send broadcasts publishing their MAC addresses and requesting an IP address. The BOOTP or DHCP server, on recieving a broadcast, assign an IP address to the MAC address in the broadcast from its address pool. The client computers can query the server and find lots of information like default gateway, IP address, subnet mask, DNS etc.

Here I will explain how to convert your linux machine into a DHCP server. The power of DHCP is that if anything changes on your network such as the IP of a DNS server, you only need to edit one configuration file even if you have hundreds of clients.



If you do not have dhcp server installed on your machine, this is the right time to do so. In Redhat, you install the dhcp (rpm) package. The dhcp server runs as a daemon and has the name dhcpd and listens on ports 67 (bootp server) and 68 (bootp client). There are two main configuration files for the DHCP server. They are :

  • /etc/dhcpd.conf
  • /var/lib/dhcp/dhcpd.leases

The dhcp package installs without any configuration. The daemon will not start if a dhcpd.leases file does not exist. An empty file (commented) is installed with this package.

#File : /etc/dhcpd.conf



ddns-update-style none;



option domain-name "mydomain.com";



default-lease-time 21600;

max-lease-time 43200;



subnet 192.168.1.0 netmask 255.255.255.0

{

range 192.168.1.100 192.168.1.200;

option subnet-mask 255.255.255.0;

option broadcast-address 192.168.1.255;

option domain-name-servers 123.123.123.10, 123.123.123.20;

option routers 192.168.1.1;



host station1

{

hardware ethernet 00:a0:cc:3d:0b:39;

fixed-address 192.168.1.7;

}



host station2

{

hardware ethernet 00:06:CD:CD:CD:CD;

fixed-address 192.168.1.8;

}

}
Above I have shown the listing of my /etc/dhcpd.conf file. I will explain the meaning of each line below :

ddns-update-style none;
The first thing we need to do is set a Dynamic DNS update style. Here I have set it to the value none. But if it is something you want to do, you may read the man pages which has lots of information on this topic.

option domain-name "mydomain.com";
The above line specifies the domain name set on your server if you are running DNS.

default-lease-time 21600;

max-lease-time 43200;

Specifies the time in seconds after which the lease will expire and the maximum lease time also in seconds.

subnet 192.168.1.0 netmask 255.255.255.0
Next we must specify what subnet and netmask we will be working on. Note that you can have many subnet configurations within the single dhcpd.conf file. Each subnet group is bound together by curly braces { }



Note that every command from here on will only pertain to the subnet specified above. This will be true until we reach the closing curly brace } as noted above.



Now we will specify what range of IP addresses we want to be made available for clients using DHCP. This option is very handy when used in conjunction with a firewall because you know exactly what IP addresses came from a client using DHCP and you can exercise restrictions upon them as necessary.

range 192.168.1.100 192.168.1.200;
option subnet-mask 255.255.255.0;
The above option is redundant as it has been already set before the curly braces. But since it is given in the man pages, I have included it here.

option broadcast-address 192.168.1.255;
Specifying the broadcast address of our subnet.

option domain-name-servers 123.123.123.10, 123.123.123.20;
The above line tells all our clients what servers to use for DNS inorder to resolve hostnames to IP addresses.

option routers 192.168.1.1;
This line tells our clients what IP address to use for the default gateway. Usually the default gateway is our router.

Even though DHCP gives out IP addresses dynamically, it has the option of reserving a particular address for a certain computer. To do this, you have to specify the MAC address of your client machine for which you need to reserve an IP address. You can find the MAC address by running the following command on your client machine:

# ifconfig eth0 | grep HWaddr
..where eth0 is your ethernet interface. The MAC address is a 48 bit address burned into the NIC by its manufacturer. It is a unique number and no two NICs in the world will have the same number. It is obtained in hexadecimal format.

host station1
The first thing we must do is to specify a name for the computer as a helpful identifier as shown above. Note that similar to the subnet grouping, we are starting a sub-group which is represented by the curly braces. This allows us to have multiple host definitions within one subnet group.

hardware ethernet 00:a0:cc:3d:0b:39;
This is the client machine's MAC address for which we are going to reserve an IP address.

fixed-address 192.168.1.7;
This line tells the dhcpd server what IP address we always want to be assigned to this computer. Now the only thing remaining is to save your /etc/dhcpd.conf file and restart your DHCP daemon.

# service dhcpd restart
Also see How to assign an IP Address to understand DHCP client configuration.