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
- 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
- gtklp - print files via CUPS
- 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
- shred - overwrite a file to hide its contents, and optionally delete it
- showfsck - show the number of reboots until a forced fsck
- tac - concatenate and print files in reverse
- 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
- 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 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 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 sheet feeder and run (do not rotate):
psselect -o book.ps | lpr
Have a postscript or PDF file to print? No problem, use GtkLP.
sudo apt-get install gtklp
Encrypting a file-system with cryptsetup-luks
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 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 loopback file, which can't be resized.
lvresize -L+10M /dev/vg00/test
cryptsetup resize testfs
e2fsck -f /dev/mapper/testfs
resize2fs /dev/mapper/testfs
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
Spamassasin
Checking the configuration
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
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:
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 MTU to a host:
ip route get <ip.address>
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
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):
Restore the state from the backup file /etc/lvm/archive/<somefile> for the volume group <volumegroup>:
vgcfgrestore -f /etc/lvm/archive/<somefile> <volumegroup>
Misc.
UUIDs
Find the UUIDs for all block devices:
Create a random UUID:
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).
Partition Tables
Find all the partitions, this also shows the block device UUIDs:
Reinstalling grub
Reinstalling grub with a /boot on /dev/sdb1 and root on /dev/sdb6:
sudo mount /dev/sdb6 /mnt/hd
sudo mount /dev/sdb1 /mnt/hd/boot
sudo mount -o bind /dev /mnt/hd/dev
sudo mount -o bind /proc /mnt/hd/proc
sudo mount -o bind /sys /mnt/hd/sys
sudo chroot /mnt/hd /bin/bash
grub
grub> root (hd0,0)
grub> setup (hd0)
grub> quit
Disk Info
hdparm and smartctl can provide detailed info on a hard drive:
hdparm -iI /dev/sda
smartctl -a /dev/sda