Sunday, October 3, 2010

Understanding of FSTAB in LINUX


FSTAB

< What is fstab and why it's useful >

fstab is a configuration file that contains information of all the partitions and storage devices in your computer. The file is located under /etc, so the full path to this file is /etc/fstab.
/etc/fstab contains information of where your partitions and storage devices should be mounted and how. If you can't access your Windows partition from Linux, aren't able to mount your CD or write to your floppy as a normal user, or have problems with your CD-RW, you probably have a misconfigured /etc/fstab file. So, you can usually fix your mounting problems by editing your fstab file.
/etc/fstab is just a plain text file, so you can open and edit it with any text editor you're familiar with. However, note that you must have the root privileges before editing fstab. So, in order to edit the file, you must either log in as root or use the su command to become root.

< Overview of the file >
Of course everybody has a bit different /etc/fstab file because the partitions, devices and their properties are different on different systems. But the basic structure of fstab is always the same. Here's an example of the contents of /etc/fstab:
/dev/hda2
/
ext2
defaults
1 1
/dev/hdb1
/home
ext2
defaults
1 2
/dev/cdrom
/media/cdrom
auto
ro,noauto,user,exec
0 0
/dev/fd0
/media/floppy
auto
rw,noauto,user,sync
0 0
proc
/proc
proc
defaults
0 0
/dev/hda1
swap
swap
pri=42
0 0
What does all this gibberish mean? As you see, every line (or row) contains the information of one device or partition. The first column contains the device name, the second one its mount point, third its filesystem type, fourth the mount options, fifth (a number) dump options, and sixth (another number) filesystem check options. Let's take a closer look at this stuff.

 

< 1st and 2nd columns: Device and default mount point >

The first and second columns should be pretty straightforward. They tell the mount command exactly the same things that you tell mount when you mount stuff manually: what is the device or partition, and what is the mount point. The mount point specified for a device in /etc/fstab is its default mount point. That is the directory where the device will be mounted if you don't specify any other mount point when mounting the device.
Most Linux distros create special directories for mount points. Most distros create them under /mnt, but some (at least SuSE) under /media. As you probably noticed when looking at the example fstab, I use SuSE's mount points as an example.
What does all this mean? If I type the following command:
$ mount /dev/fd0
... my floppy will be mounted in
/media/floppy, because that's the default mount point specified in /etc/fstab. If there is no entry for /dev/fd0 in my fstab when I issue the command above, mount gets very confused because it doesn't know where to mount the floppy.

You can freely change the default mount points listed in /etc/fstab if you're not satisfied with the defaults your distro has given you. Just make sure the mount point is a directory that already exists on your system. If it doesn't, simply create it.
Some partitions and devices are also automatically mounted when your Linux system boots up. For example, have a look at the example fstab above. There are lines that look like this:
/dev/hda2 / ext2 defaults 1 1
/dev/hdb1 /home ext2 defaults 1 2
As you've learned, these lines mean that /dev/hda2 will be mounted to / and /dev/hdb1 to /home. This is done automatically when your Linux system boots up... if it wouldn't, you'd have a hard time using your cool Linux system because all the programs you use are in / and you wouldn't be able to run them if / wasn't mounted! But how does the system know where you want to mount /dev/hda2 and /dev/hdb1? By looking at the /etc/fstab file of course.

 

< 3rd column: Filesystem type >

