Linux Tips & Tricks

Greping on specific files

find . -type f -name '*\.h' -print0 | xargs -0 grep -olR mypattern

Checking for changes

Here's how to check what changes were made to a Linux filesystem before/after performing a task:

touch /tmp/B4

# Perform task

#ignore object files

find /my/dir -newercm /tmp/B4 -not -name "*\.o" > changes.txt

RPM, Yum

This is the RedHat equivalent of dpkg and apt-* on Debian distros. Start with updating the list of packages available in the repositories that is cached on your host: yum update.

If you want to upgrade installed packages, run "yum upgrade". If this involves upgrading the kernel, you'll need to reboot for the new kernel to be used.

List installed packages

rpm -qa

yum list installed

Find if a package is available in the repositories

yum list myapp

yum search myapp (this searches for the string "myapp" even in descriptions)

Retrieve information about a package

yum info myapp

Install/remove a package

yum install myapp

yum remove myapp

List files provided by an installed package

rpm -ql myapp

Remove unneeded files

yum clean all

MC, Midnight Commander

If MC displays funny characters, make sure the terminal emulator uses UTF8 encoding.

Smooth scrolling

vi ~/.mc/ini (per user) or /etc/mc/mc.ini (system-wide):


Make both panels display the same directory

ALT+i. If NOK, try ESC+i

Navigate through history

ESC+y to go back to the previous directory, ESC+u to go the next

Options > Configuration > Lynx-like motion doesn't go through the navigation history but rather jumps in/out of a directory so the user doesn't have to hit PageUp followed by Enter

Loop through all items starting with the same letter

CTRL+s followed by the letter to jump to the first occurence, then keep hitting CTRL+s to loop through the list

Customize keyboard shortcuts

Check mc.keymap

Using CRON

CRONd (eg. /usr/sbin/crond) is the daemon that handles tasks as set in CRONTAB files. CRONTAB files are located in a common directory (eg. /var/spool/cron/crontabs) where the CRONTAB command (eg. /usr/bin/crontab) will look for them.

Typically, each user has a single CRONTAB file. The CRONTAB command will tell CRONd to reread all CRONTAB files, so there's no need to restart the daemon.

Each CRONTAB file includes the following information:

While the command can include spaces, all the other columns are space-separated.

Here's an example of a two-step command that will run each Monday morning at 2:30:

30 2 * * 1 (cd /home/fred/test; make)

To check your cron jobs, run "crontab -l". To edit your crontab, run "crontab -e".

To avoid receiving e-mail, append ">/dev/null 2>&1" to a job, and restart cron with eg. /etc/rc.d/cron restart.

Linux Boot Sequence

  1. The BIOS loads and execute the first 512 bytes off the disk (/dev/hda), a.k.a. the Master Boot Record (MBR)
  2. The boot loader executes the first 512 bytes of the partition that you selected (if more than one is available). Advanced boot loaders like Grub or System Commander offer more features than the DOS boot loader. Some boot loaders save their configuration in the MBR (hence the need to remember to run the lilo command after editing it to save your changes to the master boot record on disk), while others save their data in a partition
  3. The partition loader now decompresses the kernel in RAM, and runs it
  4. The kernel initialises devices, loads the root partition, loads the /sbin/init process (PID=1) that reads its configuration information from /etc/inittab to know which boot scripts to run and at which run-level to start, and which processes to spawn. The actual scripts are in  /etc/rc.d/init.d, and /etc/rc.d/rc#.d only contains links
  5. Init launches Getty to open consoles, and launches Login to present the user with a logon prompt

Note that once the Linux kernel has been loaded by lilo, it looks in "all the usual places" for init and runs the first copy it finds.

More information: Inside the Linux boot process by Tim Jones

LILO as boot loader

Caution: Be sure to make a backup copy of the file before making any changes using eg. dd if=/dev/hda of=/boot/secure.mbr bs=512 count=1

Here's a basic /etc/lilo.conf:

boot=/dev/hda #tells LILO to install itself on the first hard disk on the first IDE controller.
map=/boot/map #contains infos on the different OS's
install=/boot/boot.b #file to contain 2nd part of booting process
message=/boot/message #screen that LILO displays
prompt #show you whatever is referenced in the message line
timeout=50 #In 10's of seconds, amount of time LILO will wait for user input
default=linux #default OS; refers to the label line below
image=/boot/vmlinuz-2.4.0-0.43.6 #specifies the linux kernel to boot
    label=linux #label displayed by LILO
    initrd=/boot/initrd-2.4.0-0.43.6.img # initial ram disk image used at boot time to initialize devices
                                                      # that makes booting the kernel possible
    read-only # make the root partition read-only + cannot be altered during the boot process.
    root=/dev/hda5 # tells LILO what disk partition to use as the root partition
other=/dev/hda1 #Let the user choose the Other OS

The map file contains the kernel's physical location on disk, and lets Lilo access the kernel via BIOS functions stating head, track and cylinder.

Note: Make sure you run /sbin/lilo after making changes to its configuration file! If you change LILO's configuration file or install a new kernel, you must rewrite the Stage 1 LILO boot loader to the MBR by issuing the /sbin/lilo -v -v command (-v -v gives out more information on what LILO is doing).

