Friday, November 28, 2008

Ping Every Host in /etc/hosts File.

If you want to know whether hosts specified in /etc/hosts file are reachable, just give the following command:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1

Sample run:

$ awk '/^[0-9]/{print $1}' /etc/hosts | xargs -l ping -c 1 $1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.046 ms

--- 127.0.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.028 ms

--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.028/0.028/0.028/0.000 ms


Friday, November 7, 2008

Missing Linux Entry in GRUB Menu

If any Linux entry is not present in GRUB menu,
you can visit following forum to view a solution.

Linux Forum: Unable to see Fedora 7 in GRUB Menu

Wednesday, November 5, 2008

Finding Best Linux Distribution

We can find ranking of Linux distributions (distros) at site distrowatch.com. This ranking is based on Page Hits Per Day.In order to find best Linux distro, I have written a Perl script:

#!/usr/bin/perl -w
#===============================================================================
#
# FILE: best_distro.pl
#
# USAGE: ./best_distro.pl
#
# DESCRIPTION: Finds best distro from distrowatch.com
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: Mitesh Singh Jat (mitesh)
# VERSION: 1.0
# CREATED: Tuesday 04 November 2008 04:05:19 IST
# REVISION: ---
#===============================================================================

use strict;
use warnings;

my $ndistro = 1;

if (@ARGV >= 1)
{
$ndistro = $ARGV[0];
if ($ndistro > 100)
{
$ndistro = 100;
}
}

my $lynx = `which lynx`;
chomp($lynx);
if ($lynx eq "")
{
print STDERR "$0: please install lynx\n";
print STDERR "sudo apt-get install lynx\n";
exit(-1);
}

open(LYNX, "lynx -source http://distrowatch.com/ |") or die("$0: cannot open lynx: $!\n");

my $i = 1;

while ($i <= $ndistro)
{
my $line;
my $distro;
my $rank = "";
while ($line = <LYNX>)
{
chomp($line);
$line =~ s/<th class=([^>]*)>(.*?)<\/th>/$2/i;
$line =~ s/(\s+)//g;
if ($line eq "$i")
{
$rank = $line;

unless ($line = <LYNX>) {last;}
chomp($line);
$line =~ s/<td class=([^>]*)><a href=([^>]*)>(.*?)<\/a><\/td>/$3/i;
$line =~ s/(\s+)//g;
$distro = $line;

print "$rank\t$distro\n";
last;
}
}
++$i;
}

close(LYNX);

Sample runs of above program:
$ ./best_distro.pl
1 Ubuntu
$ ./best_distro.pl 5
1 Ubuntu
2 openSUSE
3 Mint
4 Fedora
5 Debian

Tuesday, November 4, 2008

Counts from min to max

In order to get counts from start number to end number,
I have written a shell script.


#!/bin/sh

