Linux Hints and Tips

See also The Linux Documentation Project


Pronunciation

Linux (pronounced with a short i, as in LIH-nucks)

The following page has a sound file which plays a recording of how Linus Torvalds pronounces it himself.

or even a YouTube clip:

Different formats at

and The Linux Documentation Project at


Clock setting

See LinuxClockGuide

Executing commands recursively

Simple Example

  • find -type f | xargs grep 'findme'

More Complex

This command will recursively work its way down through the current sub-directories executing the command 'md5sum -c md5.sum'. The '-x' option to 'sh' shows the commands being executed.

  • export WRKDIR=pwd
  • find -type d | awk "{print \"cd \"\$0 \" ; md5sum -c md5.sum; cd $WRKDIR\"}" | sh -x

Boot Disk

Tools for creating boot disks varies with distributions. The following relates to Mandrake.

First obtain the Linux version you're running with:

  • uname -a

This reports something like:

  • Linux www.mydomain.com 2.4.20-18.7 #1 Thu May 29 08:57:32 EDT 2003 i486 unknown

In this case, the version number is '2.4.20-18.7'

  • mkbootdisk [--compact] <version>

E.g.

  • mkbootdisk 2.4.20-18.7

Note: These days, most kernels won't fit on a floppy without a lot of work pairing it down. A better options is to use one of the Linux distros that boots from a CD.

Also bear in mind that your distro's installation boot CD may have a repair option. The most likely reason for needing a boot disk is some other program overwriting the boot sector. E.g. Mandrake 9.2 install boot disk will repair the boot sector.

Lilo can be persuaded to make a backup copy of the boot sector. Normally this will be an old version, but by renaming the normal backed up version (/boot/boot.0300 on my system), you could rerun lilo to create a copy of the current one. Lilo can also be used to restore such a copy ('lilo -u' or 'lilo -U' Read the man pages!). You might be able to use this in conjunction with a live CD like MandrakeMove. Have a look at the lilo man pages.

Grub is well worth considering. See GrubTips. It's not easy to create a boot floppy for lilo anymore. On many occasions I've had lilo mess up the hard disk's boot sector and left me scrabbling for a live CD or maybe a boot floppy. With grub, you've got a lot more you can do to rescue your system using Grub's fairly simple command line. The command line is pretty straight-forward and includes help.


Killing multiple tasks

Test with the grep statement first!

Just the grep:

  • ps auxw | grep runawaytask | awk '{print $2}'

With the kill

  • kill -9 ps auxw | grep runawaytask | awk '{print $2}'

See also: man killall


SSH Host Keys

To get a host's based64 encoded key:

$ ssh-keyscan -t rsa,dsa hostname

To find a base64 encoded host key in your known_hosts file:

$ ssh-keygen -H -F hostname

See http://gablog.eu/online/node/35 for more information.

Copying files using SSH

$ scp ~foo/.ssh/id_rsa.pub foo@bar.cs.umd.edu:~foo/.ssh/authorized_keys2

Trouble-shooting


CDROM not ready.

If you get the following message turning up in your syslog, you may have a rogue X process still running in the background.

   kernel: sr0: CDROM not ready. Make sure there is a disc in the drive.

Check if kscd is running with

   ps aux | grep kscd

Check whether KsCD is sitting in your systray. That's no doubt the cause. Failing that you could kill the process with (getting [pid] from the previous command.

   kill -s SIGHUP [pid]


Remount Read-Only drive for Read-Write

  • mount -o remount,rw /mnt/

Disable Terminal Bell/Beep

X

  • xset -b.

You can also change the bell behaviour in other ways. Have a look at the man pages for xset.

Shell

Update /etc/inputrc to include:

  • set bell-style none

Add the following to /etc/profile, if necessary:

<code>
if [ -z "$INPUTRC" -a ! -f "$HOME/.inputrc"]; then
INPUTRC=/etc/inputrc
fi
</code>

or maybe...

/etc/inputrc: * system-bell none


Switching between ext2 and ext3 file systems

The ext3 file system is the same format as ext2, but with a journal for recovery in the event of an un-clean shutdown.

Changes must be performed on an unmounted filesystem. After a change, run 'e2fsck -f my_device' to check the file system.

Check the man pages for tune2fs(8) before starting. This is just a hint and doesn't contain the warnings that the documentation does.

Note: The -O option is a letter, not a number.

Converting ext2 to ext3

  • tune2fs -O has_journal /dev/hda1

Converting ext3 to ext2

  • tune2fs -O ^has_journal /dev/hda1

Disk I/O Performance Test

  • hdparm -t -T /dev/hda1

or, from ftp://samba.org/pub/tridge/dbench/

  • dbench 10

to simulate 10 clients


Disk Monitoring

SmartSuite

"SMART suite controls and monitors storage devices running the Self-Monitoring, Analysis and Reporting Technology System (S.M.A.R.T.) build into ATA and SCSI Hard Drives. This is used to check the reliability of the hard drive and predict drive failures. The suite contains two utilities: smartctl is a command line utility designed to perform simple smart tasks; smartd is a daemon that periodically monitors smart status and reports errors to syslog."


Passwords

This section moved to PasswordGeneration.

Quick append to file

$ cat >> myfile <<EOF

first line second line EOF

with root rights:

$ cat <<EOF | tee myfile

first line second line EOF




Shell Scripting

See UnixShell and the Advanced Bash-Scripting Guide

Use Bash Strict Mode (Unless You Love Debugging) describes a technique for mitigating programming errors in Bash scripts by including the following switches in the script:

    set -e
    set -u
    set -o pipefail

set -e causes scripts to abort if an unhandled non-zero exit status is returned by a command.

set -u flags an error if a variable is referenced before it has been defined.

set -o pipefail normally a series of commands separated by pipes only returns the exit code of the last command. This option returns the error code of the last command to exit with a non-zero status in the entire pipeline, or zero if there were no errors.

Enter help set in a bash shell for more information on shell options.

Here Documents

See the Here Documents section in the Bash manual. Quote:

If any characters in WORD are quoted, the DELIMITER is the result of quote removal on WORD, and the lines in the here-document are not expanded.

Redirect all output

  • ls -yz > /dev/null 2>&1

Netcat

See Netcat – a couple of useful examples | G-Loaded Journal

Saving and Restoring the Master Boot Record

Save the MBR and the primary partition table

# dd if=/dev/hda of=backup-of-hda-mbr count=1 bs=512

If you only want to restore the actual MBR code and not the primary partition table entries, just restore the first 446 bytes: (The first 446 bytes are MBR, then 64 bytes of primary partition table).

#dd of=/dev/hda if=backup-of-hda-mbr bs=446 count=1.

UDev

See WritingUdevRules

Compressed Filesystem

See Fuse

FAT32 File Systems

Create the partition as type 0x0c using cfdisk.

With the dosfstools package installed:

Format it with:

# mkdosfs -F 32 <device>

Optionally, label it with:

# dosfslabel <device> <label>

References


-- Frank Dean - 5 Jan 2003

Related Topics: AwkUtils, CdBurning, CygwinHintsAndTips, DebianPcmcia, DebianTips, GrubTips, HalSleepQuirks, HowToRebuildInitrdImage, KdeTips, LinuxClockGuide, LinuxGroups, LinuxHardware, LinuxLilo, LinuxPrinting, LinuxSecurity, LinuxSoftwareRaid, LogicalVolumeManagement, LxdeTips, PasswordGeneration, RootKitDetection, SambaConfiguration, SystemVStartupScripts, UbuntuTips, UnixShell, XenTips, XfreeTips, XimianEvolution, XorgTips