The third column in /etc/fstab specifies the filesystem type of the device or partition. Many different filesystems are supported but we'll take a look at the most common ones only.
ext2 and ext3 Very likely your Linux partitions are Ext3. Ext2 used to be the standard filesystem for Linux, but these days, Ext3 and ReiserFS are usually the default filesystems for almost every new Linux distro. Ext3 is a newer filesystem type that differs from Ext2 in that it's journaled, meaning that if you turn the computer off without properly shutting down, you shouldn't lose any data and your system shouldn't spend ages doing filesystem checks the next time you boot up.
reiserfs Your Linux partitions may very well be formatted as ReiserFS. Like Ext3, ReiserFS is a journaled filesystem, but it's much more advanced than Ext3. Many Linux distros (including SuSE) have started using ReiserFS as their default filesystem for Linux partitions.
swap The filesystem name is self-explanatory. The filesystem type "swap" is used in your swap partitions.
vfat and ntfs Your USB stick is most likely formatted as Vfat (more widely known as FAT32). Your Windows partitions are probably either Vfat or NTFS. The 9x series (95, 98, ME) all use Vfat, and the NT series (NT, 2000, XP, Vista, 7) use NTFS but they may be formatted as Vfat, too.
auto No, this isn't a filesystem type :-) The option "auto" simply means that the filesystem type is detected automatically. If you take a look at the example fstab above, you'll see that the floppy and CD-ROM both have "auto" as their filesystem type. Why? Their filesystem type may vary. One floppy might be formatted for Windows and the other for Linux's Ext2. That's why it's wise to let the system automatically detect the filesystem type of media such as floppies and cdroms.

 

< 4th column: Mount options >

The fourth column in fstab lists all the mount options for the device or partition. This is also the most confusing column in the fstab file, but knowing what some of the most common options mean, saves you from a big headache. Yes, there are many options available, but I'll take a look at the most widely used ones only. For more information, check out the man page of mount.
auto and noauto With the auto option, the device will be mounted automatically (at bootup, just like I told you a bit earlier, or when you issue the mount -a command). auto is the default option. If you don't want the device to be mounted automatically, use the noauto option in /etc/fstab. With noauto, the device can be mounted only explicitly.
user and nouser These are very useful options. The user option allows normal users to mount the device, whereas nouser lets only the root to mount the device. nouser is the default, which is a major cause of headache for new Linux users. If you're not able to mount your cdrom, floppy, Windows partition, or something else as a normal user, add the user option into /etc/fstab.
exec and noexec exec lets you execute binaries that are on that partition, whereas noexec doesn't let you do that. noexec might be useful for a partition that contains binaries you don't want to execute on your system, or that can't even be executed on your system. This might be the case of a Windows partition.
exec is the default option, which is a good thing. Imagine what would happen if you accidentally used the noexec option with your Linux root partition...
ro Mount the filesystem read-only.
rw Mount the filesystem read-write. Again, using this option might cure the headache of many new Linux users who are tearing their hair off because they can't write to their floppies, Windows partitions, or something else.
sync and async How the input and output to the filesystem should be done. sync means it's done synchronously. If you look at the example fstab, you'll notice that this is the option used with the floppy. In plain English, this means that when you, for example, copy a file to the floppy, the changes are physically written to the floppy at the same time you issue the copy command.
However, if you have the async option in /etc/fstab, input and output is done asynchronously. Now when you copy a file to the floppy, the changes may be physically written to it long time after issuing the command. This isn't bad, and may sometimes be favorable, but can cause some nasty accidents: if you just remove the floppy without unmounting it first, the copied file may not physically exist on the floppy yet!
async is the default. However, it may be wise to use sync with the floppy, especially if you're used to the way it's done in Windows and have a tendency to remove floppies before unmounting them first.
defaults Uses the default options that are rw, suid, dev, exec, auto, nouser, and async.

 

< 5th and 6th columns: Dump and fsck options >

Dump and, uh, what options? Well, dump is a backup utility and fsck is a filesystem check utility. I won't discuss them in great length here, but I'll mention them, because otherwise you'd spend the rest of the day wondering what on God's green Earth do these things mean.
The 5th column in /etc/fstab is the dump option. Dump checks it and uses the number to decide if a filesystem should be backed up. If it's zero, dump will ignore that filesystem. If you take a look at the example fstab, you'll notice that the 5th column is zero in most cases.
The 6th column is a fsck option. fsck looks at the number in the 6th column to determine in which order the filesystems should be checked. If it's zero, fsck won't check the filesystem.

 

< Example /etc/fstab entries >

