Saturday, December 6, 2008

Auto Start Applications at Login to GNOME Desktop

It may sound trivial, but for the longest time, I, being an avid CLI fan, did not configure GNOME to auto-start certain applications after login to desktop.

A couple of times, I actually went looking for some entry similar to Microsoft Windows Startup menu in the GNOME menu structures, but each time, I came up empty and frustrated.

Finally, I found out how, and I wrote it down. The following works for the GNOME Desktop 2.14.3 using the Debian Etch Linux distribution.

To configure GNOME to auto start an application on login,

  1. Mouse to Desktop -> Preferences -> Sessions.



  2. Click to select the Startup Programs Tab.
  3. Click Add.




  4. Enter the command, e.g., skype, and click OK.

    Note: You may need to enter the full path for the command, (e.g., /etc/bin/skype) if the command is not in the command path.
  5. Click Close to exit Sessions dialog box.

Saturday, November 29, 2008

How to increase number of disk mounts before next fsck at system boot

Many home users often power off their computers when they are not being used. Some do it to be green: turning idle computers off saves electricity and $$. Others do it for the extra security. To those who are hard-core Linux geeks, machine uptime is sacred, and voluntarily rebooting the machine is nothing but sacrilegious.

If you do reboot your machine from time to time, you most definitely have encountered a most annoying experience. Once in a while, while the computer is booting up, you see the message /dev/hdaN has reached maximal mount count, check forced. The check seems to take forever, and the system boot won't resume until the check is over.

The check refers to a file system check performed using the fsck command. For many Linux distributions, by default, the system will do a fsck check on a file system after it has been mounted 30 times, which means after 30 reboots. This is the maximum mount count before fsck is performed on the file system.

You can specify a maximum mount count for each individual partition on your hard drive. To find out the maximum mount count for /dev/hda1, execute this command as root:
$ tune2fs -l /dev/hda1 | grep 'Maximum mount count'
Maximum mount count: 30


Note that the tune2fs command is only applicable for ext2 and ext3 file systems.

The tune2fs command can also tell you how many times a file system has actually been mounted since the last fsck check.
$ tune2fs -l /dev/hda1 |grep 'Mount count'
Mount count: 17


To increase the maximum mount count, you will use the same tune2fs command but with the -c option.

Note that you should not modify the maximum mount count which is a file system parameter while the file system is mounted. The recommended way is to boot your system using a Linux Live CD, and then run tune2fs.

For me, I happened to have a Ubuntu 7.10 Live CD at my desk. I inserted the Live CD to boot up my system. Then, I opened a Terminal window, sudo -s, and executed the following commands.

First, I reminded myelf of how the /dev/hda disk is partitioned:
$ fdisk -l /dev/hda
Disk /dev/hda: 82.3 GB, 82348277760 bytes
255 heads, 63 sectors/track, 10011 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hda1 * 1 31 248976 83 Linux
/dev/hda2 32 10011 80164350 5 Extended
/dev/hda5 32 10011 80164318+ 8e Linux LVM


To increase the maximum mount count to 50 for /dev/hda1:
$ tune2fs -c 50 /dev/hda1
tune2fs 1.40.2 (12-Jul-2007)
Setting maximal mount count to 50


If there are more than 1 file system on your hard drive, you should stagger the maximum mount count for the different file systems so that they don't all trigger the lengthy fsck at the same time. For example, set the maximum mount count to 40, 50 and 60 for /dev/hda1, hda2, and hda3 respectively.

In the above case, /dev/hda5 is a physical LVM volume. You cannot run tune2fs on physical LVM volumes directly.
$ tune2fs -l /dev/hda5
tune2fs 1.40.2 (12-Jul-2007)
tune2fs: Bad magic number in super-block while trying to open /dev/hda5
Couldn't find valid filesystem superblock.


You need to run tune2fs against each logical LVM volume. To find out their names, cat /etc/fstab.
$ tune2fs -c 60 /dev/mapper/myhost-root 
tune2fs 1.40.2 (12-Jul-2007)
Setting maximal mount count to 60

Saturday, November 8, 2008

How to open and close the CD DVD tray

You can open/close the CD, DVD disk tray from the command line.

Many Linux users already know about the eject command for opening the disk tray:
 $ eject


How do you close the tray?

Turns out that you can use the same eject command but with an additional -t option to close the tray.

$ eject -t 


Before I found out about the -t option, I always had to reach and press the open/close button on the drive to close the tray. With the -t option, you can now both open and close the tray from the command line.

There is another option -T (CAPITAL T, that is) you should know.

eject -T
basically closes the tray if it is open, and opens the tray if it is closed.

The man page for eject has detailed explanation about the options.

Sunday, November 2, 2008

Two additional ways to tail a log file

I want to revisit a topic: how to tail a log file. My earlier posts discussed the use of tail and less commands to tail a log file, and multitail if you need to tail multiple files at once.

This followup article discussed two other ways to tail a log file: the use of the most command, and to tail within emacs.

The most command bills itself as a replacement for less. Like its predecessors less and more, most is a file pager program.

To install most on my Debian Etch system:
$ apt-get update && apt-get install most


To page the log file say /var/log/messages:
$ most /var/log/messages


This opens the log file, and displays the first page.

To tail the file, put most into Tail Mode by pressing the F key (capital F).

You should see the following line in the status area at the bottom of the tail window.
Most Tail Mode--  MOST keys are still active.


If a new line is appended to the log file, most will automatically reposition the file to the end and display the new line.

Note that you can scroll the file or do a search when you are in Tail Mode. For example, you can press the PageUp/PageDown, UpArrow/DownArrow keys to scroll the file. Also, you can press / to search forward or ? to search backward. However, when you press a key in Tail Mode, you break out of Tail mode in the sense that newly appended lines are not automatically displayed in the window. Yet, you can hit F again to re-enter Tail Mode, or simply use the DownArrow scroll key to scroll past the end of file to fetch the new lines.

