Thursday, 6 October 2005

Enhancing the System Prompt - $PS1

If you spend a lot of time remotely logging into several machines, or just have many terminals open using the $PS1 variable can aid in keeping track of the whole mess. Even prevent you from making silly mistakes as root.
For instance if you put:
    PS1="[\[\033[1;31m\]\u\[\033[0m\]@\h \W]\\$ "
at the bottom of your /root/.bashrc file. Every time you are logged in as root, the terminal will look like this:
    [root@hostname root]#
Suppose you have seperate accounts like a developer account, a regular account and remote machines account, then you can use separate colors for each of your accounts like, for developer account (magenta), regular account (blue) and remote machines (green) so you know at a glance which is which without reading every line of output looking for that regular text that blends in.

The actual color code is "1;31" inside the PS1 variable. The 1 says make it bold, and the 31 says the color (red). Other colors are:
1;30 Black
1;32 Green
1;33 Yellow
1;34 Blue
1;35 Magenta
1;36 Cyan
1;37 White
This is just a subset of PS1 variable tricks for more information on enhancing the system prompt, read the article at www-106ibm.com.

Find the number of days elapsed since January 1st

To find out how many days have lapsed since January 1st. There is a easy method in Linux. That is using the cal command. For example, try the following :

$ cal -j 10 2005

October 2005
Sun Mon Tue Wed Thu Fri Sat
274
275 276 277 278 279 280 281
282 283 284 285 286 287 288
289 290 291 292 293 294 295
296 297 298 299 300 301 302
303 304

Since today is October 6th, as of January 1st 2005, 279 days have elapsed. The option -j displays the Julian date.

Wednesday, 5 October 2005

Configuring Xterm in Linux

Xterm is a terminal which runs in X. In linux when you open xterm, you get a small window with a small - hard to read - font by default. Compared to the ordinary xterm, the gnome-terminal and konsole come loaded with lots of features and are good to view. So why would anybody use an xterm over the other two? The answer lies in its low memory foot print. While konsole takes a whooping 8MB and gnome-terminal over 3MB of memory, you can run xterm under 1MB which makes it blazingly fast even when your computer has only 64MB of RAM.Read more »

Tuesday, 4 October 2005

Configuring Serial Wheel mouse (Microsoft Intellimouse) in Linux

I own a serial port, Microsoft Intellimouse which I have attached to my PC. But each time I reinstall, linux on the machine, the mouse fails to work. Here I will explain how to configure this mouse to work in Linux.

You can use mouseconfig script (found in Fedora) to configure the mouse. First log in as root and enter the command:
#  mouseconfig
It will ask a few questions like which port the mouse is connected (in my case the serial DOS COM1 port), what kind of mouse it is and so on. Then it creates a character device called /dev/ttyS0 and map it to the COM1 port.
Note: If your mouse is connected to the COM2 port, you have to specify it when asked in the mouseconfig script.

Alternately, you can create a character device using the "mknod" program instead of the mouseconfig script; provided you know the major and minor modes of the device. In the case of /dev/ttyS0, it is 4 and 64 respectively.
# mknod c 4 64 /dev/ttyS0
Now check whether it is working or not by doing :
# cat /dev/ttyS0
... and then moving the mouse. If you see characters being written on the screen, it means the device is working properly.

Now change the relavent section in the /etc/X11/xorg.conf file to get it working in X.
My /etc/X11/xorg.conf file's modified Input section is as follows:

# File: /etc/X11/xorg.conf
...
Section "InputDevice"
Identifier "Mouse0"
Driver "mouse"
# Option "Protocol" "IMPS/2"
Option "Protocol" "IntelliMouse"
# Option "Device" "/dev/input/mice"
Option "Device" "/dev/ttyS0"
Option "ZAxisMapping" "4 5" #For enabling wheel
Option "Buttons" "3"
Option "Emulate3Buttons" "no"
EndSection
...
The commented out lines were the default original settings after I installed Fedora core 2; and the lines below it are the settings I have inserted. "IMPS/2" protocol can be used if you have a PS/2 wheel mouse; in which case, you have to change the Device settings too.
Then you restart X.
That is all there is to it. Now you can use your Microsoft Intellimouse to its full potential in linux.

How to play VCD .dat files using mplayer

Mplayer is a versatile piece of GNU/GPLed video/audio software which supports an astronishing variety of audio and video formats. In the previous post, I had explained how to convert a VCD to MP3 format. Here is another tip to help one use mplayer to play a VCD file. As you know a VCD has video files by names avesqrt.dat . To play such files in linux (provided you have mplayer) , do the following :
$ mplayer   vcd://1
If that doesn't work then specify your cdrom (In my case it is /dev/cdrom but it may be different in your case) device as follows:
$ mplayer   -cdrom-device   /dev/cdrom   vcd://1

Note: You should not mount the cdrom for this to work.

