Homepage of Lars E. Pettersson

Linux, tips and tricks

Why Linux

I am one of those that skipped the Microsoft Windows train. I started out using MS-DOS in 1984, UNIX in 1987, and Linux around 1991. I have never liked Windows, everything seem to be hidden away in odd places, not to mention the problems with viruses etc. that have created huge problems during the years. For me the ultimate operating system is Linux. It is fast, you do not need the fastest and latest computer around, safe, it is based on the UNIX model with different users and security between these users, free, you can just download it without being afraid that the police will show up at your doorstep for having illegal copies of software, and open source, you may freely change, or edit the code, this also makes it easy to create safe and sound code as the code will be audited by a huge number of people.

Why use Microsoft Windows when Linux is around?

Mini-howtos, notes, etc.

This is a small collection of different kinds of notes etc. I have written while setting up my computers. It is put here mainly for my own use, to have all information in one place, but may be of some use for others. Please feel free to comment my writings.

By the way, I take no responsibility whatsoever for you hosing your system using the information on this web page. What I have written worked for me, but may not do so for you. If you encounter problems though, and find a solution, please let me know, and I will update this page.

A4 paper size
A4 paper size
awk

Print a certain field (in this case number 11) delimited by white space

awk '{ print $11 }'
cacti (http://cacti.net/)

If you only want to ping a certain host, i.e. not use SNMP at all, set "Host Template" to "None" and make sure that "SNMP Community" is blank. Set up the "Unix - Ping Latency" graph template as usual.

CD/DVD

To copy a CD/DVD (this works for both data and unencrypted movies)

dd if=/dev/dvd of=dvdcopy.iso cdrecord dev=/dev/dvd dvdcopy.iso

To burn backups on DVD do, for a folder do

growisofs -Z /dev/dvd -R -J /path/to/folder

and for an iso file do

growisofs -dvd-compat -Z /dev/dvd=image.iso

Create ISO files with

mkisofs -v -l -r -J -R -max-iso9660-filenames -no-bak -o updates.iso /path/to/files

Some more information is available in the evolution tip.

convmv

Converts file names from one encoding to another. Quite handy converting file names to/from UTF8 etc.

CVS
CVS server
diction

As mentioned in the man-page "diction - print wordy and commonly misused phrases in sentences".

To check the text in the file textfile.txt, do

diction -bs textfile.txt

This will make diction complain about mistakes typically made by beginners, and suggest better wording, if any. Quite handy for someone who do not have English as first language.

dovecot

To create a dovecot cert, first configure

/etc/pki/dovecot/dovecot-openssl.cnf
and then remove the old certificates
rm /etc/pki/dovecot/*/dovecot.pem
then run
/usr/share/doc/dovecot-0.99.14/examples/mkcert.sh
or
/usr/libexec/dovecot/mkcert.sh
depending on version. The certificates end up in the directories
/etc/pki/dovecot/certs/ /etc/pki/dovecot/private/
emacs
emacs
evolution
evolution
find

Find all files in or below current directory, ., modified in the last day, -mtime -1, that are not directories, \! -type d, and execute the list function on them, ls -l

find . -mtime -1 \! -type d -exec ls -l {} \;

Find all pdf's, no matter what case, in or below current directory and feed them to acroread. -print 0 and xargs -0 takes care of directories and/or files containing space etc.

find . -iname '*.pdf' -type f -print0 | xargs -0 acroread
firefox

To be able to handle mailto: URL:s go to about:config and change network.protocol-handler.external.mailto to true, then add the network.protocol-handler.app.mailto preference name (right click in the about:config window, chose new->string and follow the instructions, for value write the path and name of your e-mail application, /usr/bin/thunderbird in my case.)

formail

To split a mailbox into separate text files, do

formail -ds sh -c 'cat > msg.$FILENO' < $MAIL
Gnome
gnome
Gnome terminal

When using mc (Midnight Commander) in the gnome terminal you will notice that F10 brings up the gnome terminal menu, instead of being sent to mc. To fix this, in the gnome terminal chose the "edit" menu, then "keyboard shortcuts", and in the new window check "Disable menu shortcut key (F10 by default)".

GPIB
GPIB
initrd

To see the contents of an initrd-file

gunzip --to-stdout /boot/initrd-2.6.17-1.2145_FC5.img | cpio -t

To extract

mkdir /tmp/initrd cd /tmp/initrd gunzip --to-stdout
/boot/initrd-2.6.17-1.2145_FC5.img | cpio -i
Upgrading Fedora from harddisc

From the boot.iso file copy the install kernel and initrd to the boot partition and update grub to start that kernel.

# wget
http://download.fedora.redhat.com/pub/fedora/linux/core/5/i386/os/images/boot.iso
# mkdir -p /mnt/temp # mount -r -o loop boot.iso /mnt/temp # cp
/mnt/temp/isolinux/vmlinuz /boot/vmlinuz-fc5 # cp
/mnt/temp/isolinux/initrd.img /boot/initrd-fc5.img # umount /mnt/temp
# rmdir /mnt/temp

Added lines to /etc/grub.conf (copy one of the existing grub entries but change the kernel and initrd lines.)

title Fedora Core 5 install root (hd0,0) kernel /vmlinuz-fc5 initrd
      /initrd-fc5.img

Please note that if do not have your /boot directory as its own partition, you have to append /boot to the kernel and initrd images above.

Upgrading Fedora using USB stick

NOTE: Any data on the USB stick will be destroyed. Also make sure that you use the correct device.

Your flash drive must have a single partition with a vfat file system. To determine how it is formatted, find the name of this partition and the device itself by running dmesg shortly after connecting the drive. The device name (similar to /dev/sdc) and the partition name (similar to /dev/sdc1) both appear in several lines towards the end of the output.

Use the partition name to ensure that the file system type of the USB flash drive is vfat.

# blkid partition
You should now see a message similar to:
LABEL="LIVE" UUID="6676-27D3" TYPE="vfat"
If TYPE is anything other than vfat (for example, TYPE="iso9660"), clear the first blocks of the USB flash drive:
# dd if=/dev/zero of=partition bs=1M count=100
Use the dd command to transfer the boot ISO image to the USB device:
# dd if=path/image_name.iso of=device
where path/image_name.iso is the boot ISO image file that you downloaded and device is the device name for the USB flash drive. Ensure you specify the device name, not the partition name. For example:
# dd if=~/Download/Fedora-17-x86_64-DVD.iso of=/dev/sdc
New hard-drive

First copy the contents of the old hard-drive to the new one (mounted as /mnt/disk) (note; zeros i.e. 0, not big o's, i.e. O):

find / -xdev -print0 | cpio -pa0V /mnt/disk

(make sure that all partitions are copied, if you have more than one) then halt the computer. Remove the old hard-drive, and install the new one in its place. Start with a live system on a USB-stick, or a DVD. When the live system has started, mount the new hard-drive as /mnt/disk, then mount the following:

mount -t proc none /mnt/disk/proc mount -o bind /dev /mnt/disk/dev

Make sure that the disk UUID:s are correct in /mnt/disk/etc/fstab and /mnt/disk/boot/grub/menu.lst, correct if wrong (you can get the UUID of your new disk by issuing the cammand "blkid").

Now you can install the boot loader (grub) into the MBR by issuing the following command:

chroot /mnt/disk grub-install /dev/sda

Cross your fingers, and restart.

iptables

Stop SSH scans with

iptables -A INPUT -i eth0 -p tcp -m tcp --dport 22 -m conntrack \\
    --ctstate NEW -m recent --set --name sshscans --rsource

iptables -A INPUT -m recent --rcheck --seconds 60 --hitcount 10 \\
    --name sshscans --rsource -j DROP

or

# Then setup the ssh reject trap.  $IPTABLES -A INPUT -p tcp --syn
--dport 22 -m recent --name sshattack --set $IPTABLES -A INPUT -p tcp
--dport 22 --syn -m recent --name sshattack \\ --rcheck --seconds 120
--hitcount 3 -j LOG --log-prefix 'SSH REJECT: ' $IPTABLES -A INPUT -p
tcp --dport 22 --syn -m recent --name sshattack \\ --rcheck --seconds
120 --hitcount 3 -j REJECT --reject-with tcp-reset

With the values present a given site gets three tries within a 120 second interval. Once that is exceeded it is locked out until the retry count drops below 3 in 2 minutes. This means ALL attacks get blocked. It also means that if you screw up your password three times you can still get in if you wait a little bit.

Kde
kde
Kernel

To keep the two latest kernels, including the running one, do

# yum install yum-utils # package-cleanup --oldkernels

A guide describing how to rebuild kernels can be found at http://home.swbell.net/kwgow/kernel/steps.html

LaTeX
LaTeX
lsof

To find out what ports are open

/usr/sbin/lsof -i

or, for a certain port

/usr/sbin/lsof -i :22
mod_ssl

To create a certificate for apache, do

openssl genrsa -out httpd.key 1024 openssl rsa -in httpd.key -pubout
openssl req -new -key httpd.key -out httpd.csr openssl x509 -req -days
365 -in httpd.csr -signkey httpd.key -out httpd.crt openssl x509 -text
-in httpd.crt mv httpd.crt /etc/httpd/conf/ssl.crt/ mv httpd.key
/etc/httpd/conf/ssl.key/
mrtg
mrtg
My mailserver
First mailserver setup
Postfix
NIS
NIS
raid
Raid1 setup
rename

To rename multiple files (*.cpp into *.c)

rename .cpp .c *.cpp

or try

for file in *.cpp do mv ${file} ${file%cpp}c done
RPM
RPM
rrdtool

To convert some rrd databses between different systems (i.e. 32 to 64 bit etc.) do the following two steps. Step one on the 32-bit, and step two on the 64-bit.

for i in `find -name "*.rrd"`; do rrdtool dump $i > $i.xml; done
for i in `find -name "*.xml"`; do rrdtool restore $i `echo $i |sed s/.xml//g`; done
rsync

Copy a local file or directory to a remote machine using rsync and ssh

rsync -avP -e ssh local_source user@new_server:remote_destination/
smartctl and smartd

To make smartd check SATA discs, add the following to /etc/smartd.conf

/dev/sda -d ata -H -m root@localhost.localdomain
/dev/sdb -d ata -H -m root@localhost.localdomain

And to check status, do

smartctl -d ata -a /dev/sda
SNMP
SNMP
SSH

SSH-keys description:

SSH-agent description:

SSH programs for Windows:

Subversion

To get the file modification date (well, not exactly, but the commit time of the file) for checkout, update, switch, and revert, add the following to the ~/.subversion/config file

[miscellany]
### Set use-commit-times to make checkout/update/switch/revert
### put last-committed timestamps on every file touched.
use-commit-times = yes

To create a subversion repository, in this case named myprog, and filling it with data from the directory local_copy, and finally taking a look at the repository, do

svnadmin create /usr/local/svn/myprog
svn import local_copy file:///usr/local/svn/myprog -m "First Import"
svn list --verbose file:///usr/local/svn/myprog