To quit most, press the q key.

For my fellow emacs users out there, another way to tail a log file is to do it right within emacs.

You need the tail-file elisp function which is in the emacs-goodies-el package. Debian users can install this package like this:
$ apt-get update && apt-get install emacs-goodies.el


To tail a file in emacs: start emacs, hit M-x (Alt and x keys together), and type tail-file. Then, enter the filename to tail. The net result is that this will spawn an external tail -f process.

Note that you can't read the entire file under emacs using tail-file, only the tail initially and the newly appended lines portion afterwards.

Saturday, October 18, 2008

How to disable SSH host key checking

Remote login using the SSH protocol is a frequent activity in today's internet world. With the SSH protocol, the onus is on the SSH client to verify the identity of the host to which it is connecting. The host identify is established by its SSH host key. Typically, the host key is auto-created during initial SSH installation setup.

By default, the SSH client verifies the host key against a local file containing known, trustworthy machines. This provides protection against possible Man-In-The-Middle attacks. However, there are situations in which you want to bypass this verification step. This article explains how to disable host key checking using OpenSSH, a popular Free and Open-Source implementation of SSH.

When you login to a remote host for the first time, the remote host's host key is most likely unknown to the SSH client. The default behavior is to ask the user to confirm the fingerprint of the host key.
$ ssh peter@192.168.0.100
The authenticity of host '192.168.0.100 (192.168.0.100)' can't be established.
RSA key fingerprint is 3f:1b:f4:bd:c5:aa:c1:1f:bf:4e:2e:cf:53:fa:d8:59.
Are you sure you want to continue connecting (yes/no)?

If your answer is yes, the SSH client continues login, and stores the host key locally in the file ~/.ssh/known_hosts. You only need to validate the host key the first time around: in subsequent logins, you will not be prompted to confirm it again.

Yet, from time to time, when you try to remote login to the same host from the same origin, you may be refused with the following warning message:
$ ssh peter@192.168.0.100
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
3f:1b:f4:bd:c5:aa:c1:1f:bf:4e:2e:cf:53:fa:d8:59.
Please contact your system administrator.
Add correct host key in /home/peter/.ssh/known_hosts to get rid of this message.
Offending key in /home/peter/.ssh/known_hosts:3
RSA host key for 192.168.0.100 has changed and you have requested strict checking.
Host key verification failed.$

There are multiple possible reasons why the remote host key changed. A Man-in-the-Middle attack is only one possible reason. Other possible reasons include:
  • OpenSSH was re-installed on the remote host but, for whatever reason, the original host key was not restored.
  • The remote host was replaced legitimately by another machine.

If you are sure that this is harmless, you can use either 1 of 2 methods below to trick openSSH to let you login. But be warned that you have become vulnerable to man-in-the-middle attacks.

The first method is to remove the remote host from the ~/.ssh/known_hosts file. Note that the warning message already tells you the line number in the known_hosts file that corresponds to the target remote host. The offending line in the above example is line 3("Offending key in /home/peter/.ssh/known_hosts:3")

You can use the following one liner to remove that one line (line 3) from the file.
$ sed -i 3d ~/.ssh/known_hosts

Note that with the above method, you will be prompted to confirm the host key fingerprint when you run ssh to login.

The second method uses two openSSH parameters:
  • StrictHostKeyCheckin, and

  • UserKnownHostsFile.

This method tricks SSH by configuring it to use an empty known_hosts file, and NOT to ask you to confirm the remote host identity key.
$ ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no peter@192.168.0.100
Warning: Permanently added '192.168.0.100' (RSA) to the list of known hosts.
peter@192.168.0.100's password:

The UserKnownHostsFile parameter specifies the database file to use for storing the user host keys (default is ~/.ssh/known_hosts).

The /dev/null file is a special system device file that discards anything and everything written to it, and when used as the input file, returns End Of File immediately.

By configuring the null device file as the host key database, SSH is fooled into thinking that the SSH client has never connected to any SSH server before, and so will never run into a mismatched host key.

The parameter StrictHostKeyChecking specifies if SSH will automatically add new host keys to the host key database file. By setting it to no, the host key is automatically added, without user confirmation, for all first-time connection. Because of the null key database file, all connection is viewed as the first-time for any SSH server host. Therefore, the host key is automatically added to the host key database with no user confirmation. Writing the key to the /dev/null file discards the key and reports success.

Please refer to this excellent article about host keys and key checking.

By specifying the above 2 SSH options on the command line, you can bypass host key checking for that particular SSH login. If you want to bypass host key checking on a permanent basis, you need to specify those same options in the SSH configuration file.

You can edit the global SSH configuration file (/etc/ssh/ssh_config) if you want to make the changes permanent for all users.

If you want to target a particular user, modify the user-specific SSH configuration file (~/.ssh/config). The instructions below apply to both files.

Suppose you want to bypass key checking for a particular subnet (192.168.0.0/24).

Add the following lines to the beginning of the SSH configuration file.
Host 192.168.0.*
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null

Note that the configuration file should have a line like Host * followed by one or more parameter-value pairs. Host *means that it will match any host. Essentially, the parameters following Host * are the general defaults. Because the first matched value for each SSH parameter is used, you want to add the host-specific or subnet-specific parameters to the beginning of the file.

As a final word of caution, unless you know what you are doing, it is probably best to bypass key checking on a case by case basis, rather than making blanket permanent changes to the SSH configuration files.

If you make it this far in the article, you may find the following more recent ssh articles interesting:

Allow root ssh login with public key authentication
How to auto fill in ssh client parameters
One-liner to shutdown remote host
X11 Forwarding over SSH