Based on the LILO, Linux Crash Rescue HOW-TO, here's how to boot from a live CD, and repair a broken LILO that displays L99 99 99 due to some hardware issue (eg. you removed a drive without first running /sbin/lilo):

  1. Get infos about the faulty drive:

    # fdisk /dev/hda

    Command (m for help): p         (Gives you list of partitions)
    Command (m for help): q
  2. Create end-points to mount:

    # mkdir /mnt/hda1
    # mount -t ext3 /dev/hda1 /mnt/hda1
    # ls /mnt/hda1

    If /dev/hda1 turns out not to be the root partition, try the other partitions until you find it

  3. Edit lilo.conf so that it matches the new configuration with just one drive:

    vi /mnt/hda1/etc/lilo.conf

  4. Run lilo by reconfiguring Linux so that the root partition is no longer the live CD, but rather, the one on the faulty drive:

    # chroot /mnt/hda1 /sbin/lilo

  5. Check /mnt/hda1/etc/fstab, and make sure it fits the configuation (eg. if you removed a drive, make sure it is no longer listed in fstab)

  6. Reboot: Lilo should now be happy


What is L 99 99?

"99 indicates an invalid second stage index sector; this error relates to a disk read error, normally an issue with reading the map file. Generally the underlying cause is some confusion over BIOS device codes; i.e., the map file is not on the BIOS device code which was used for the disk in question when LILO was installed.

Two solutions:

  1. Use:   disk=/dev/hdX   bios=0x8N            #   X=a,b,c,...   N=0,1,2,...  to resolve the BIOS codes explicitly.
  2. Use LILO version 22.5 or later (22.5.8 is current), which boots by disk VolumeID, resolving the BIOS codes at boot time.   

#2 is the more fault tolerant approach."


Working more productively with bash 2.x


Working with an SMTP server

telnet smtp
From: Bill G <>
To: You <>
Subject: Hi there

What's up?

Working with a POP server

telnet pop3
USER jdoe
PASS secret


tar -cf - ./sourcedir | (cd /targetdir ; tar xf -)

Turning off beeping


setterm -blength 0, or set bell-style none. Add this to your startup script (~/.bashrc)

edit ~/.inputrc (or /etc/inputrc), and insert this line: set bell-style visible


xset -b

From the Visual Bell HOWTO:

The bell in xterm defualts to be audible, but you can use the "-vb" command line option and the "xterm*visualBell: true" resource to turn it to a visible flash. You can toggle visible/audible signaling at run-time by using the menu invoked by control--left-mouse-button. If you run X you most likely won't need the following information.


~/.exrc : set ????


Removing MBR

dd if=/dev/zero of=/dev/hda bs=512 count=1

Compiling a kernel



To compile a new kernel, you'll first need to install a copy of the source code in /usr/src/linux, either through RPM or by downloading the tarball from eg. .

 [root@mylinux linux]# l /boot/
-rw-r--r--    1 root     root        23108 Feb 23  2001 message
-rw-r--r--    1 root     root         5824 Feb 23  2001 boot.b
-rw-r--r--    1 root     root          612 Feb 23  2001 chain.b
-rw-r--r--    1 root     root        13598 Apr  9  2001 module-info-2.4.2-2
-rw-r--r--    1 root     root       410454 Apr  9  2001
-rwxr-xr-x    1 root     root      2271899 Apr  9  2001 vmlinux-2.4.2-2*
-rw-r--r--    1 root     root       785602 Apr  9  2001 vmlinuz-2.4.2-2
-rw-r--r--    1 root     root       368079 Oct 24 06:18 initrd-2.4.2-2.img
-rw-r--r--    1 root     root          512 Oct 24 06:18 boot.0800
lrwxrwxrwx    1 root     root           14 Oct 24 05:59 kernel.h -> kernel.h-2.4.2
-rw-r--r--    1 root     root          405 Oct 24 06:00 kernel.h-2.4.2
-rw-------    1 root     root        16384 Oct 24 06:18 map
lrwxrwxrwx    1 root     root           19 Oct 24 05:59 module-info -> module-info-2.4.2-2
lrwxrwxrwx    1 root     root           15 Oct 24 05:59 vmlinuz -> vmlinuz-2.4.2-2
lrwxrwxrwx    1 root     root           18 Oct 24 04:20 ->
  1. cd /usr/src/linux
  2. make menuconfig
  3. make dep
  4. make clean
  5. make bzImage (binary saved on your hard drive) OR make bzdisk (saved on a floppy)
  6. make modules
  7. make modules_install
  8. Regardless of whether you ran bzImage or bzdisk, you'll have to copy the kernel and to the ad hoc location:
    1. cp /usr/src/linux/
  9. edit /etc/lilo.conf, and add this new kernel (assuming the root partition is located in /dev/hda1):
    image = /usr/src/linux/arch/i386/boot/bzImage
    label = new
    root = /dev/hda1
  10. /sbin/lilo
  11. Reboot.
  12. At the Linux: prompt, hit the TAB key, and choose the "new" kernel.