if [ $# -ne 2 ]
then
echo "Usage: $0 min max"
exit
fi


min=$1
max=$2

while [ $min -le $max ]
do
echo -n $min " "
min=`expr $min + 1`
done
#end script



Sample run:
$ ~/bin/count.sh 2 5
2 3 4 5

Tip: This is very handy script, that is very helpful
in getting counts in the loop. For example:
$ for i in `~/bin/count.sh 2 8`
> do
> echo "Hello $i"
> done

Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8

Friday, October 17, 2008

Total RAM in your System

In order to find total RAM in your system, give following command:

$ cat /proc/meminfo | awk '/^MemTotal/ {print $2/1024 " MB"}'
2008.07 MB

If you all information related to memory:

$ cat /proc/meminfo


Extra:
Total CPU cores in your system:

$ cat /proc/cpuinfo | awk '/^processor/ {ncores++} END {print "Total cores = " ncores}'
Total cores = 2

Thursday, October 16, 2008

Finding Processes, which are Accessing a File System

After completing every task related to CD/DVD/USB, you might find it annoying to unmount/eject CD/DVD/USB, while it is being used by some unknown/stuck process. Therefore I am providing a solution (fuser). Suppose your were accessing CDROM mounted at /mnt/cd/.

To view which files (of CD) are being accessed by whom :
$ fuser -mu /mnt/cd/*

If you are sure that, the processes are stuck/hang. Then you can kill all those processes :
$ fuser -kmu /mnt/cd/*


Warning!!!: If you are using writable medium (like USB stick), please give following command before killing processes:
$ sudo sync

Wednesday, October 15, 2008

Retrieving Date and Time from Unix Timestamp

In Unix, we can retrieve Date and Time from Timestamp by using following command:
$ date -r 1220000000
Fri Aug 29 14:23:20 IST 2008

But in Linux, there is no '-r' option. Therefore, I have written a Perl script to
retrieve date and time from timestamp. Here is the script:

$ vi ~/bin/dater.pl

#!/usr/bin/perl -w

use strict;
use warnings;

my $ts = time;
if (@ARGV >= 1)
{
    $ts = $ARGV[0];
}
my ($sec, $min, $hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($ts);
++$mon;
$year+=1900;
print "$mday-$mon-$year $hour:$min:$sec\n";


# end dater.
pl


Sample runs of above program:
$ dater.pl
15-10-2008 12:34:37
$ dater.pl 1220000000
29-8-2008 14:23:20

PS: you can change parameters in print function in the program
according to output you desire.



Monday, October 13, 2008

Terminating Processes Containing Given Pattern

Generally, if you want to terminate a process which contains the given pattern, you use following 4 steps:

  1. $ ps aux | grep pattern 
  2. Copy the pid of the process from output of above command.
  3. Paste this pid in the following command
  4. $ kill -9 pid

The above procedure is cumbersome and requires much typing.

Therefore I have written a program to terminate process(es), which contains given pattern. (I am assuming that ~/bin is in your $PATH).

$ vi ~/bin/kill_prog.sh

#!/bin/sh

# Kills the program given


if [ $# -lt 2 ]
then
  echo "Usage: $0 [1/0] signal program-name"
  echo "[0/1] Do not Kill / Do kill (optional)"
  echo "signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)"
  exit
fi

donothing=1
signal=$1
program=$2
if [ $# -gt 2 ]
then
  donothing=$1
  signal=$2
  program=$3
fi

pids=$(ps auxww | awk "/ $program / { print \$2 }")


for pid in $pids
do
  if [ $donothing -eq 1 ] ; then
  echo "kill $signal $pid"
  else
  kill $signal $pid
  fi
done

#end kill_prog.sh

By default, this program does not kills the process. You have to

pass 0 as second parameter to the command to kill.

Sample runs of above programs:

$ kill_prog.sh
Usage: /home/mitesh/Programming/Shell/WCS/kill_prog.sh [1/0] signal program-name
[0/1] Do not Kill / Do kill (optional)
signal -TERM (Graceful Terminate), -KILL (Abrupt KIll)

$ kill_prog.sh -TERM ping
kill -TERM 16050
$ kill_prog.sh -KILL ping
kill -KILL 16050
$ kill_prog.sh 1 -KILL ping
kill -KILL 16050
$ kill_prog.sh 0 -KILL ping
Killed

The last command actually terminates(-TERM) or kills(-TERM).

Friday, October 3, 2008

Shell Session Recording

In order to record the commands and their output in a shell session,
you can use script command. For example:

[mitesh@linuxbox:370:~]$ script
Script started, file is typescript
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
mitesh
[mitesh@linuxbox:373:~]$ exit
exit

Script done, file is typescript

You can view the output file typescript file using your favorite editor.
$ cat typescript
Script started on Friday 03 October 2008 04:51:27 PM IST
[mitesh@linuxbox:370:~]$ ls
CvsRoot Desktop Documents Download Examples linux Music PDF Pictures Programming Public Templates typescript Videos workspace
[mitesh@linuxbox:371:~]$ /bin/date
Fri Oct 3 16:51:42 IST 2008
[mitesh@linuxbox:372:~]$ whoami
mitesh
[mitesh@linuxbox:373:~]$ exit
exit

Script done on Friday 03 October 2008 04:51:54 PM IST

PS: you can save the output to any file, you want instead of typescript in
current directory. For example
$ script shell_session_20081003.log
...
...
$ exit
$ cat shell_session_20081003.log

Listing the Last Times a User Logged In

To output a list of top 10 recent system users:

$ last | head

To output a list of last 10 logins of a user (say: mitesh):

$ last mitesh | head

Note: This command is very useful in determining who has
logged in the system at particular moment. Helpful in forensics ;)

Wednesday, October 1, 2008

Viewing PDF Files on Terminal

First you need to convert a PDF document to HTML, then you run it through the elinks pager. There's a fine utility for doing just that, and it's called (appropriately) pdftohtml. You can find the home page for pdftohtml. If pdftohtml isn't already installed in your distribution of Linux, or isn't on your CD set, it's commonly available for Debian and RPM-based distributions, such as Fedora, SUSE, and more. The elinks program is also easily available if it isn't automatically installed in your distribution.
For example, you can install pdftohtml and elinks in Debian Linux with this command:
# apt-get install pdftohtml elinks

Users of the yum package can get the RPM version with this command:
# yum -y install pdftohtml elinks

Now you can view a PDF document with the following command. This particular command has one drawback. The output will not include frames (PDF files generally have a frame on the left that lets you jump to different pages).

$ pdftohtml -q -noframes -stdout document .pdf | elinks

If you want the left frame of page numbers, you can always use the following command instead:

$ pdftohtml -q document .pdf ; elinks document .html

You can write a script to save you all this typing each time you view a document. Use sudo or log in as root to create the /usr/local/bin/viewpdf script and enter the following code:

#!/bin/bash

pdftohtml -q $1 ~/temp.html
elinks ~/temp.html

#
#end of script

This code assumes it's OK to store the temporary HTML file in your home directory. You can use another location if you prefer. Now save your work and make the file executable:

$ sudo chmod +x /usr/local/bin/viewpdf

Create your own personalized boot splash backgrounds for GRUB

The default GRUB bootloader screen is rather bland, but you can spice it up a little by creating your own custom graphical background screen for the bootloader.
GRUB imposes a number of limitations on the image size and number of colors. It also doesn't let you move the menu. The menu appears in a rectangle near the top of the screen, with some text instructions below the menu. This makes it relatively easy to create a graphical background screen for the GRUB bootloader, because you can focus primarily on making the bottom one-third of the screen interesting. That is not to say you cannot use other areas of the screen, but you should be careful. For example, don't make it difficult to read the GRUB instructions by placing complex graphics behind the text.

Here are the rather strict requirements for the image:
  • It must be sized at 640x480.
  • It must be a .xpm format image file (gzip compression is optional).
  • It must contain no more than 14 colors.Most people will cringe at the 14-color limit, but it is rather amazing what you can do with just 14 colors. Cartoon images are quite suitable for this narrow range of colors, and the narrow range of colors to represent the official Linux penguin (Tux) works fine.Find or create any image you want to try as a background for GRUB.

If you create an image yourself, it's best to create a 640x480 image and use as few colors as possible so that you don't lose the fidelity of the image when you later reduce it to 14 colors. Don't worry about using your graphics editor to limit yourself to 14 colors, however. It is possible to use the Gimp to reduce your image to use 14 colors, which can be a good way of fine-tuning the results you want.
Here is what you need:
A graphics editor, such as the Gimp, if you want to create or modify an image.
You must install ImageMagick if it is not already installed. Nearly all Linux distributions provide this on the install CD, and you can use your preferred package manager to install it.Suppose you have found or created the image myimage.png.
If you have ImageMagick installed, all you need to do to prepare the image is log in as root and issue these commands:

# convert myimage.png -colors 14 -resize 640x480 myimage.xpm

The convert command recognizes the extension png and knows what format it must convert the image from. It also recognizes the extension xpm and knows what format to convert the image to. The -colors 14 switch reduces the number of colors in the image to 14. If the image isn't already sized at 640x480, the switch -resize 640x480 will do that for you.

This is sample myimage.xpm

Changing RunLevel in Linux

When a Linux system is booted, the first process that the kernel starts is /sbin/init. It is always process id (PID) 1 and has a parent process id (PPID) of 0. The init process is always running.
The /etc/inittab file is the configuration file for /sbin/init. /etc/inittab identifies the processes that init starts, and it can be customized as desired. Few environment variables are set when a process is started by init.
The inittab lines have four colon-separated fields:

id:runlevel:action:command

Let's look at the meaning of each.
(i). id The inittab id consists of one to four characters that identify the inittab line. The id must be unique.
(ii). runlevels The runlevels field contains one or more characters, usually numbers identifying the runlevels for which this process is started.
  • 0 System Halt
  • 1 Single user mode
  • 2 Local multiuser without remote network (e.g., NFS)
  • 3 Multiuser with network
  • 4 Not used
  • 5 Multiuser with network and xdm
  • 6 System reboot
You can change runlevel by changing this field, to the number
you want. And reboot the machine.

Wednesday, September 17, 2008

How to find out which process is listening upon a port?

How to find out which process is listening upon a port (say 6000) ?

$ lsof -i :6000

Alternatively, you can use following command:

$ fuser -v -n tcp 6000

Find Command : A Swiss Army Knife

Find is a very useful and powerful utility. It is often used by system administration and in shell scripts. Here are 6 commands that might be useful:

$ find / -perm -4000 -print

This command will find every file on the system that is suid. This means that when you run it you will be running it as an other user. For example, traceroute is a utility that needs to be run as root. To allow users to run it, systems administrators will set it suid root so it will be run as root even if a user starts it. This can be useful, but can also be a big security risk if the utility has a security hole in it.

Here is another interesting command:

$ find / -atime +10 -print

This command will find all the files accessed more than 10 days ago. Commands like this one can be useful to find old files that need to be backuped or erased.

Find links that point to nothing

$ find / -type l -print | perl -nle '-e || print';

List zero-length files

$ find . -empty -exec ls {} \;

Finding files which contains a given pattern:

$ find . -type f -exec grep "pattern" /dev/null {} \;

Finding large files (>= 100 MB) in your home directory:

$find ~/ -size +100M

Saturday, September 6, 2008

Creating Bootable USB from System Rescue CD

Creating Bootable USB from System Rescue CD
In order to create bootable USB stick from CD image (ISO file)
of System Rescue Linux, generally we follow steps given
in this link. After following instructions given, I have created
a small shell script to create bootable rescue USB.

The contents of the script create_rescue_usb.sh are:
$ cat create_rescue_usb.sh
#!/bin/bash

if [[ $# < 4 ]]
then
  echo "Usage: $0 CDROM_Device CDROM_mount USB_Device USB_mount"

  exit

fi


CDROM_Device=$1

CDROM_mount=$2
USB_Device=$3

USB_mount=$4


if
 [[ -e $CDROM_Device ]]
then
  echo "$CDROM_Device is present."
else
  echo "$0: $CDROM_Device is not present."
  exit
fi

if
 [[ -e $USB_Device ]]
then
  echo "$USB_Device is present."
else
  echo "$0: $USB_Device is not present."
  exit
fi

echo "Mounting..."
mount -o loop $CDROM_Device $CDROM_mount

if
 [ $? -ne 0 ]
then

  echo "$0: cannot mount $CDROM_Device on $CDROM_mount."
  exit
fi

mount $USB_Device $USB_mount


i
f [ $? -ne 0 ]
then

  echo "$0: cannot mount $USB_Device on $USB_mount."
  umount $CDROM_mount
  exit
fi

echo "Copying Files..."
cp $CDROM_mount/syslinux/syslinux.cfg $USB_mount/
cp -r $CDROM_mount/isolinux/* $USB_mount/
cp -r $CDROM_mount/bootdisk/* $USB_mount/
cp $CDROM_mount/sysrcd.dat $USB_mount/

echo "Unmounting..."
umount $USB_mount
umount $CDROM_mount

echo "Making $USB_Device bootable..."
syslinux $USB_Device
sync

echo "$USB_Device is now ready to serve as bootable Linux USB."
#end of script


Now, you can make it executable.
$ chmod u+x create_rescue_usb.sh

Sample execution of above script (Please run as root user):
# ./create_rescue_cd.sh /path/of/system-rescue-cd.iso /mnt/cdrom /dev/sdb /mnt/usb

Where,
a. /path/of/system-rescue-cd.iso -- ISO image of the CD can be created as given in blog Creation of ISO Image of CD/DVD.
b. /mnt/cdrom -- directory, where you want to mount above ISO.
c. /dev/sdb -- or /dev/sdb1, your usb-stick device. May differ
according to your system configuration
d. /mnt/usb -- directory, where you want to mount usb-stick.

Now, your usb stick is ready to serve as bootable rescue USB.
You can test it. At the time of BIOS boot, please select USB device
as first boot device in BIOS. then save and reboot with the usb-stick
plugged in one USB port.

Wednesday, August 27, 2008

How to Download mms:// (MultiMedia Stream) Video?

If you are not able to download the mms://a-web-site/stream_video.wmv
and only able to play via streaming. But you might have slow speed,

Then, you can use mencoder to download above file.

$ mencoder -oac copy -ovc copy -o out_video_file.wmv mms://a-web-site/stream_video.wmv

PS. If you want to view via streaming, you can use mplayer:

$ mplayer mms://a-web-site/stream_video.wmv

Friday, August 22, 2008

Denial of Service (DoS) Prevention

You can prevent DoS on you machine, by running following
commands as root user. Or you can put following lines
in a shell script and run it as root.

# shut some DoS stuff down
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 1 > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts

# increase the local port range
echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range

# increase the SYN backlog queue
echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog

echo 0 > /proc/sys/net/ipv4/tcp_sack
echo 0 > /proc/sys/net/ipv4/tcp_timestamps

Wednesday, August 6, 2008

How to stop certain network to start at boot time in Fedora?

For example, if you do not want wlan0 to be started at boot time,
just move ifcfg-wlan0 file from /etc/sysconfig/network-scripts/ directory
to any other directory.

I am telling not to remove the file, because you can start
wlan0 interface after copying the file back to /etc/sysconfig/network-scripts/
whenever you want. And give command

# service network restart

Friday, August 1, 2008

Fixing tsocks 1.8 Beta

I was trying to use tsocks 1.8 beta5 on Fedora 8. After installing
I was able to use it. But, on next day, what I found is: I was not
able to access any website, messenger service. Although, I was
able to ping websites. Then I realized the tsocks is interfering
with direct connection to Internet. I was not using ssh tunnel at
that moment.

I have created patch for Makefile and tsocks script.

$ vi patch_Makefile

5c5

< prefix=/usr

---

> prefix=/usr/local

9c9

< libdir = /lib

---

> libdir = ${prefix}/lib

31c31

< CFLAGS = -g -O2 -Wall

---

> CFLAGS = -O3 -Wall



$ vi patch_tsocks
6c6

< # /usr/bin/tsocks program [program arguments...]

---

> # $PREFIX/bin/tsocks program [program arguments...]

13c13

< # /usr/bin/tsocks telnet www.foo.org

---

> # $PREFIX/bin/tsocks telnet www.foo.org

20c20

< # . /usr/bin/tsocks on

---

> # . $PREFIX/bin/tsocks on

22c22

< # . /usr/bin/tsocks off

---

> # . $PREFIX/bin/tsocks off

26c26

< # source /usr/bin/tsocks on

---

> # source $PREFIX/bin/tsocks on

28c28

< # source /usr/bin/tsocks off

---

> # source $PREFIX/bin/tsocks off

33c33

< # /usr/bin/tsocks

---

> # $PREFIX/bin/tsocks

39a40,41

> PREFIX="/usr/local"

>

49c51

< export LD_PRELOAD="/usr/lib/libtsocks.so"

---

> export LD_PRELOAD="$PREFIX/lib/libtsocks.so"

51,52c53,54

< echo $LD_PRELOAD | grep -q "/usr/lib/libtsocks\.so" || \

< export LD_PRELOAD="/usr/lib/libtsocks.so $LD_PRELOAD"

---

> echo $LD_PRELOAD | grep -q "$PREFIX/lib/libtsocks\.so" || \

> export LD_PRELOAD="$PREFIX/lib/libtsocks.so $LD_PRELOAD"

56c58

< export LD_PRELOAD=`echo -n $LD_PRELOAD | sed 's/\/usr\/lib\/libtsocks.so \?//'`

---

> export LD_PRELOAD=`echo -n $LD_PRELOAD | sed 's/\$PREFIX\/lib\/libtsocks.so \?//'`

71c73

< export LD_PRELOAD="/usr/lib/libtsocks.so"

---

> export LD_PRELOAD="$PREFIX/lib/libtsocks.so"

73,74c75,76

< echo $LD_PRELOAD | grep -q "/usr/lib/libtsocks\.so" || \

< export LD_PRELOAD="/usr/lib/libtsocks.so $LD_PRELOAD"

---

> echo $LD_PRELOAD | grep -q "$PREFIX/lib/libtsocks\.so" || \

> export LD_PRELOAD="$PREFIX/lib/libtsocks.so $LD_PRELOAD"


How to apply patch and install tsocks?

$ tar -zxvf tsocks-1.8beta5.tar.gz


$ cd tsocks-1.8


$ ./configure


$ patch Makefile < patch_Makefile

$ patch tsocks < patch_tsocks


$ make


$ sudo make install


PS: If you do not find patches working, please mail me
at mitesh[dot]singh[dot]jat[at]gmail[dot]com . I will
send both patches to you.

Monday, July 28, 2008

Finding the Largest and the Smallest File in a Directory

1. In order to find 9 largest files in a directory (say /etc),
please give following command:
$ ls -lS /etc | head

total 2816
-rw-r--r-- 1 root root 600399 2008-07-28 10:11 prelink.cache
-rw-r--r-- 1 root root 362047 2007-04-18 14:10 services
-rw-r--r-- 1 root root 144190 2007-10-12 15:08 lynx.cfg
-rw-r--r-- 1 root root 122098 2008-07-25 10:47 ld.so.cache
-rw-r--r-- 1 root root 117276 2007-09-17 17:57 Muttrc
-rw-r--r-- 1 root root 114765 2007-10-11 01:28 gwen-public-ca.crt
-rw-r--r-- 1 root root 84649 2007-08-23 12:54 sensors.conf
-rw-r--r-- 1 root root 44990 2007-10-16 18:50 php.ini
-rw-r--r-- 1 root root 31274 2007-10-09 14:33 jwhois.conf

2. In order to find 9 smallest files in a directory (say /etc),
please give following command:

$ ls -lSr /etc | head
total 2816
-rw-r--r-- 1 root root 0 2007-08-03 06:09 odbc.ini
-rw-r--r-- 1 root root 0 2000-01-13 04:48 motd
-rw-r--r-- 1 root root 0 2000-01-13 04:48 exports
-rw-r--r-- 1 root root 0 2007-08-16 19:23 environment
-rw-rw-r-- 1 root disk 0 2007-08-22 17:15 dumpdates
-rw-r--r-- 1 root root 0 2007-09-24 19:28 cron.deny
-rw------- 1 root root 1 2007-10-05 17:41 at.deny
lrwxrwxrwx 1 root root 7 2008-07-18 03:26 rc -> rc.d/rc
lrwxrwxrwx 1 root root 10 2008-07-18 03:26 rc6.d -> rc.d/rc6.d

Wednesday, July 23, 2008

Installing Linux (Fedora 8) on Macbook Pro

I was trying to install Linux on Macbook Pro along with Mac OS X, because
it has Intel Processor, Intel chipset on Motherboard
and Nvidia 8600 GT graphics card. I followed steps
given in Debian Wiki (Mainly upto rEFIt part) and
in Mactel (for remaining part).

I would like to suggest some tips regarding installation of
Fedora 8 along with Mac OS X on Macbook Pro.

  1. Audio: Download the latest Linux kernel and compile it with Intel HD Audio drivers 82801H (ICH8 Family).
  2. Video: Boot into new kernel, and install NVidia driver, restart X server by logging out and logging in.
  3. Keyboard: Download pomme daemon, Compile and install it. Now Optical Drive Eject button will work.
  4. Backlight: Even after installing pommed, I was seeing very bright screen. I was not able to stare at screen for more than a minute. Later, after some tweaking of /etc/pommed.conf , I was able to decrease the backlit. I changed init = -1 to init = 1, as shown below:
# nVidia GeForce 8600M GT backlight control (MacBook Pro v3 & v4)
lcd_nv8600mgt {
# initial backlight level [12] (0 - 15, -1 to disable)
init = 1
# step value (1 - 2)
step = 1
# backlight level when on battery [6] (1 - 15, 0 to disable)
on_batt = 6
}

PS: Everything else should work as given in Mactel site. Feel free to write
comments. If you have any doubt, mail me at mitesh[dot]singh[dot]jat[at]gmail[dot]com

Friday, July 18, 2008

What is Pseudo Terminal (PTY)?

Honestly, I did not know about the Pseudo Terminal before facing a problem. I had overwritten /etc/fstab in Fedora 8. As a result, I was not able to open either Terminal or Konsole. The error, I was getting, was:


Not enough permission for PTY device.



Then, I searched Internet for PTY device, there I found the full form of PTY, which is Pseudo Terminal.

Like the /dev directory, /dev/pts contains entries corresponding to devices. But unlike /dev, which is an ordinary directory, /dev/pts is a special directory that is created dynamically by the Linux kernel.The contents of the directory vary with time and reflect the state of the running system.

The entries in /dev/pts correspond to pseudo-terminals (or pseudo-TTYs, or PTYs). Linux creates a PTY for every new terminal window you open and displays a corresponding entry in /dev/pts.The PTY device acts like a terminal device—it accepts input from the keyboard and displays text output from the programs that run in it. PTYs are numbered, and the PTY number is the name of the corresponding entry in /dev/pts.

Then, I searched for how to open Pseudo Terminal with enough permissions.

Solution: Since I had overwritten the /etc/fstab, the entry to

mount devpts was not present. I then created the following entry in /etc/fstab file.


# file_system mount_point type options dump pass

none /dev/pts devpts (rw,mode=620) 0 0

Thursday, July 10, 2008

Password-less SSH for Batch Processing

1. Generation of Public-Private RSA key pair.

$ mkdir -p ~/.ssh
$ cd ~/.ssh
$ ssh-keygen -f keyname-identity -P '' -t rsa1

2. Allowing Password less Login on remote-machine.

Copy your public key on remote-machine.
$ scp keyname-identity.pub remote-machine:./.ssh/authorized_keys

Edit authorized_keys on remote-machine
Please place this line before your public key
in the authorized_keys file content
from="local_machine",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,command="remote_command_you_want_to_execute_on_local-machine"

For example:

from="local-machine",no-pty,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,command="ls -la" 2048 35 1383...

3. Please make your private key secure.

$ chmod 511 ~/.ssh
$ chmod 400 ~/.ssh/keyname-identity

!! Caution !!

Putting a password less key in a file is exactly like writing a password on a piece of paper. A person who can access to your key file can do whatever you can do with the key.

Tuesday, July 8, 2008

Creation of ISO Image of CD/DVD

If you want to create ISO image of CD/DVD in the
optical drive, please give the following command:

$ dd if=/dev/hdc of=/path/of/iso/image/xyz.iso

You can replace /dev/hdc (for Secondary Master)
with the CD/DVD drive path on your system. like

/dev/hdb (for Primary Slave)
/dev/sdc (for SATA optical drive,
or newer drive naming convention)

Tuesday, July 1, 2008

Most CPU Using Processes

The following 1 line script prints the processes which are using CPU most of the time:

$ top -d 3600 -n 24 | awk '{arr[$13] += $10} END { for (x in arr) print x" \t"arr[x]; }' | sort -r -n -k2 > daily_stats

You can vary value of -d ---- and -n -- of top, to increase accuracy of
statistics. i.e. (delay) * (count) should be 86400 always for daily
statistics.
for example:

$ top -d 1800 -n 48 | awk '{arr[$13] += $10} END { for (x in arr) print x" \t"arr[x]; }' | sort -r -n -k2 > daily_stats

Thursday, June 26, 2008

Bandwidth Limiting SCP (Secure CoPy)

You can also limit the bandwidth scp may use when copying.
This is very useful if you want to copy a huge amount of data
without suffering from slow network for a long time. Limiting
bandwidth is done in this way:

$ scp -l bandwidthlimit_in_kbps username@remote_host:/path/to/file .

The bandwidth is specified in Kbit/sec. What does this mean?
Eight bits is one byte. If you want to copy no faster than
10 Kbyte/sec, set the limit to 80. If you want to copy no faster
than 80 Kbyte/sec, set the limit to 640. You should set the limit
to eight times the maximum Kbyte/sec you want it to be.
I would recommend to set the -l option with all scp'ing you do
on a connection that other people need to use, too. A big amount
of copying can virtually block a whole 10 Mbit network if you are
using hubs.

Tuesday, June 24, 2008

Reducing Booting Time in Linux

You can reduce the booting time in Linux, by disabling the
services, which are not being used by you.

There is a directory for each run-level in /etc. Those
are named as /etc/rc.< run-level > , where
run-level = 0~6

On most Linux distros, default run-level is 5, in Debian default
run-level is 2.

1. Go to your run-level directory /etc/rc.n

# cd /etc/rc.5

2. If you want to disable, Apache Web-Server (apache), just
you have to rename, the corresponding file my replacing 'S'
(start) by 'K' (kill).

# mv S91apache K91apache


Likewise, you can disable all the undesired services.
This procedure will make your linux not only fast,
but also secure.

Monday, June 16, 2008

Prevention of Man-in-Middle Attack

Having anticipated, Man-in-Middle attack by ARP Spoofing. a problem,
(For example: there are many lab machines which have NFS access to user
disks on a server. These machines may even be turned OFF which makes it
easy for a spoofer to get in.), I wrote a short Perl script designed to
be run from the system startup file. Basically, it fills the ARP cache
on Linux with the IP and MAC addresses of known machines, setting a flag
so that they are never removed from the cache and can never be changed.

The config file format is simple -- IP address followed by MAC address,
separated by whitespace. Pound at the beginning of a line indicates
comment.
For example:
# vi ip_mac.conf
# IP_Address MAC_Address
10.1.1.2 aa.bb.cc.dd.ee.ff
...
...


This has only been tested on Linux -- people on other platforms may need
to adjust the parameters to arp in the system call.

It is a quick 'n' dirty program, but works -- maybe it will be useful to
somebody out there, too.

Note: you want to make sure that it is run after your network interface is
brought up but before any servers or clients are started; otherwise,
somebody may be able to sneak in a connection before the ARP tables are
"locked".

Here is the Perl script:

# vi force_hw_addr.pl

#!/usr/bin/perl -w
# Program: force_hw_addr.pl
# Program to run ARP to force certain tables.

# Specify filenames(Redirection) or stdin

foreach (<>) # For each input line....
{
chomp; # Strip if CR/LF
if (/^#/)
{
next;
} # If it's a comment, skip it.
if (((($host, $hw) = /\s*(.+?)\s+(\S+)\s*/) == 2) &&
!(/^#/))
{
# The text between the slashes parses the input line as follows:
# Ignore leading whitespace. (\s*)
# Then, start matching and put it into $host ($host, (.+?))
# Skip over the whitespace after that (\s+)
# Start matching. Continue matching until end of line or optional
# trailing whitespace.

# Then, the if checks to see that both a
# host and a hardware address were matched.
# (2 matches). If not, we skip the
# line (assuming it is blank or invalid or something).
# The second part of the if checks to see if the line starts with
# a pound sign; if so, ignore it (as a comment).

# Otherwise, run the appropriate command:
printf("Setting IP %-15s to hardware address %s\n", $host, $hw);
system "/usr/sbin/arp -s $host $hw\n";
}
}

