Wednesday, March 30, 2011

DHCP Server Configuration

DHCP Server Configuration for Linux

This guide will help you setup a dhcp server to provide network configuration information to clients on the network. These instructions were written with Red Hat 7.x systems in mind but the basic concepts provided here can be applied to other distributions as well.


1. Download dhcp rpm package from Red Hat and install:

# rpm -ivh dhcp-2.0pl5-8.i386.rpm

2. Open file /etc/sysconfig/dhcpd and edit the first line as follows:

DHCPDARGS=eth1

Replace 'eth1' above with the network interface that you want to use for dhcp; this should be an internal network interface; denial of service attacks are possible if dhcp is running on an external interface.

3. Copy /usr/share/doc/dhcp-2.0pl5/dhcpd.conf.sample to /etc

# cp /usr/share/doc/dhcp-2.0pl5/dhcpd.conf.sample /etc/dhcpd.conf

This sample file is a good starting point for our /etc/dhcpd.conf file, which by default is not installed. Alternatively, copy the file from a working server.

4. Edit /etc/dhcpd.conf to suit your needs. An example file is included below for reference:

#################file begin######################
subnet 10.0.0.0 netmask 255.255.255.0 {
# --- default gateway
option routers 10.0.0.1;
option subnet-mask 255.255.255.0;

# option nis-domain "mydomain.com";
option domain-name "mydomain.com";
option domain-name-servers 216.227.56.120, 64.34.4.36;

option time-offset -28800; # Pacific Standard Time
# option ntp-servers 192.168.1.1;
# 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;

range 10.0.0.50 10.0.0.254;
default-lease-time 604800;
max-lease-time 604800;

host test {
# option dhcp-client-identifier "test";
hardware ethernet 00:e0:18:90:28:b2;
fixed-address 10.0.0.10;
}
# we want the nameserver to appear at a fixed address
# host ns {
# next-server marvin.redhat.com;
# hardware ethernet 12:34:56:78:AB:CD;
# fixed-address 207.175.42.254;
# }
}
########################file end##########################

Notes: specific settings always override global settings; in the above, the range 10.0.0.50 to 10.0.0.254 have been set side for dynamic hosts; this allows anything between 10.0.0.1 and 10.0.0.49 to be set aside as static ips. In the example, host 'test' is given a static ip using its mac address. The option 'dhcp-client-identifier' may work as an alternative to mac address, but may require some additional configuration on the client. The max lease time of 604800 translates to 7 days. Lease times are automatically renewed by clients once 50% of the expiration date is reached. Because of this, very long lease times should be unnecessary. If a very long one is required, provide the client a static ip using the host declaration. Also, the option time-offset setting is in seconds according to the manual page; Red Hat's configuration document erroneously lists this setting in hours. Use option host-name "apex.example.com" in a host declaration to provide hostnames to clients.

5. Check that the lease database has been created; the rpm should create this file automatically; if not, create the file:

# touch /var/lib/dhcp/dhcpd.leases

The lease database is recreated from time to time so that it is not too large. First, all known leases are saved in a temporary lease database. The dhcpd.leases file is renamed dhcpd.leases~, and the temporary lease database is written to dhcpd.leases.

The DHCP daemon could be killed or the system could crash after the lease database has been renamed to the backup file but before the new file has been written. If this happens, there is no dhcpd.leases file that is required to start the service. Do not create a new lease file if this occurs. If you do, all the old leases will be lost and cause many problems. The correct solution is to rename the dhcpd.leases~ backup file to dhcpd.leases and then start the daemon.

6. Run 'setup' and check dhcpd to have it load at system boot

7. Start/restart the server

# service dhcpd start (restart)

Changes to the file /etc/dhcpd.conf require the dhcp server to be restarted

8. Test to make sure it works.

Reserved blocks

Increase available disk space by decreasing the reserved blocks in extended filesytems ext2/3/4 using tune2fs

Have you ever noticed that after formatting a partition or external storage device - hard disk etc. into ext2/3/4 decreases the total available disk space by some amount? That's because the file-system reserves some part of the space to privileged processes. This is done to make sure that in case of file-system fill up, when user processes may not be allowed to write to disk, privilege system processes(or root user) may still be able to function properly(write on disk) e.g. syslogd etc.
This space by default is around 5% of the total. And considering the amount of hard-drive or partitions available these days, this reserved memory could be huge e.g. 5% of 500GB hard disk is 25GB, which is too much for critical cases. And this memory is only needed in the root(/) partition and it doesn't make much sense to reserve blocks on /home partitions or external storage devices, because privileged processes don't usually write on home partition.
It should be noted that this reserved space is also used to reduce fragmentation. So, it plays a major role in avoiding fragmentation by preventing the disk to be filled completely.
So, if you think 5% of your partition/disk is more than enough for reserved blocks then you can save some space by decreasing this amount. You can do this by using the tune2fs utility.
[root]# tune2fs -m 3 /dev/sda1
The -m option is used to set the new percentage share, we used 3%. /dev/sda1 is my root(/) partition.
We can set the value to 0 for home partition, assuming that you haven't configured any privileged process to write in /home directory. A word of warning here, since these reserved blocks are also used to prevent fragmentation, leaving some amount is a good idea.
[root]# tune2fs -m 0 /dev/sda2
where /dev/sda2 is the home partition.
You can even check the current number of reserved blocks on a partition or disk using tune2fs.
[root]# tune2fs -l /dev/sda2 | grep -i reserve
Reserved block count: 1257387
Reserved GDT blocks: 1018
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)

