home index sites engineering software hardware email
 

How-To: Linux



Sites




Documentation & References




Useful Commands


  • apropos - search the manual page names and descriptions, supports wildcards (same as 'man -k')
  • arp-scan - uses the ARP protocol for scanning an entire LAN for hosts that are up
  • ascii - displays an ASCII table in decimal or hex
  • bvi - visual editor for binary files
  • dc - an arbitrary precision RPN calculator
  • dlint - DNS error checking utility
  • dstat - versatile tool for generating system resource statistics
  • fdupes - finds duplicate files in a given set of directories
  • fromdos - converts text files between from DOS to Unix format
  • gtklp - print files via CUPS
  • identify - describes the format and characteristics of one or more image files
  • iotop - display IO usage in top-like format
  • lanmap - network discovery tool that produces nice 2d images
  • multitail - browse through several files at once
  • netrik - fast text mode browser with vi like keybindings
  • ntop - display network usage in top-like format
  • 7zip/p7zip - 7-zip file archiver with high compression ratio
  • pgrep & pkill - look up or signal processes based on name and other attributes (alt to ps auxw |grep)
  • pv - monitor the progress of data through a pipe
  • pwgen - generate pronounceable & secure passwords
  • rename - renames multiple files using perl regular expressions
  • rsync - the ultimate command line file/directory transfer command
  • saidar - a curses-based tool for viewing system statistics
  • screen - screen manager with VT100/ANSI terminal emulation
  • seq - print a sequence of numbers
  • shellcheck is a lint style tool that catches common shell scripting mistakes.
  • showfsck - show the number of reboots until a forced fsck
  • shred - overwrite a file to hide its contents, and optionally delete it
  • sipcalc - IP subnet calculator
  • sloccount - counts source lines of code
  • streamripper - rip shoutcast radio streams to mp3 files
  • tac - concatenate and print files in reverse
  • todos - converts text files between from Unix to DOS format
  • type - reveal whether a command is an alias, executable, shell built in or script
  • wget - the non-interactive network downloader
  • whowatch - console, interactive, process and users monitoring tool
  • xxd - convert hex to/from ASCII
  • zcat - expand and concatenate compressed data


Installing dictionaries and setting the default language for OpenOffice.org


Install a new dictionary


This step depends on your OS and distro. Under Gentoo Linux you can install one of the myspell dictionary packages. The following shows how to install the English package while logged in as root in Gentoo:

emerge app-dicts/myspell-en
eselect oodict set myspell-en

Set the default language


Then restart OpenOffice.org as a regular user, select the menu item 'tools/options' and then under the 'language settings/languages' dialog set the 'default languages for documents' field to your language of choice (in my case it is 'English (Canda)', in other words 'Canadian English').


Printing


Print using GtkLP


Have a postscript or PDF file to print? No problem, use GtkLP. To install:

sudo apt-get install gtklp

To print a postscript or PDF run the following command (change file.ps to the name of your postscript or PDF file).

gtklp file.ps


psutils


To use any of the printing programs in following sections you will need to first install the psutils package:

sudo apt-get install psutils


Print 2 pages per sheet on a single sided printer


First print to a postscript file (say file.ps), then run:

psnup -2 -q -d1 -pletter file.ps > file2.ps
psselect -e file2.ps | lpr

Then rotate the printed pages around and insert printed face up into the sheet feeder and run:

psselect -o file2.ps | lpr


Print an A4 sized document on a US-letter printer


First print to a postscript file (say file.ps), then run:

psresize -PA4 -pletter file.ps | lpr


Print a 2 page per sheet booklet on a single sided printer


First print to a postscript file (say file.ps), then run:

psbook -q file.ps test.ps
psnup -2 -q -pletter test.ps > book.ps
psselect -e book.ps | lpr

Then insert printed face up into the sheet feeder and run (do not rotate):

psselect -o book.ps | lpr


Print a 2 page per sheet booklet on a double sided (duplex) printer


When printing on a duplex printer the steps are similar to the single sided case above. However, instead of using psselect to selectively print even or odd pages, you just print the file book.ps. Note that in order to use 'lpr' to print you need to have short edge duplex printing on by default. If you don't, you can use gtklp to print the file, selecting 'short edge binding' under 'duplex settings' on the 'general' tab.

psbook -q file.ps test.ps
psnup -2 -q -pletter test.ps > book.ps
lpr book.ps


Print a 2 page per sheet booklet using page-crunch


The page-crunch application can alter a postscript or PDF file in a number of ways, including making a booklet. To install page-crunch:

sudo apt-get install page-crunch

