arminstraub.com

Useful Linux tools

These are some random tips, collected for personal usage, on the usage of a few tools useful in a Linux environment.

Passwordless SSH

Whenever you log into your SSH account on some host (e.g. your webhosting provider) you'll be prompted for and have to enter a password. Although this is important for the security of your account, it might be annoying at some point. Fortunately, there is a way to automate the authorization. To do this create locally (i.e. on the client) a pair of keys consisting of a private and a public key. As the name implies the public key doesn't need to be kept secure. It's the one we will transfer to the server. But make sure that the private key is kept secret and is not available for anyone else, as this would have the same effect as telling the password to your account.

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/armin/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/armin/.ssh/id_rsa.
Your public key has been saved in /home/armin/.ssh/id_rsa.pub.
The key fingerprint is:
[Some numbers] armin@armin-laptop

Transfer the public part of this key to the server.

$ ssh-copy-id -i ~/.ssh/id_rsa.pub username@server

You should now be able to log into your server without typing a password. More details and a further discussion on more secure variants can be found e.g. at http://www.debian-administration.org/articles/152.

OpenSSH and SSH

The above implies that both the workstation and the server are using OpenSSH. However, you can still setup passwordless logins when e.g. the server uses SSH. You can find out which versions are in use by issuing

$ ssh -V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu

In this case we get to know that the machine does not use OpenSSH but SSH instead.

First we have to convert our public key id_rsa.pub so that it will be readable for SSH as well. This is done by

$ ssh-keygen -f id_rsa.pub -e >> id_rsa_openssh.pub

Now, we transfer this key to the server and configure the server so it will use it.

$ scp id_rsa_openssh.pub username@server:~/.ssh2
$ ssh username@server 'echo "Key id_rsa_openssh.pub" >> \
> .ssh2/authorization'

Work with PDF & PS

Convert PDF to PS

To convert from pdf to ps you can use pdftops:

$ pdftops file.pdf

There is a similar tool named pdf2ps which internally uses gs but it doesn't produce as good results for me.

Put several pages on a page

The tool mpage allows one to merge ps files.

$ mpage -2 file.ps > new.ps

Alternatively, you can use psnup.

$ psnup -2 file.ps new.ps

Both tools can be used to put more than two pages on a single page (if you got good eyes).

Convert PS to an image

pstoimg can convert ps files to image files like png.

$ pstoimg -type png -crop a -transparent file.eps

This way you get a nicely cropped (-crop a) and transparent png.

Merge several files

To combine several pdf files:

$ pdftk file1.pdf file2.pdf cat output combined.pdf

Note to myself: Did I try psmerge?

Reorder pages

Lately, I wanted to print a 4-page document on a single paper that I would fold like a card. For this I needed to print pages 4 and 1 on the front, and pages 2 and 3 on the back.

$ pdftk doc.pdf cat 4 1 2 3 output sorted.pdf
$ pdftops sorted.pdf
$ psnup -2 sorted.ps print2.ps
$ ps2pdf print2.ps

Scale A4 to A5

First convert to ps, then use psresize:

$ psresize -Pa4 -pa5 doc-a4.ps doc-a5.ps

Unfortunately, for me this results in A4 sized pages that contain the actual content in A5 size. To get A5 sized pages as well, I printed the document to a pdf file again and chose pagesize A5 in the driver settings.

To delete the first page from a pdf file:

$ pdftk input.pdf cat 2-end output output.pdf

Play with djvu

To delete the first page from a djvu file:

$ djvm -d ebook.djvu 1

Use your computer as an alarm

To have some command being executed at a given time you can use the standard tool at while sleep is great for having something done after a certain amount of time. The following commands show for instance how to have amarok start playing at 6:30 in the morning, or how to have it start playing in 45 minutes.

$ echo "dcop --user armin amarok player play" | at 6:30AM
$ sleep 45m && amarok --play

Convert WMA to MP3

$ mplayer file.wma -vo null -vc dummy \
> -ao pcm:waveheader:file=temp.wav

This command will produce a (temporary) wave file that you can encode to mp3 for instance using lame. While converting it might make sense to use the option -af resample=44100. For me, however, this produces an error.

Split MP3 files

Suppose you have a music file that actually contains two songs seperated by a longer silence. You can split it into its two parts using mp3splt (which, of course, is much more powerful than this simple example requires).

$ mp3splt -s -p nt=2,rm long.mp3

Save an internet stream

