Misc console stuff, settings and scripts

Some Unix / FreeBSD specific stuff I need once in a while but which I’m unable to learn by heart ;)

German keyboard layout in X11

Insert the following line into the keyboard (InputDevice) section of xorg.conf:

Option "XkbLayout" "de"

IceWM Desktop with rox & GDeskCal

Insert the following line into your .xsession (and/or your .xinitrc):

icewm-session & rox -p=Desktop & gdeskcal

German language in X11

Insert the following line into your .profile:

export LANG=de_DE.ISO8859-1

Recursively copy the content of directory1 to directory2

Preserving sparse files, permissions, timestamps, symlinks, owner, group, executability:

# rsync -lrSvpogEt directory1/ directory2/

or (long form):

# rsync --links --recursive --sparse --verbose --perms --owner --group --executabilty --times directory1/ directory2/

alternatively (very short form which also copies devices and special files):

# rsync -aSv directory1/ directory2/

Loop stuff

Mass rename all files with the ending .avi to .mp4

for i in *.avi; do mv $i `basename $i avi`mp4 ; done

Resize all .jpg images in the current folder to 800x600 (with ImageMagick)

for i in `ls *.jpg`; do convert $i -resize 800x600 $i ; done

The usual Unix find madness

Delete all files named "fw*" in the current directory that are older than 30 days

# find . -name "fw*" -mtime +30 -exec rm {} \;

Replace "expression1" by "expression2" in all files in the current directory

# find . -exec perl -pi -e s/expression1/expression2/g {} \;

Hard disk imaging

Create a gzipped image of an NTFS partition with Linux

# ntfsclone --save-image -o - /dev/hda1 | gzip -c > /mnt/nas02/backup_name.img.gz

Restore a gzipped image of an NTFS partition with Linux

# gunzip -c /mnt/usb/backup_name.img.gz | ntfsclone --restore-image --overwrite /dev/hda1

Create an image of the the MBR and boot sector of a hard drive

# dd if=/dev/hda of=mbrboot.img bs=512 count=2

Create an image of the the MBR only

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

Rescan of all SCSI HBAs (useful for hotplug)

for i in /sys/class/scsi_host/*; do echo "- - -" > $i/scan; done

Linux LVM:

Allocate 20 GB of unused space at the end of a disk to an existing logical volume. Assuming you already have 2 partitions /dev/sda1 and /dev/sda2 on that disk, add a third partition (/dev/sda3) with fdisk, afterwards perform the following steps:

# pvcreate /dev/sda3
# vgextend /dev/VolGroup00 /dev/sda3
# lvextend -L 20G /dev/VolGroup00/LogVol00
# resize2fs /dev/VolGroup00/LogVol00

2 pass video conversion:

Convert a video to 720p webm with 600k video bitrate, using 2 threads (dual core CPU).

# avconv -i source.mp4 -vf scale=-1:720 -acodec libvorbis -ac 2 -ab 128k -ar 44100 -pass 1 -qmin 0 -qmax 50 -b:v 800k -threads 2 -f webm /dev/null
# avconv -i source.mp4 -vf scale=-1:720 -acodec libvorbis -ac 2 -ab 128k -ar 44100 -pass 2 -qmin 0 -qmax 50 -b:v 800k -threads 2 -f webm destination.webm

Frequently used openssl operations:

Generating a new private key for your host:

# openssl genrsa -out host.key -des3 2048
If you want to launch your httpd automatically on system boot (you probably do), don’t enter any passphrase, otherwise your system will hang in a password prompt. To remove a passphrase from your key, use the following command:
# openssl rsa -in server.key -out server.key_nopass

Reading an SSL cert:

# openssl x509 -in host.crt -noout -text

Generating a new certificate request:

# openssl req -new -key host.key -out certrequest.pem

Alternately, generate a new self signed certificate:

openssl req -new -x509 -nodes -sha1 -days 365 -key host.key > host.crt

Create a certificate for an imported request (as a CA):

echo -ne '01' > ca.serial
openssl x509 -days 7300 -CA ca.cer -CAkey ca.key -CAserial ca.serial -in imported.csr -req -out imported.cer

Export a certificate as PKCS12

openssl pkcs12 -export -in cert.pem -inkey cert.key -out cert.p12