To create a 2 pages per sheet booklet simply run the following command (change file.ps to the name of your postscript or PDF file):

page-crunch file.ps

Then:

  1. select '2' under 'Pages per sheet'
  2. select 'in' and 'out' page 'Format' as appropriate ('letter' for those in North America)
  3. click on 'Produce a book'
  4. click on 'See'

A new file will be generated (file_reduit.ps if your input was file.ps) containing the original file formatted as a booklet, ready for printing. You can use gtklp to print the file, when doing so make sure to select 'short edge binding' if you are using a duplex printer.


Shrink/compress/optimize a PDF file


You can use ghostscript/gs to reduce the size of a PDF file. The quality of the output is dependent on the -dPDFSETTINGS parameter (see this page for more details):

-dPDFSETTINGS=/screen   (screen-view-only quality, 72 dpi images)
-dPDFSETTINGS=/ebook    (low quality, 150 dpi images)
-dPDFSETTINGS=/printer  (high quality, 300 dpi images)
-dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)
-dPDFSETTINGS=/default  (almost identical to /screen)

For example, to obtain the best compression, which results in the lowest quality, use -dPDFSETTINGS=/screen:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf


Encrypting a file-system with cryptsetup-luks


cryptsetup-luks uses the device-mapper to seamlessly encrypt a block device (either on the disk or with a file using a loop-back device). You will need a kernel with device mapper and crypto api support to use this. For more information on the encryption settings see the Linux hard disk encryption settings page @ clemens.endorphin.org.

Create and encrypt the file-system


Select a device and encrypt it. In this example I create a 1GB file-system that is backed by the file /crypt/test using the loop-back device /dev/loop0. You will be asked to 'Enter LUKS passphrase', this is your master passphrase that will be needed whenever you want to mount the file-system, so don't forget it!

dd if=/dev/zero of=/crypt/test bs=1M count=1 seek=1023
chmod 600 /crypt/test
losetup /dev/loop0 /crypt/test
cryptsetup -y -c aes-cbc-essiv:sha256 -s 256 luksFormat /dev/loop0
cryptsetup luksOpen /dev/loop0 testfs
mkfs.ext3 -m 0 /dev/mapper/testfs

Mount the file-system


Now you can mount the encrypted file-system. The following commands are all that is necessary to mount the file-system on reset (you can skip the first two lines if you just created it using the previous step).

losetup /dev/loop0 /crypt/test
cryptsetup luksOpen /dev/loop0 testfs
mount /dev/mapper/testfs /mnt/crypt

Unmount the file-system


Now you can unmount the encrypted file-system. These are all the commands that are needed to do a clean shutdown of the file-system.

umount /dev/mapper/testfs
cryptsetup luksClose testfs
losetup -d /dev/loop0

Check the device mapper table


Run the following command to ensure that the device mapper is correctly configured (after you have run cryptsetup luksOpen). You should see something starting with 'testfs: 0 2095096 crypt aes-cbc-plain'.

dmsetup table testfs

Resizing


The following will increase the size of an encrypted EXT3 partition by 10MB. Note that the following is using an LVM partition (/dev/mapper/vg00/test which is the test partition in the vg00 volume group) instead of a loop-back file, which can't be re-sized.

lvresize -L+10M /dev/vg00/test
cryptsetup resize testfs
e2fsck -f /dev/mapper/testfs
resize2fs /dev/mapper/testfs

The following will increase the size of an encrypted XFS partition by 10MB (the partition is mounted under /extra). Note that XFS file-systems can be resized while mounted.

lvresize -L+10M /dev/vg00/test
cryptsetup resize testfs
xfs_growfs /extra