Example execution.

# ./force_hw_addr.pl < ip_mac.conf

I hope that this script will help you access resources in

your network and prevent DoS/Man-in-Middle Attack.

Tuesday, June 10, 2008

Process Status of Any Process Containing given String

In order to get Process Status (ps) of any process containing given string
(say 'mitesh'), we use to type following commands, which are long and
tedious to type.

$ ps auxww | grep "mitesh" | grep -v grep

Instead, if we type

$ psg.sh mitesh

which is more convenient to type. So what this '
psg.sh'
contains (I am assuming, that
~/bin is in $PATH):

$ vi ~/bin/psg.sh

#!/bin/bash
function is ()
{
ps auxww | grep "$@" | grep -v "grep"
}

is $@

# END : psg.sh

Ethernet Configurations on Fedora/RHEL

In order to configure ethernet (Usually eth0 for first ethernet card),
open /etc/sysconfig/network-scripts/ifcfg-eth0 file.

# vi /etc/sysconfig/network-scripts/ifcfg-eth0

#!/bin/sh
#>>>Device type: ethernet
#>>>Variable declarations:
DEVICE=eth0
IPADDR=192.168.0.2
NETMASK=255.255.255.0
NETWORK=192.168.0.0
BROADCAST=255.255.255.255
GATEWAY=192.168.0.1

