Tuesday, February 22, 2011

Using find command in linux

"find" command is one of the important commands in Linux. It makes easy for us to find a file or a directory.We can also find the unused files from last few days and delete it. "find" command also makes it easy to search for files which have been modified few hours or days back.
Below are the some of the examples of using  find command :

Find files using name :
[root@localhost ~]#find /root -name anaconda-ks.cfg

Find files ignoring the case
[root@localhost ~]#find /usr/src -iname  makefile

Find with mindepth and maxdepth i.e limit search to specific directory
The command below gives the list of passwd file in whole /
[root@localhost ~]#find / -name passwd
/usr/bin/passwd
/usr/share/doc/nss_ldap-264/pam.d/passwd
/usr/lib/news/bin/auth/passwd
/etc/passwd
/etc/pam.d/passwd

Find files with inode numbers
Lets create the test file and note down its inode number

[root@localhost ~]# touch test-file
[root@localhost ~]# ls -il test-file
1632299 -rw-r--r-- 1 root root 0 2011-02-22 22:44 test-file

Now lets find the file by inode number
[root@localhost ~]# find -inum 1632299
./test-file

Find the  file by inode number  and rename it
[root@localhost ~]# find -inum 1632299 -exec mv {} new-test-file \;
[root@localhost ~]# ls -il
1632299 -rw-r--r--  1 root root     0 2011-02-22 22:44 new-test-file

Now if we want to search files just one directory level down
[root@localhost ~]#find / -maxdepth 2 -name passwd
/etc/passwd

If we want to search files two directories level down
/usr/bin/passwd
/etc/passwd
/etc/pam.d/passwd

If we want to find the files between subdirectory level 2 and 4
[root@localhost ~]#find / -mindepth 2 -maxdepth 5 -name passwd
/usr/bin/passwd
/etc/passwd
/etc/pam.d/passwd

Find the files and execute commands on them

first we create some files with .tmp and .c extension in /tmp directory 
[root@localhost ~]#cd /tmp
[root@localhost tmp]# touch file{1,2,3,4}.tmp
[root@localhost tmp]#touch file{1,2,3,4}.c
[root@localhost tmp]#l
file1.c    file1.tmp  file2.cpp  file3.c    file3.tmp  file4.cpp
file1.cpp  file2.c    file2.tmp  file3.cpp  file4.c    file4.tmp

Now lets search .tmp files and remove them
[root@localhost ~]#find /tmp -name *.tmp -exec rm -fr {} \;

Find all the files which has rw permission to owner/user

[root@localhost ~]#find  /root -perm -u=rw -type f -exec ls -l {} \;

Find files world writable and list them
[root@localhost ~]#find /root -perm -o=rw -exec ls -ld {} \;

Find file which has 666 permission:


[root@localhost ~]# find /root -perm 666 -type f -exec ls -l {} \;
No result as no file with 666 permission

Let's create a file with with permission 666
[root@localhost ~]# touch file11
[root@localhost ~]# chmod 666 file11

[root@localhost ~]# find /root -perm 666 -type f -exec ls -l {} \;
-rw-rw-rw- 1 root root 0 2011-02-22 23:24 /root/file11









Thursday, January 20, 2011

DHCP Server

Dynamic Host Configuration Protocol
Works on following ports:
# cat /etc/services | grep dhcp
bootpc          68/tcp          dhcpc           # BOOTP client
bootpc          68/udp          dhcpc
dhcpv6-client   546/tcp
dhcpv6-client   546/udp
dhcpv6-server   547/tcp
dhcpv6-server   547/udp
dhcp-failover   647/tcp                 # DHCP Failover
dhcp-failover   647/udp                 # DHCP Failover
dhcp-failover2  847/tcp                 # dhcp-failover 2
dhcp-failover2  847/udp                 # dhcp-failover 2
qip-qdhcp       2490/tcp                # qip_qdhcp
qip-qdhcp       2490/udp                # qip_qdhcp

bootps          67/tcp                          # BOOTP server
bootps          67/udp

DHCP is used for allocating the IP Addresses Automatically i.e Dynamically.

DHCP Lease Allocation Process:
It is very important to understand the lease allocation process of DHCP.

Allocation process is done in following 4 steps:
1. DHCPDISCOVER message: Client requests the DHCP Server in the network in the form  of broadcast packet
2. DHCPOFFER message: Any of the available DHCP server gives response to the Discover message
3. DHCPREQUEST message: A client can receive DHCP offers from multiple servers, but it will accept only one DHCP offer and broadcast a DHCP request message
4. DHCPACK message: When the DHCP server receives the DHCPREQUEST message from the client, the configuration process enters its final phase. The acknowledgement phase involves sending a DHCPACK packet to the client. This packet includes the lease duration and any other configuration information that the client might have requested. At this point, the IP configuration process is completed.

Now we will configure the basic DHCP server which will allocate IP, Netmask,Gateway and DNS information to the cleint.This configuration has been tested on CentOS- 5.5 

First search for required RPMs
# rpm -qa | grep -i dhcp
dhcpv6_client-0.10-33.el5
dhcp-3.0.5-3.el5
dhcp-devel-3.0.5-3.el5
dhcpv6-0.10-33.el5

If not available install it using yum
# yum -y install dhcp*

Edit the /etc/dhcpd.conf file as given below :
# vim /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
option wpad-url code 252 = text;
one-lease-per-client true;
subnet 192.168.1.0 netmask 255.255.255.0 
{
option routers                               192.168.1.1; # Default Gateway
option subnet-mask                       255.255.255.0;
option nis-domain                         "example";
option domain-name                     "example.com";
option domain-name-servers         192.168.1.3;
option time-offset                          -18000; # Eastern Standard Time
option ntp-servers                          192.168.1.1;
option netbios-name-servers          192.168.1.1;
range dynamic-bootp                     192.168.1.102 192.168.1.220;
default-lease-time                           86400;
max-lease-time                               604800;
next-server                                     192.168.1.3;  # For PXE-Boot
filename                                         "pxelinux.0";     # For PXE-Boot

}
# We want Lisa to appear at a fixed address
#1.Lisa Dsouza
      host node03.example.com
              {hardware ethernet  00:21:97:2C:09:A5;
               fixed-address 192.168.1.108;
              }

Following two lines will be used(as in the above configuration file) if you are doing the over the  network installation using DHCP/BOOTP and NFS. This will be covered in my other posts.
next-server                                     192.168.1.3;     # For PXE-Boot
filename                                         "pxelinux.0";     # For PXE-Boot

Some of the other configurations would be :
  • Fail Over DHCP Server : Primary and Secondary DHCP Servers
  • Configuring the Multiple DHCP Servers on Multiple LAN cards serving multiple subnets
  • Configuring single DHCP server serving multiple subnets
  • Configuring DHCP Relay Agent
  • Configuring DHCP PXE-BOOT Server
Most of the above mentioned options/configurations will be posted soon.