To play a DVD
$ mplayer dvd://[title  [start_title]-end_title] [options]
To see a TV channel (Provided you have a TV tuner card)
$ mplayer tv://[channel] [options] 

Monday, 3 October 2005

Convert a VCD (avesqrt.dat) file into mp3 format

There is an easy way to convert VCD (Video CD) file into MP3. What you need are just MPlayer and Lame. MPlayer is used to convert the VCD file to WAV by using the PCM audio output, and then you can convert the WAV file to MP3 by using Lame.
First, you have to convert it to WAV by using the command:
$ mplayer -ao pcm /path/to/vcd/avseq01.dat 
MPlayer will play the VCD file like usual, but with no sound. Just wait until it finished. You'll get a file 'audiodump.wav' that you can convert to MP3 by using the command:
$ lame -h audiodump.wav newfile.mp3
Switch -h is used to get high quality MP3 file, but bigger filesize.

Disk Quotas in GNU/Linux explained

Have you ever encountered a situation where your children who are using your PC are hoarding music and video on the harddisk and filling up all the space ? In linux, there is a way for you to prohibit others from hogging all the disk space. This you do by using quotas. Here I will explain how to setup disk quotas in Linux.
Setting up disk quotas
In this example, let us assume that the /home is on its own seperate partition and it is running out of space. So we use quota system to manage and restrict disk space to all users (or a select few).
  1. Enter Single User mode - As we'll need to remount the /home filesystem it's best to ensure that no other users or processes are using it. This is best achieved by entering single user mode from the console. This may be unnecessary if you are certain that you're the only user on the system.
    # init 1
  2. Edit your /etc/fstab file - You'll need to add the usrquota option to the /etc/fstab file to let it know that you are enabling user quotas in your file system.
    ----------------------------------------------------------
    Old /etc/fstab file
    LABEL=/home /home ext3 defaults 1 2
    ----------------------------------------------------------
    New /etc/fstab file
    LABEL=/home /home ext3 defaults,usrquota 1 2
    ----------------------------------------------------------
  3. Remount your file system - Once you finish editing your /etc/fstab file, you have to remount your filesystem as follows :
    # mount -o remount /home
  4. Create aquota.user and/or aquota.group files - These are created in the top most directory of the file system. In our case /home. In our case, we are enabling only per user quotas so only aquota.user file is required.
    # touch /home/aquota.user
    # chmod 600 /home/aquota.user
  5. Make linux read the aquota.user file - This is done using the quotacheck command.
    # quotacheck -vagum
  6. Modify the user's quota information - Use the edquota command for this purpose.
    # edquota -u
    The above command will invoke the vi editor which will allow you to edit a number of fields.
    Disk quota for user ravi (uid 503):

    Filesystem blocks soft hard inodes soft hard
    /dev/hda3 24 0 0 7 0 0
    Blocks : The amount of space in 1k blocks the user is currently using
    inodes : The number of files the user is currently using.
    Soft Limit : The maximum blocks/inodes a quota user may have on a partition. The role of a soft limit changes if grace periods are used. When this occurs, the user is only warned that their soft limit has been exceeded. When the grace period expires, the user is barred from using additional disk space or files. When set to zero, limits are disabled.
    Hard Limit : The maximum blocks/inodes a quota user may have on a partition when a grace period is set. Users may exceed a soft limit, but they can never exceed their hard limit.
    In our example, we limit the user ravi to a maximum of 5MB of data storage on /dev/hda3 (/home) as follows:
    Disk quota for user ravi (uid 503):

    Filesystem blocks soft hard inodes soft hard
    /dev/hda3 24 5000 0 7 0 0
    Note: 5MB is used just for test purposes. A more realistic size will be '5 GB' if you are having a hard disk of size 20 GB.

  7. Get out of single user mode : Return to your original run level by typing either init 3 or init 5 commands.
Other quota commands

Editing grace periods

# edquota -t
This command sets the grace period for each filesystem. The grace period is a time limit before the soft limit is enforced for a quota enabled file system. Time units of seconds, minutes, hours, days, weeks and months can be used. This is what you will see with the 'edquota -t' command:

Grace period before enforcing soft limits for users :
Time units may be : days, hours, minutes or seconds
File System Block grace period Inode grace period
/dev/hda3 7days 7days
Editing Group quotas
# edquota -g
Checking quotas regularly - Linux doesn't check quota usage each time a file is opened, you have to force it to process the aquota.user and aquota.group files periodically with the quotacheck command.You can setup a cron job to run a script similar to the one below to achieve this.
#!/bin/bash
quotacheck -vagu
Getting quota reports - The repquota command lists quota usage limits of all users of the system. Here is an example.
# repquota /home

*** Report for user quotas on device /dev/hda3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft
------------------------------------------------
root 52696 0 0 1015 0 0
...
...
...
ravi 24 0 0 7 0 0