# Whether to make available after boot
# or enable at root's consent
ONBOOT=no

# PROTO = dhcp, none

#>>>End variable declarations

Booting in Single User Mode in Linux

At the boot time, you usually see GRUB (GRand Unified Boot) Loader.

You select any one option of available OSs from the menu.

For example, if you have Windows and Linux.
You will get 2(or more) options to boot.

In order to boot into single user mode, you select desired
linux and press 'e' in grub menu, you will see new window
with something given below

root (hd0,0)
kernel /boot/vmlinuz-2.6.22.6 root=/dev/sda1 ro resume=/dev/sda3
initrd /boot/initrd.img-2.6.22.6
savedefault

Now, you append '1' or 'single' in the second line (kernel) as kernel
parameter. For example

kernel /boot/vmlinuz-2.6.22.6 root=/dev/sda1 ro resume=/dev/sda3 1

or

kernel /boot/vmlinuz-2.6.22.6 root=/dev/sda1 ro resume=/dev/sda3 single

Now, press 'b', to boot into single user mode. :)

Monday, June 9, 2008

Shortcuts for Working in BASH (Bourne Again SHell)

Navigation
Left/right cursor key --- Move left/right in text
Ctrl+A --- Move to beginning of lIne
Ctrl+E --- Move to end of line
Ctrl+right arrow --- Move forward one word
Ctrl+left arrow --- Move left one word

