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):
panel_scroll_pages=0
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:
- minute: integer
- hour: integer
- day of the month: integer
- month: integer, comma-separated list, dash-separated interval, or *
- day of the week: integer, comma-separated list, dash-separated interval,
or * (where 0=Sunday)
- command to run
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
- The BIOS loads and execute the first 512 bytes off the disk (/dev/hda),
a.k.a. the Master Boot Record (MBR)
- 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
- The partition loader now decompresses the kernel in RAM, and runs it
- 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
- 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
- label=dos
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):
- Get infos about the faulty drive:
# fdisk /dev/hda
Command
(m for help): p (Gives you
list of partitions)
Command (m for help): q
- 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
- Edit lilo.conf so that it matches the new configuration with just one
drive:
vi /mnt/hda1/etc/lilo.conf
- 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
- 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)
- Reboot: Lilo should now be happy
LILO mini-HOWTO
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:
- Use: disk=/dev/hdX bios=0x8N #
X=a,b,c,... N=0,1,2,... to resolve the BIOS
codes explicitly.
- 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."
Bash
Working more productively
with bash 2.x
Em@il
Working with an SMTP server
- telnet mail.acme.com smtp
- EHLO me.acme.com
//EHLO is for ESMTP; Use HELO for SMTP
- MAIL FROM: <billg@microsoft.com>
- RCPT TO: <you@acme.com>
- DATA
- From: Bill G <bill@microsoft.com>
To: You <you@acme.com>
Subject: Hi there
What's up?
.
- QUIT
Working with a POP server
- telnet mail.acme.com
pop3
- USER jdoe
- PASS secret
- STAT
- LIST
- LIST 2
- RETR 2
- DELE 2
- QUIT
Archiving
tar -cf - ./sourcedir | (cd /targetdir ; tar xf -)
Turning off beeping
Bash
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
X
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.
Vi
~/.exrc : set ????
Kernel
Removing MBR
dd if=/dev/zero of=/dev/hda bs=512 count=1
Compiling a kernel
Tips
- As compiling a kernel is resource-intensive, make sure you run a test
to check that your RAM is OK: Some segfaults can be due to faulty hardware
(Using Test
Suites to Validate the Linux Kernel, Memtest86,
Linux hardware
stability guide, Part 1)
- If you are building a new kernel that has the same version number
as the one you are currently using, move the current module directory out of
the way so you don't get dependency errors when you reboot with your new kernel
(assuming you are running Linux 2.2.16) : mv /lib/modules/2.2.16 /lib/modules/2.2.16.old
- Make sure that you have installed the source code in /usr/src/linux,
or that you added a symbolic link to it (eg. ln -s linux-2.2.16 linux)
- Make sure you have the latest version of GCC because compiling could
fail due to some bugs in it.
Procedure
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. http://www.kernel.org/pub/ .
- [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 System.map-2.4.2-2
- -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
System.map -> System.map-2.4.2-2
- cd /usr/src/linux
- make menuconfig
- make dep
- make clean
- make bzImage (binary saved on your hard drive) OR make bzdisk (saved
on a floppy)
- make modules
- make modules_install
- Regardless of whether you ran bzImage or bzdisk, you'll have to copy
the kernel and System.map to the ad hoc location:
- cp /usr/src/linux/
- 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
- /sbin/lilo
- Reboot.
- 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/System.map /boot/System.map-2.2.16new
. 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.
Files
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
Networking
[FTPd] Anonymous uploading
- Install VsFTPd
- mkdir /var/ftp/incoming
- chown ftp.ftp /var/ftp/incoming
- Use the following /etc/vsftpd/vsftpd.conf:
listen=YES
syslog_enable=YES
anonymous_enable=YES
anon_upload_enable=YES
write_enable=YES
anon_mkdir_write_enable=YES
#chroot
anonymous to this directory, if you wish
anon_root=/var/ftp/incoming
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.
- Boot into Linux using the boot floppy that you created while installing
Linux (you did create a boot floppy, right?)
- lilo -s /tmp/linux.bin
- Copy /tmp/linux.bin onto a DOS-formatted floppy
- Reboot into NT, and copy linux.bin from the floppy into the partition
where boot.ini lives
- Edit C:\boot.ini and add Linux, eg.:
[boot loader]
timeout=30
default=multi(0)disk(0)rdisk(1)partition(1)\WINNT
[operating
systems]
multi(0)disk(0)rdisk(0)partition(1)\WINNT="Microsoft Windows
2000 Professional" /fastdetect
c:\linux.bin="Linux"
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
OR
netstat -nltp
Installing a DLink DE-220E ISA card
- Download the setup program from the DLink web site (ftp://ftp.dlink.com/NIC/de220/Driver/de220.exe)
- 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
- If the Linux kernel has no support for ISA NE2000, build a new kernel
with that option, and boot with this new kernel
- 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)
- 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.
- #!/bin/bash
- exec > /root//backup.log 2>/root/backup.err
-
- for host in /acme/*/backup
- do
- mount $host
- sleep 2
- done
-
- while true
- do
- find /acme \( -name '*.cpp' -o -name '*.h' \)
-exec /bin/cp -pPRubVt \{\} /backup \;
- done
-
- exit 0
Terminal
Backspace
FTP sessions while in text-mode:
Xterm while under X:
stty erase ^H
When connecting through telnet/SSH:
Arrows
Up = ^[[A
Change how man pages are displayed
In case man pages hide some words because of the colors used,
Miscellaneous
To activate NUM LOCK (with no error when connecting remotely)
Resources