As an example, we'll take a look at a couple of fstab entries that have been a source of endless frustration for new Linux users: floppy and CD-ROM (although these days floppies aren't that important anymore).
/dev/fd0 /media/floppy auto rw,noauto,user,sync 0 0
This line means that the floppy is mounted to /media/floppy by default and that its filesystem type is detected automatically. This is useful because the type of the floppy may wary. Note especially the rw and user options: they must be there if you want to be able to mount and write to the floppy as a normal user. If you have trouble with this, check your fstab file to see if these options are there. Also note the sync option. It can be async just as well, but it's sync because of reasons discussed a bit earlier.
/dev/cdrom /media/cdrom auto ro,noauto,user,exec 0 0
Note, again, the user option that enables you to mount the CD as a normal user. The CD-ROM has the ro option because it's no use mounting a CD-ROM read-write because you wouldn't be able to write to it anyway. Also note the exec option. It's especially useful if you'd like to be able to execute something from your CD.
Also note that the noauto option is used with the floppy and CD-ROM. This means that they won't be automatically mounted when your Linux system boots up. This is useful for removable media, because sometimes there won't be any floppy or CD-ROM when you boot up your system, so there isn't any reason to try to mount something that doesn't even exist.

Understanding of INITTAB in LINUX


INNITTAB
inittab - format of the inittab file used by the sysv-compatible init process  
DESCRIPTION
The inittab file describes which processes are started at bootup and during normal operation (e.g. /etc/init.d/boot, /etc/init.d/rc, gettys...). Init(8) distinguishes multiple runlevels, each of which can have its own set of processes that are started. Valid runlevels are 0-6 plus A, B, and C for ondemand entries. An entry in the inittab file has the following format:
id:runlevels:action:process
Lines beginning with `#' are ignored.
id
is a unique sequence of 1-4 characters which identifies an entry in inittab (for versions of sysvinit compiled with libraries < 5.2.18 or a.out libraries the limit is 2 characters).
Note: For gettys or other login processes, the id field should be the tty suffix of the corresponding tty, e.g. 1 for tty1. Otherwise, the login accounting might not work correctly.
runlevels
lists the runlevels for which the specified action should be taken.
action
describes which action should be taken.
process
specifies the process to be executed. If the process field starts with a `+' character, init will not do utmp and wtmp accounting for that process. This is needed for gettys that insist on doing their own utmp/wtmp housekeeping. This is also a historic bug.
The runlevels field may contain multiple characters for different runlevels. For example, 123 specifies that the process should be started in runlevels 1, 2, and 3. The runlevels for ondemand entries may contain an A, B, or C. The runlevels field of sysinit, boot, and bootwait entries are ignored.
When the system runlevel is changed, any running processes that are not specified for the new runlevel are killed, first with SIGTERM, then with SIGKILL.
Valid actions for the action field are:
respawn
The process will be restarted whenever it terminates (e.g. getty).
wait
The process will be started once when the specified runlevel is entered and init will wait for its termination.
once
The process will be executed once when the specified runlevel is entered.
boot
The process will be executed during system boot. The runlevels field is ignored.
bootwait
The process will be executed during system boot, while init waits for its termination (e.g. /etc/rc). The runlevels field is ignored.
off
This does nothing.
ondemand
A process marked with an ondemand runlevel will be executed whenever the specified ondemand runlevel is called. However, no runlevel change will occur (ondemand runlevels are `a', `b', and `c').
initdefault
An initdefault entry specifies the runlevel which should be entered after system boot. If none exists, init will ask for a runlevel on the console. The process field is ignored.
sysinit
The process will be executed during system boot. It will be executed before any boot or bootwait entries. The runlevels field is ignored.
powerwait
The process will be executed when the power goes down. Init is usually informed about this by a process talking to a UPS connected to the computer. Init will wait for the process to finish before continuing.
powerfail
As for powerwait, except that init does not wait for the process's completion.
powerokwait
This process will be executed as soon as init is informormed that the power has been restored.
powerfailnow
This process will be executed when init is told that the battery of the external UPS is almost empty and the power is failing (provided that the external UPS and the monitoring process are able to detect this condition).
ctrlaltdel
The process will be executed when init receives the SIGINT signal. This means that someone on the system console has pressed the CTRL-ALT-DEL key combination. Typically one wants to execute some sort of shutdown either to get into single-user level or to reboot the machine.
kbrequest
The process will be executed when init receives a signal from the keyboard handler that a special key combination was pressed on the console keyboard.
The documentation for this function is not complete yet; more documentation can be found in the kbd-x.xx packages (most recent was kbd-0.94 at the time of this writing). Basically you want to map some keyboard combination to the "KeyboardSignal" action. For example, to map Alt-Uparrow for this purpose use the following in your keymaps file:
alt keycode 103 = KeyboardSignal
  EXAMPLES