Editing
Ctrl+U --- Delete everything behind cursor to start of line
Ctrl+K --- Delete from cursor to end of line
Ctrl+W --- Delete from cursor to beginning of word
Alt+D --- Delete from cursor to end of word
Ctrl+T --- Transpose characters on left and right of cursor
Alt+T --- Transpose words on left and right of cursor

Miscellaneous
Ctrl+L --- Clear screen (everything above current line)
Ctrl+U --- Undo everything since last command
Alt+R --- Undo changes made to the line
Ctrl+Y --- Undo deletion of word or line caused by using Ctrl+K, Ctrl+W, and so on
Alt+L --- Lowercase current word (from the cursor to end of word)


Note: If you find these shortcuts hard to remember and you know vi(m),
you can enable vi mode for editing command line using following command:

$ set -o vi

To enable vi mode from start of Bash, add following lines to your ~/.bashrc
# Start vi Mode for command line editing
set -o vi



Monday, June 2, 2008

Backup Using TAR

Tar utility can be used to take backup. I have created a
handy script to do backup of files/directories(mentioned in
tar_include.txt).

# vi my_backup.sh

1 out_file=/mnt/Backup/backup_`date +20%y%m%d`.tar.gz

2
3 echo "Creating $out_file ..."
4
5 tar -zcpvf $out_file -T tar_include.txt -X tar_exclude.txt
6
7 sync
8 echo "Completed Backing up"