If the new kernel works as expected, you should move it from /usr/src/linux/arch/i386/boot/ into the standard location for kernels, eg. /boot for the RedHat distro: cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-2.2.16new , followed by coying the system map: cp /usr/src/linux/ /boot/ . Remember to edit lilo.

Important: If you are booting from a SCSI disk and want to load the SCSI driver as a module, run mkinitrd to create an image that contains the proper SCSI driver. The image is loaded by LILO so that the kernel can access the root filesystem. mkinitrd uses information in /etc/conf.modules.

If you are lazy and don't care about keeping the old kernel around, cd /usr/src/linux ; make install : This will move bzImage into /boot as vmlinuz, along with the new system map. You'll still need to remove the temporary, new section that you added in lilo.conf, and run /sbin/lilo to refresh its configuration.


Listing partitions

fdisk -l

Using Wget

To download a whole web site

wget -cN --http-user=ME --http-passwd=MYPASSWD, where -c is "resume getting a partially-downloaded file", -N "don't re-retrieve files unless newer than local"

To download a whole FTP site

wget  -m --passive-ftp, where -m is "mirror", ie. equivalent to -r -N -l inf -nr

Using Wget to connect to a remote web server that runs on a non-standard port (ie. different from TCP 80)


Finding files that were created recently

Find files that were created within the last 10 minutes: find . -mmin +0 -mmin -10. As an alternative: find . -cmin -10

Changing access rights on directories only

find . -type d -exec chmod 755 {} \;
Note: chmod -R 755 ./* also affects files, not just directories

Raw file viewing

cat myrawfile.txt| tr -d '\r' > myrawfile.txt.output
hexdump -c myrawfile.txt.output  | head

Regular expressions in Vi

To get rid of any commented line in a configuration line: :%s/^#.*$//g . To look for one or more occurrence of a character, use the \+ pattern.

Finding changes made by an application

If you need to know how the filesystem was changed after running an application, eg. installing/removing software:

# ls -aR /tmp > b4.txt

(run the application you want to watch)

# ls -aR /tmp > after.txt
# diff -u b4.txt after.txt


[FTPd] Anonymous uploading

  1. Install VsFTPd
  2. mkdir /var/ftp/incoming
  3. chown ftp.ftp /var/ftp/incoming
  4. Use the following /etc/vsftpd/vsftpd.conf:

    #chroot anonymous to this directory, if you wish

Listing TCP apps listening on ports

netstat -nltp

Add Linux to NT's bootloader

This is needed if NT was already installed on a hard disk on which you added Linux. The goal here is to copy LILO's bootloading part that lives in the Linux partition into the NT partition so that Linux shows up in the list of available OS's in NT's multiboot menu.

  1. Boot into Linux using the boot floppy that you created while installing Linux (you did create a boot floppy, right?)
  2. lilo -s /tmp/linux.bin
  3. Copy /tmp/linux.bin onto a DOS-formatted floppy
  4. Reboot into NT, and copy linux.bin from the floppy into the partition where boot.ini lives
  5. Edit C:\boot.ini and add Linux, eg.:

    [boot loader]

    [operating systems]
    multi(0)disk(0)rdisk(0)partition(1)\WINNT="Microsoft Windows 2000 Professional" /fastdetect

Using Linux with no login prompt

In LILO, use "linux init=/bin/sh"

Adding/removing routes

route add default gw ppp0

Finding which program is listening on a port

lsof -p process-id


netstat -nltp

Installing a DLink DE-220E ISA card

  1. Download the setup program from the DLink web site (
  2. Run its setup.exe from a DOS window, or boot with a DOS floppy. Set its IRQ, I/O, and connection (BNC or RJ-45) settings, and run the tests to check that the card works
  3. If the Linux kernel has no support for ISA NE2000, build a new kernel with that option, and boot with this new kernel
  4. If the card was properly detected by Linux, set an IP configuration for the NIC by editing (RedHat) /etc/systemconfig/network, and /etc/systemconfig/network-scripts/eth0 (eth1 if you already use another NIC as eth0 and want to add a second NIC to let the Linux box act as a router or firewall)
  5. Reboot, and check that you can use both NICs. If you cannot ping, make sure no firewall is enabled on either or both hosts (either check their log files, or connect to a specific application, eg. Apache or Sendmail.) 

Backing up remote Windows hosts

You'll need a kernel that supports smbfs, so we can mount remote Windows hosts in the file system. They will be mounted in /acme, and each host will reside in a sub-directory (eg. /acme/jdoe, /acme/janed, etc.), with actual files in yet another sub-directory, backup/ . We will copy all *.cpp and *.h files in /backup.

exec > /root//backup.log 2>/root/backup.err
for host in /acme/*/backup
    mount $host
    sleep 2
while true
    find /acme \( -name '*.cpp' -o -name '*.h' \) -exec /bin/cp -pPRubVt \{\} /backup \;
exit 0  



FTP sessions while in text-mode:

Xterm while under X: stty erase ^H

When connecting through telnet/SSH:


Up = ^[[A

Change how man pages are displayed

In case man pages hide some words because of the colors used,


To activate NUM LOCK (with no error when connecting remotely)