This is an example of a inittab which resembles the old Linux inittab:
# inittab for linux
id:1:initdefault:
rc::bootwait:/etc/rc
1:1:respawn:/etc/getty 9600 tty1
2:1:respawn:/etc/getty 9600 tty2
3:1:respawn:/etc/getty 9600 tty3
4:1:respawn:/etc/getty 9600 tty4
This inittab file executes /etc/rc during boot and starts gettys on tty1-tty4.
A more elaborate inittab with different runlevels (see the comments inside):
# Level to run in
id:2:initdefault:

# System initialization before anything else.
si::sysinit:/etc/rc.d/bcheckrc

# Runlevel 0,6 is halt and reboot, 1 is maintenance mode.
l0:0:wait:/etc/rc.d/rc.halt
l1:1:wait:/etc/rc.d/rc.single
l2:2345:wait:/etc/rc.d/rc.multi
l6:6:wait:/etc/rc.d/rc.reboot

# What to do at the "3 finger salute".
ca::ctrlaltdel:/sbin/shutdown -t5 -rf now

# Runlevel 2&3: getty on console, level 3 also getty on modem port.
1:23:respawn:/sbin/getty tty1 VC linux
2:23:respawn:/sbin/getty tty2 VC linux
3:23:respawn:/sbin/getty tty3 VC linux
4:23:respawn:/sbin/getty tty4 VC linux
S2:3:respawn:/sbin/uugetty ttyS2 M19200

 
FILES
/etc/inittab  

DHCP Server and DHCPD in LINUX



DHCP Server Configuration:
Dynamic Host Configuration Protocol (DHCP) automatically assigns IP addresses and other network configuration information (subnetmask, broadcast address, etc) to computers on a network. A client configured for DHCP will send out a broadcast request to the DHCP server requesting an address. The DHCP server will then issue a "lease" and assign it to that client. The time period of a valid lease can be specified on the server. DHCP reduces the ammount of time required to configure clients and allows one to move a computer to various networks and be configured with the appropriate IP address, gateway and subnet mask. For ISP's it conserves the limited number of IP addresses it may use. DHCP servers may assign a "static" IP address to specified hardware. Microsoft NetBios information is often included in the network information sent by the DHCP server.
DHCP assignment:
1.                  Lease Request: Client broadcasts request to DHCP server with a source address of 0.0.0.0 and a destination address of 255.255.255.255. The request includes the MAC address which is used to direct the reply.
2.                  IP lease offer: DHCP server replies with an IP address, subnet mask, network gateway, name of the domain, name servers, duration of the lease and the IP address of the DHCP server.
3.                  Lease Selection: Client recieves offer and broadcasts to al DHCP servers that will accept given offer so that other DHCP server need not make an offer.
4.                  The DHCP server then sends an ack to the client. The client is configured to use TCP/IP.
5.                  Lease Renewal: When half of the lease time has expired, the client will issue a new request to the DHCP server.
Starting DHCP server: service dhcpd start
(or
/etc/rc.d/init.d/dhcpd start for Red Hat, Fedora and CentOS Linux distributions)
Sample DHCP server config file: (DHCP v3.0.1) /etc/dhcpd.conf
(See
/usr/share/doc/dhcp-3.X/dhcp.conf.sample)
ddns-update-style interim;                                   # Required for dhcp 3.0+ / Red Hat 8.0+
ignore client-updates;