# vi tar_include.txt
1 /home/mitesh/.purple/
2 /home/mitesh/.thunderbird/
3 /home/mitesh/Programming/
4 /home/mitesh/Documents/
...
...

You can exclude files/directories inside those
mentioned in tar_include.txt. Just you have to
write files/directories in tar_exlcude.txt .
# vi tar_exclude.txt
1 /home/mitesh/Programming/Perl/Modules/*
2 /home/mitesh/Documents/CrawlDoxy/*
...
...

Now run the above shell script to get backup with
date in /mnt/Backup/ .

Note: You can take regular backups using above scipt too.
Just you have to give interval and path of above script
in /etc/crontab file.

Friday, May 23, 2008

Enabling Hibernate on Linux

Yesterday, I was thinking to enable hibernate on
my HP-Compaq nc-6400 laptop.
Some prerequisites for enabling hibernate on Linux:

(i). ACPI should be enabled in Linux kernel.
I have compiled kernel 2.6.22.6

(ii) Go to KDE Control Center > Power Control
> Laptop Battery. Then select 'ACPI Config',
click 'Setup Helper Application'. If ACPI
is supported on your PC, you can see that
check boxes are enabled.

(iii) kpowersaved daemon should be installed.

(iv) You should have swap partition >= RAM on your
PC.

I did not have the swap partition. :( Then I thought
to create it. Because swap file cannot act as resume
device. i.e. At the boot time kernel should know the
resume device before file system.

How did I create swap partition? Following are the
steps:

(a) I created 1 GB partition /dev/sda3 from /dev/sda2
using gparted.

(b) I formatted it and made swap.

# mkswap /dev/sda3

(c) To mount it (as swap) automatically at boot time,
it created following entry in /etc/fstab.

/dev/sda3 swap swap defaults 0 0

(d) I then modified /boot/grub/menu.lst , to make /dev/sda3
as resume device.

I changed booting kernel line from
kernel /boot/vmlinuz-2.6.22.6 root=/dev/sda1 ro
to
kernel /boot/vmlinuz-2.6.22.6 root=/dev/sda1 ro resume=/dev/sda3

(e) Then I rebooted the kernel with resume device. Now I
can hibernate (Suspend to disk) my laptop, using kpowersave
available in KDE tray.

Wednesday, May 21, 2008

Viewing CD/DVD Image (.iso) File Contents

If you want to view contents of ISO images of CD/DVD
(say xyz.iso in your home directory), you can use mount command.

# mkdir /mnt/cd

# mount -o loop ~/xyz.iso /mnt/cd

Now you can go to /mnt/cd and view the contents of the iso file.
:)

Wednesday, May 14, 2008

Typing Error Correction at Command Line

Open your Bash Resource Config file

$ vim ~/.bashrc

...
alias vmi='vim'
alias mvi='vim'
...
$ mvi ~/.bashrc
will open .bashrc

Wednesday, May 7, 2008

Creation of Tar and Compressed file (Tar-ball) in one go

Suppose you want to create tar ball of directory xyz in your home directory.

$ cd ~

$ tar -zcvf xyz.tar.gz xyz/

or

$ tar -jcvf xyz.tar.bz2 xyz/

If you want to untar an unzip at one go, go to
the desired output directory.

$ cd ~

$ tar -zxvf xyz.tar.gz

or

$ tar -jxvf xyz.tar.bz2

Wednesday, April 23, 2008

Offline Package Repository in Debian

If you are not connected to internet, then still you can install packages
from Debian Installer DVDs. (Many thanks to my friend P. A. Venkatesh,
for inspiring me to use Debian).

1. Configuration for /etc/apt/sources.list. Just put these 3 lines in it.

deb file:///mnt/debian/disk1/ etch main contrib

deb file:///mnt/debian/disk2/ etch main contrib

deb file:///mnt/debian/disk3/ etch main contrib


2. Create directories.

# mkdir -p /mnt/debian/disk1
# mkdir /mnt/debian/disk2
# mkdir /mnt/debian/disk3

3. Mount *.iso (ISO images) file to to above directories.

# mount -o loop /debian-40r1-amd64-DVD-1.iso /mnt/debian/disk1
# mount -o loop /debian-40r1-amd64-DVD-2.iso /mnt/debian/disk2
# mount -o loop /debian-40r1-amd64-DVD-3.iso /mnt/debian/disk3

4. Now start synaptic package manager and click reload button. And Viola!
you will be able to see packages. Now you can install any package you want. :)

Wednesday, April 16, 2008

Group By Counts in a File (Generally csv files)

You can use following snippet to get GROUP BY - COUNT as in SQL
query. Like

If you want number of students in a department:

SELECT department, COUNT(*) FROM Student GROUP BY department;

Suppose that this file contains:

Mitesh Singh Jat,CSA
Mahesh Singh Sonal,CSA
Paneendra B A,SSA
Shrikant Joshi,SERC
Rupesh Bajaj,CSA
Chandrakant,SSA

1 #!/bin/bash
2
3 if [[ $# <> 4 ]]
4 then
5 echo "Usage: $0 file delim field_pos
6 exit
7 fi
8
9 CMD=`echo $1 | sed 's/.*\.gz/zcat/'`
10
11 if [[ $CMD != "zcat" ]]
12 then
13 CMD="cat"
14 fi
15
16 echo "========================"
17 echo "Buckets for $1"
18 echo "========================"
19 echo "Bucket_id Count"
20 echo "------------------------"
21 $CMD $1 | cut -d$2 -f$3 | awk '{sum[$1]++} END {for (x in sum) {out+=sum[x]; print x" \t"sum[x];} print "========================"; print "Total records : " out;}'
22 echo "========================"

$ ./group_by_count.sh Student , 2
========================
Buckets for Student
========================
Bucket_id Count
------------------------
SSA 2
SERC 1
CSA 3
========================
Total records: 6
========================


Friday, April 4, 2008

Prevent Yourself from Disaster

Edit your .bashrc file
$ vi ~/.bashrc

# prevent overwriting to existing file, while using redirection
set -o noclobber

# useful aliases
alias mv='mv -i'
alias cp='cp -i'
alias rm='rm -i'

Tuesday, April 1, 2008

Making Fedora 8 Usable

1. Make YUM faster by using faster and nearer repositories.
# yum install yum-fastestmirror

2. Installing Non-Free Softwares

a. Install livna repository for non-free softwares.
# rpm -Uvh http://rpm.livna.org/livna-release-8.rpm

b. Install most used audio-video support
# yum install xine-lib-extras.i386 amarok-extras-nonfree.i386 amarok-konqueror.i386 amarokFS.i386 moodbar.i386

c. Install Adobe flash player
# rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm
# yum -y install flash-plugin

d. Making Adobe Acrobat Reader 7 work on Fedora 8
# vi /usr/local/Adobe/Acrobat7.0/bin/acroread
Change line number 644 from
check_gtk_ver_and_set_lib_path "$MIN_GTK_VERSION" ### returns 0 if found gtk >= 2.4
to
#check_gtk_ver_and_set_lib_path "$MIN_GTK_VERSION" ### returns 0 if found gtk >= 2.4

PS. If you find any other good Non-free software, do let me know. Thanks in Advance.

Monday, March 31, 2008

Dynamic Swap on File

1. Create a 500MB swap file.

$ dd if=/dev/zero of=/mnt/WinD/Swap500M bs=1024 count=500000
$ mkswap /mnt/WinD/Swap500M



2. Change variables according to your configuration (or liking) in
the following Shell script (my_swapon.sh).

#!/bin/sh

## Turns on swap ##

SWAP_PARTITION="/mnt/WinD"
SWAP_DEV="$SWAP_PARTITION/Swap500M"
if [[ -e $SWAP_DEV ]]
then
echo "$SWAP_DEV is already mounted."
else
mount $SWAP_PARTITION
fi

IS_ON=`grep -c $SWAP_DEV /proc/swaps`

if [ $IS_ON -eq 1 ]
then
echo "$SWAP_DEV is already turned on."
else
swapon $SWAP_DEV
fi


3. Run this script as root user whenever you want to
use extra swap space. (Above steps 1 and 2, need
to be done only once).

$ sudo ./my_swapon.sh