The following will set the size of an encrypted XFS loop-back file to 2GB (the first three steps are only required if the file-system isn't already in use).

umount /dev/mapper/testfs
cryptsetup luksClose testfs
losetup -d /dev/loop0
truncate -s 2G /crypt/test
losetup /dev/loop0 /crypt/test
cryptsetup luksOpen /dev/loop0 testfs
cryptsetup resize testfs
mount /dev/mapper/testfs /mnt/crypt
xfs_growfs /mnt/crypt

Generating good passwords


pwgen is great tool that can create random passwords that are easy for humans to remember, or super strong ones for encryption keys, etc..

Installation


To install in a Debian/Ubuntu system:

sudo apt-get install pwgen

Generating passwords


To generate a 12 character password that is easy for a human to remember (the combination of vowels makes it easier):

pwgen 12 1

To generate a 16 character strong password that is NOT easy to remember (generated from raw random data for use as an encryption key):

pwgen -s 16 1

User management


Adding a user to a group


gpasswd can be used to add a user to a group. In the following example the user user1 is added to the group group1.

gpasswd -a user1 group1


Editing text


Using Vim


See my Vim How-To page for some useful information.


Spamassasin


Checking the configuration


spamassassin -D --lint


Mplayer


Using mplayer to extract a specific range of frames from a movie


mencoder dvd://1 -oac copy -ovc copy -ss START:TIME -endpos SCENESECONDS -o scene.avi


rkhunter


To manually update the rkhunter database


rkhunter --checkall --update --report-warnings-only --propupd


ack


ack -- better than grep, a power search tool for programmers


Honestly, this is *the* ultimate search tool. Faster than grep with intelligent behavior (such as automatically ignoring .svn, CVS and other VCS directories and binary files). Try it and you won't be disappointed!

Get it at the ack website. There is also a Gentoo ebuild. Read these nice blog entries here and here discussing some of the features.


BitTorrent


rTorrent


rTorrent is by far the best command line torrent client for Linux. Read the on-line manual here.


GNU Screen


Intro


GNU Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells. It's the command line equivalent to a window manager, and is the best tool for running persistent interactive programs via the command line.


Command usage


  • screen -ls lists your screen sessions
  • screen -S somename creates a new screen session named 'somename'
  • screen -r re-attaches to the first detached session found
  • screen -dr forces the first session found to detach then attaches to it
  • screen -dr somename forces the session named 'somename' to detach then re-attaches to it

  • Screen tips


    Note: C-a means CTRL-A (the 'control' and 'a' keys pressed simultaneously).

  • C-a c create a new terminal
  • C-a A allows you to name the terminal
  • C-a d detaches from the screen session
  • C-a " lists the terminals
  • C-a p moves to the previous terminal
  • C-a n moves to the next terminal
  • C-A ESC enters scroll back mode, uses vim style movement/copy/paste keys
  • C-A q leaves scroll back mode
  • C-A :quit quits/kills the screen session


  • Gnome


    Themes & Graphics


    Check out GNOME-Look.org for themes and graphics (wallpapers, etc.).

    Tweaks


    Setting the wallpaper via the command line:

    gconftool-2 -t str --set /desktop/gnome/background/picture_filename /path/to/some/file.png

    Enabling wire-frame move and resize:

    gconftool-2 -t bool --set /apps/metacity/general/reduced_resources true

    Editing the menus:

    alacarte

    Empty the trash:

    rm -rf ~/.local/share/Trash/files/*

    Useful commands


    • ALT-F1 launch the applications menu
    • ALT-F2 launch "run application" dialog prompt
    • CTRL-ALT left-arrow move to the previous virtual desktop
    • CTRL-ALT right-arrow move to the next virtual desktop
    • CTRL-ALT-SHIFT left-arrow move focused window to the previous virtual desktop
    • CTRL-ALT-SHIFT right-arrow move focused window to the next virtual desktop


    Networking


    Determine the route to a host:


    ip route get <host_ipaddress>

    Force a NIC into full duplex 1000 Mbps:


    sudo ethtool -s eth0 speed 1000 duplex full autoneg on

    Check on the status/settings for a NIC:


    sudo ethtool eth0


    iptables


    Basic iptables commands to block incoming connections yet allow out everything


    iptables -F INPUT
    iptables -A INPUT -i lo -j ACCEPT
    iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    iptables -A INPUT -p tcp --syn -j LOG --log-prefix "rejected tcp connection: "
    iptables -A INPUT -p tcp --syn -j REJECT
    iptables -A INPUT -j DROP

    The above blocks all incoming connections, including ping (ICMP echo requests). If you want to allow in ping requests add the following:

    iptables -I INPUT -p icmp --icmp-type echo-request -j ACCEPT


    Creating/converting eBooks using calibre


    Calibre is an amazing program for converting e-books in Linux. It can read MOBI, LIT, PRC, EPUB, ODT, HTML, CBR, CBZ, RTF, TXT, PDF and LRS formats, producing either LRF or EPUB (my preferred format) files (Ubuntu package).

    Converting a PRC (or any non HTML) file to an EPUB


    You can use the GUI tool (simply run calibre) or you can perform the same action from the command line with:

    any2epub --margin-top=0 --margin-bottom=0 --margin-right=0 --margin-left=0 book.prc

    Converting an HTML file to an EPUB


    Because an HTML file doesn't contain a cover page graphic you can specify the cover page with the following syntax:

    any2epub --margin-top=0 --margin-bottom=0 --margin-right=0 --margin-left=0 --cover cover.jpg book.html

    Calibre may not be able to determine the author name from an HTML file, here is the syntax to specify that:

    any2epub --margin-top=0 --margin-bottom=0 --margin-right=0 --margin-left=0 --cover cover.jpg -a "First Last" book.html


    Logical Volume Manager


    Restore a removed logical volume


    Find the backup file under /etc/lvm/archive that represents the last state where the removed logical volume existed (the files in /etc/lvm/archive are automatically created by vgcfgbackup when changes are made to logical volumes, they are plain text files that have names ending with .vg):

    find /etc/lvm/archive

    Restore the state from the backup file /etc/lvm/archive/<somefile> for the volume group <volumegroup>:

    vgcfgrestore -f /etc/lvm/archive/<somefile> <volumegroup>


    TightVNC


    Lost keyboard focus when using vncviewer in fullscreen mode


    If you lose the keyboard focus when running the TightVNC vncviewer in fullscreen mode simply run the following two commands (the second one only needs to be run once):

    echo "Vncviewer*grabKeyboard: true" >> ~/.Xresources
    xrdb -merge ~/.Xresources


    Misc.


    UUIDs


    You can find good info in How To Manage Your Disk By UUID On Linux.

    Find the UUIDs for all block devices:

    blkid

    Create a random UUID:

    uuidgen       


    Hardware probing


    You can determine a variety of things about your system with lshw, lsusb and lspci (you will be limited to the amount of information if you don't run these as root, or don't use sudo).

    lshw
    lsusb
    lspci


    Partition tables


    Find all the partitions, this also shows the block device UUIDs:

    sudo fdisk -l


    Reinstalling grub


    Reinstalling grub with /boot on /dev/sda1 and root on /dev/sda3 using 'grub-install' from a rescue CD (such as SystemRescueCd):

    mkdir -p /mnt/hd
    mount /dev/sda3 /mnt/hd
    mount /dev/sda1 /mnt/hd/boot
    mount -o bind /dev /mnt/hd/dev
    mount -o bind /proc /mnt/hd/proc
    mount -o bind /sys /mnt/hd/sys
    chroot /mnt/hd /bin/bash
    chroot> grub-mkconfig -o /boot/grub/grub.cfg
    chroot> grub-install /dev/sda
    chroot> exit
    umount /mnt/hd/sys
    umount /mnt/hd/proc
    umount /mnt/hd/dev
    umount /mnt/hd/boot
    umount /mnt/hd
    sync
    reboot

    After rebooting you should update the grub menu with 'update-grub':

    sudo update-grub


    Disk info


    hdparm and smartctl can provide detailed info on a hard drive:

    hdparm -iI /dev/sda
    smartctl -a /dev/sda


    Magic SysRq


    The magic SysRq key is a key combination that can help properly shutdown a system that has become unresponsive. The CONFIG_MAGIC_SYSRQ kernel config option has to be enabled at kernel compile time to enable this feature (all recent Ubuntu releases have this enabled by default).

    There are a number of magic SysRq keys and each can trigger the kernel to do various actions ranging from killing processes to unmounting file-systems. To invoke an action simply press the associated key while also pressing the CTRL + ALT + PrintScrn/SysRq keys simultaneously (although CTRL isn't normally needed, it will avoid causing a screenshot from being made on Gnome and KDE based systems). If your system is hung the following magic SysRq keys are a recommended way to help properly shutdown your system:

    R E I S U B

    unRaw (take control of keyboard back from X)
    tErminate (send SIGTERM to all processes, allowing them to terminate gracefully)
    kIll (send SIGKILL to all processes, forcing them to terminate immediately)
    Sync (flush data to disk)
    Unmount (remount all filesystems read-only)
    reBoot


    Verifying the kernel source signature


    When downloading kernel source tarballs from kernel.org you can verify the GPG signature with the following command line (source):

    gpg --verify linux-X.Y.X.tar.bz2.sign linux-X.Y.X.tar.bz2

    If you get the error "gpg: Can't check signature: public key not found" you first need to download the kernel GPG public key with the following command line:

    gpg --keyserver wwwkeys.pgp.net --recv-keys 0x517D0F0E


    Numeric keys not working?


    If you find that your numeric keys are no longer working (few or none of the keys work) then try the following key press: CTRL + SHIFT + NUMLOCK


    Scripting


    Displaying GTK+ dialog boxes from the command line


    Zenity allows you to display GTK+ dialogs from shell scripts. For example the following will display an error window:

    zenity --error --text="Could not find some file."