Suppose you found an internet stream of the form rtsp://*.rm that you want to save on your harddisc. With mplayer this can be done as follows.

$ mplayer -dumpstream rtsp://*.rm
If you are only interested in audio or video you might want to take a look at the options -dumpaudio and -dumpvideo.

By the way, if trying to play a *.ram video stream with mplayer gives you an error like Win32 LoadLibrary failed to load: avisynth.dll, /usr/lib/win32/avisynth.dll this usually is no problem with codecs but is because you're dealing with a playlist instead of a real video file. Try instead:

$ mplayer -playlist file.ram

Remove noise from audio

To remove noise from bad-quality audio/video files you can try to use the concept of filters in mplayer. Ideally such filters should be able to do even greater things like producing karaoke versions of your (music) files

$ mplayer -af karaoke music.mp3
but don't expect that to work. The filters comp, gate or sinesuppress are supposed to remove noise but again didn't really improve my listening experience. Since the most annoying noise was kind of a constant deep crumbling, the most effective way I came up with was to use the equalizer filter to change the volume of certain frequency ranges.
$ mplayer -af equalizer=-12:-12:-12:-6:3:6:3:0:-12:-12 video.mov
This definitely will have to be adapted to the specific movie file but the idea here is that human speech (my video was an online lecture) mainly covers the frequency range from 500Hz to 2kHz (which the 5-7th numbers refer to). Of course, you can use any player like kaffeine that comes with an equalizer and play with these values.

Adjust hard drive options

If I try to have my hard drive "enter the low power consumption standby mode" (see manpage of hdparm) it immediately wakes up again.

$ sudo hdparm -C /dev/hda
/dev/hda:
 drive state is:  active/idle
$ sudo hdparm -y /dev/hda
/dev/hda:
 issuing standby command

My previous configuration and test results.

$ sudo hdparm /dev/hda
/dev/hda:
 multcount    =  0 (off)
 IO_support   =  0 (default 16-bit)
 unmaskirq    =  0 (off)
 using_dma    =  1 (on)
 keepsettings =  0 (off)
 readonly     =  0 (off)
 readahead    = 256 (on)
 geometry     = 19457/255/63, sectors = 312581808, start = 0
$ sudo hdparm -tT /dev/hda
/dev/hda:
 Timing cached reads:   696 MB in  2.00 seconds = 348.15 MB/sec
 Timing buffered disk reads:   58 MB in  3.04 seconds =  19.08 MB/sec

My new hard drive is getting pretty hot while seemingly doing nothing. So I looked for ways to keep it cooler.

$ sudo hdparm -M 128 /dev/hda
/dev/hda:
 setting acoustic management to 128
 acoustic     =  0 (128=quiet ... 254=fast)

Changing my settings. /dev/hda { mult_sect_io = 16 dma = on io32_support = 3 }

(Internet) Connection sharing

Suppose, you have one computer connected to the internet (the gateway) and another one (the client) which you want to have internet connection as well. All you have to do on the client side is to add the gateway option to its network interface. The corresponding entry in /etc/network/interfaces may for instance look like iface eth0 inet static address 192.168.0.1 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.2

On the gateway, something like the following needs to be configured to enable forwarding.

# echo "1" > /proc/sys/net/ipv4/ip_forward
# iptables -t nat -A POSTROUTING -j MASQUERADE

However, the above setup is quite insecure since any computer connected to the gateway could use it for internet access as well. Slightly more specific (see the manpage of iptables):

# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -d 0/0 -j MASQUERADE

Using SVN

Properties

Every file or directory may have properties which may be viewed, read and set as follows.

$ svn proplist kiolocate_screenshot.png
$ svn propget svn:mime-type kiolocate_screenshot.png
$ svn propset svn:mime-type "image/png" kiolocate_screenshot*.png

Properties starting with svn: have special roles. For instance svn:executable is used to mark a file as binary while the property svn:ignore is used for the same purpose as the .cvsignore files for CVS. It may be comfortably edited with

$ svn propedit svn:ignore .

To set certain properties automatically for particular files you can edit ~/.subversion

Tools

The python script svnshell allows you to browse the repository using the usual cd and ls commands.

$ svnshell /data/svn

When backing up a directory which also contains files under version control it is useful to ignore the hidden copies in the .svn subdirectories. This can be done for instance with tar using the --exclude parameter.

$ tar --exclude=".svn" -czf backup.tar.gz files