subnet 192.168.1.0 netmask 255.255.255.0 {

        range 192.168.1.128 192.168.1.254;                   # Range of IP addresses to be issued to DHCP clients
           option subnet-mask              255.255.255.0;    # Default subnet mask to be used by DHCP clients
           option broadcast-address        192.168.1.255;    # Default broadcastaddress to be used by DHCP clients
           option routers                  192.168.1.1;      # Default gateway to be used by DHCP clients
           option domain-name              "your-domain.org";
           option domain-name-servers      40.175.42.254, 40.175.42.253;           # Default DNS to be used by DHCP clients
           option netbios-name-servers     192.168.1.100;    # Specify a WINS server for MS/Windows clients.
                                                             # (Optional. Specify if used on your network)

#         DHCP requests are not forwarded. Applies when there is more than one ethernet device and forwarding is configured.
#       option ipforwarding off;

        default-lease-time 21600;                            # Amount of time in seconds that a client may keep the IP address
        max-lease-time 43200;

        option time-offset              -18000;              # Eastern Standard Time
#       option ntp-servers              192.168.1.1;         # Default NTP server to be used by DHCP clients
#       option netbios-name-servers     192.168.1.1;
# --- Selects point-to-point node (default is hybrid). Don't change this unless you understand Netbios very well
#       option netbios-node-type 2;

        # We want the nameserver "ns2" to appear at a fixed address.
        # Name server with this specified MAC address will recieve this IP.

        host ns2 {
                next-server ns2.your-domain.com;
                hardware ethernet 00:02:c3:d0:e5:83;
                fixed-address 40.175.42.254;
        }

        # Laser printer obtains IP address via DHCP. This assures that the
        # printer with this MAC address will get this IP address every time.

        host laser-printer-lex1 {
                hardware ethernet 08:00:2b:4c:a3:82;
                fixed-address 192.168.1.120;
        }
}
Test configuration file for errors with the following command: /etc/rc.d/init.d/dhcpd configtest
(Other distributions may use:
/usr/sbin/dhcpd -f)
Note: The MAC addresses for the static address name server (ns2.your-domain.com),
can be obtained with the command
/sbin/ifconfig:
eth0      Link encap:Ethernet  HWaddr 00:02:C3:D0:E5:83
          inet addr:40.175.42.254  Bcast:40.175.42.255  Mask:255.255.255.0
          inet6 addr: fe80::202:b3ff:fef0:e484/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4070 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3878 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3406445 (3.2 MiB)  TX bytes:439612 (429.3 KiB)
                   
When dhcpd is running it will generate entries in the file: /var/lib/dhcp/dhcpd.leases
lease 192.168.1.128 {
 starts 2 2004/12/01 20:07:05;
 ends 3 2004/12/02 08:07:05;
 hardware ethernet 00:00:e8:4a:2c:5c;
 uid 01:00:00:e8:4c:5d:31;
 client-hostname "Node1";
}
LANs separated by routers: In order to have your DHCP broadcast pass through a router on the the next network, one must configure the router to allow DHCP relay. (Cisco: ip-helper address, Juniper: dhcp-relay) The local lan subnet{ } configuration must come before the configuration directives of the remote LANs.


Time Server and NTP in LINUX


Setup an NTP Server for Time Synchronization

It is a very good idea to set up your system so that it keeps its clock synchronized to the correct time. If you have multiple systems, including an HA configuration, it can be essential. If clocks are not synchronized, correlating events on different systems can make diagnosing any problem very difficult.
You should run an ntp daemon on each of your servers as described below; you can also use those servers to provide a local time source to your phones and other systems within your network; this will be more accurate and less burden on your external network links than each internal system using the external time sources individually.

Installation & Configuration of the NTP Time Server

To install the ntp daemon package:
yum install ntp
To enable it to run by default:
chkconfig ntpd on

Configuring time sources