# tune2fs -l /dev/mapper/VolGroup00-LogVol00 | grep -i block

Block count: 8568832

Reserved block count: 428441

Free blocks: 2247916

First block: 0

Block size: 4096

Reserved GDT blocks: 1024

Blocks per group: 32768

Inode blocks per group: 512

Reserved blocks uid: 0 (user root)

Reserved blocks gid: 0 (group root)

Journal backup: inode blocks

[root@util100 ~]#

(428441 / 8568832) * 100 = 4.99

Reserved blocks

Increase available disk space by decreasing the reserved blocks in extended filesytems ext2/3/4 using tune2fs

Have you ever noticed that after formatting a partition or external storage device - hard disk etc. into ext2/3/4 decreases the total available disk space by some amount? That's because the file-system reserves some part of the space to privileged processes. This is done to make sure that in case of file-system fill up, when user processes may not be allowed to write to disk, privilege system processes(or root user) may still be able to function properly(write on disk) e.g. syslogd etc.

This space by default is around 5% of the total. And considering the amount of hard-drive or partitions available these days, this reserved memory could be huge e.g. 5% of 500GB hard disk is 25GB, which is too much for critical cases. And this memory is only needed in the root(/) partition and it doesn't make much sense to reserve blocks on /home partitions or external storage devices, because privileged processes don't usually write on home partition.

It should be noted that this reserved space is also used to reduce fragmentation. So, it plays a major role in avoiding fragmentation by preventing the disk to be filled completely.

So, if you think 5% of your partition/disk is more than enough for reserved blocks then you can save some space by decreasing this amount. You can do this by using the tune2fs utility.

[root]# tune2fs -m 3 /dev/sda1

The -m option is used to set the new percentage share, we used 3%. /dev/sda1 is my root(/) partition.

We can set the value to 0 for home partition, assuming that you haven't configured any privileged process to write in /home directory. A word of warning here, since these reserved blocks are also used to prevent fragmentation, leaving some amount is a good idea.

[root]# tune2fs -m 0 /dev/sda2

where /dev/sda2 is the home partition.

You can even check the current number of reserved blocks on a partition or disk using tune2fs.

[root]# tune2fs -l /dev/sda2 | grep -i reserve

Reserved block count: 1257387
Reserved GDT blocks: 1018
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)

# tune2fs -l /dev/mapper/VolGroup00-LogVol00 | grep -i block

Block count: 8568832

Reserved block count: 428441

Free blocks: 2247916

First block: 0

Block size: 4096

Reserved GDT blocks: 1024

Blocks per group: 32768

Inode blocks per group: 512

Reserved blocks uid: 0 (user root)

Reserved blocks gid: 0 (group root)

Journal backup: inode blocks

[root@util100 ~]#

(428441 / 8568832) * 100 = 4.99

Wednesday, March 16, 2011

How to Get Detailed Information About a Linux PID, Process ID

To get detailed information about a Linux PID you can use the strace command. If strace is not installed it can be installed via yum using the command below.

Strace Install:

1yum install strace

Below is a sample output from using the strace command on a Litespeed web server process.

Sample Output:


[root@idev log]# strace -p 14387


Realtime Example:

#ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
%CPU PID USER COMMAND
12.5 22288 root cp -Rvp /oracle/data/ /mnt/backup
0.4 31293 nagios /opt/nagios/bin/nagios -d /opt/nagios/etc/nagios.cfg
0.3 16481 root top
0.2 4024 root hpasmd
0.1 4645 root cmanicd
0.1 23378 tinydns /usr/local/bin/dnscache
0.0 9 root [kacpid]
0.0 9880 apache /opt/apache/bin/httpd
0.0 8 root [khelper]

# strace -cp 22288
Process 22288 attached - interrupt to quit
Process 22288 detached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
56.29 0.459280 20 23088 write
43.71 0.356596 15 23089 read
------ ----------- ----------- --------- --------- ----------------
100.00 0.815876 46177 total