If you are using a system that has X windows running, you can use the command system-config-date, system-config-time, or dateconfig at a shell prompt (as root) to guide you through setting up the time.
The full documentation is available in man pages
man ntp
and at The Network Time Protocol (NTP) Distribution web site.
You should select from 2 to 4 time sources for your servers. You can get help finding servers at The Public NTP Servers List; a simple approach is to use the NTP Pool Servers (this is what the sipX ISO images use).
Essentially, you just need to add server lines to your /etc/ntp.conf configuration file. If your distribution has one, it's a good idea to configure the same servers in the /etc/ntp/step-tickers file (that is used to set the clock at boot time).

Using a local clock

If you do not have any access to a real NTP server and can't get one, you can still use NTP to synchronize your clocks locally. Pick one system to be your local time source, and install the ntp server there as above. Then look at that documentation in /usr/share/doc/ntp-*/drivers/driver1.html and set the "stratum number" on your chosen system to a lower number than your other systems (a lower number indicates a more precise clock). THen configure that system as the server for the others; even if that time is not strictly correct, at least all your systems will have the same incorrect time, which is better than having many different incorrect times.

To allow your system to be a server for others

Add the line as shown below to the standard /etc/ntp.conf configuration file. It allows access to the time source for other clients on the local LAN segment (here 192.168.5).
File: /etc/ntp.conf
# -- CLIENT NETWORK -------
# Permit systems on this network to synchronize with this
# time service.  Do not permit those systems to modify the
# configuration of this service.  Also, do not use those
# systems as peers for synchronization.
restrict 192.168.5.0 mask 255.255.255.0 nomodify notrap

Start the daemon

/sbin/service ntpd start
Important: After starting the ntp deamon it will be some time before its clock is sufficiently synchronized to provide a clock to the other clients on your network. If everything is properly configured, this will resolve itself. If you try to use the ntpdate command to set the time from some other system before the servers clock is sufficiently synchronized, you will see the error "ntpdate[10560]: no server suitable for synchronization found"

How to update the TimeZone files

Assuming you have ntpd installed and working properly...
 1. Login to the server as root.
 
 2. Stop ntpd: service ntpd stop
 
 3. Get the latest time zone files: wget ftp://elsie.nci.nih.gov/pub/tzdata2008a.tar.gz
       You might want to check the pub folder on elsie.nci.nih.gov for the most current timezone file.
 
 4. Expand: tar xzf tzdata2008a.tar.gz
 
 5. Remove the old localtime link: rm -f /etc/localtime
 
 6. Link to the proper timezone file (for me): ln -s /usr/share/zoneinfo/America/New_York /etc/localtime
 
 7. Restart ntpd: service ntpd start

How to Configure Clients

Phone Clients

SIP phones can be synchronized with an NTP server, but usually do not pick up the server address through DHCP. For sipX ConfigServer managed phones (i.e. Polycom, Snom, Cisco, Grandstream, and others) the NTP server address is configured as part of the phone's profile. Best practice is to create a device group for which the NTP server is set, so that it does not have to be set for every phone individually.

Windows NTP Clients

You can add your new NTP server to your Windows client. Here's how:
  1. Click on the time at the bottom right hand side of your screen.
  2. Click on the "Internet Time" tab of the dialog box
  3. Click the check box labeled "Automatically synchronize with an Internet time server" and enter the name or IP address in the box underneath it.
  4. Click on the "Update Now" button
You will get a message saying "Your time has been successfully synchronized" when the operation is complete.
Note: If your Windows 2000 or Windows XP system is part of a Windows Domain, then time synchronization is provided through the Windows domain controller and therefore the tab "Internet Time" is not available in the time dialog box.

DHCP Distribution of NTP Server Information

An NTP server address can be distributed as an option with the DHCP server. Some clients will pick it up automatically then when obtaining an IP address:
File: /etc/dhcpd.conf
 option ntp-servers            192.168.5.145;

Troubleshooting

The ntpq utility program is used to query NTP servers. You can use the following two commands to print a status of the server if executed on the ntpd host. Refer to the man pages of ntpq.
ntpq -p
 
echo rv | ntpq
This command can be executed on a Linux client to print the status of its clocks without actually updating the time (-q option).
ntpdate -q sipx.domain.com
 
date