Wednesday, September 30, 2009

How to Change MAC Address

Changing MAC address of a machine is called spoofing a MAC address or faking a MAC address. In linux, you can change MAC address of your machine. This is how it is done.

First find the physical MAC address of your machine by running the following command :

$ ifconfig -a | grep HWaddr
eth0 Link encap:Ethernet HWaddr 00:1f:f3:cc:c2:f9

The hexadecimal numbers in blue denote my machine's MAC address. Yours will be different.

Next, login as root in Linux and enter the following commands -



# ifconfig eth0 down
# ifconfig eth0 hw ether 00:11:22:33:44:55
# ifconfig eth0 up
# ifconfig eth0 | grep HWaddr


Note above that I have changed the MAC address to a different number highlighted in blue. 00:11:22:33:44:55 is the new MAC address I have provided for my Linux machine. You can choose any 48 bits hexadecimal address as your MAC address.

Thursday, September 10, 2009

Disabling Macbook Pro Touchpad

We may want to disable Macbook Pro (or any other Laptop) Touchpad, such cases are:
(i) We have connected a USB mouse, so we do not want to use touchpad for now.
(ii) While typing, there is no use of touchpad, if touchpad is too sensitive, the cursor keeps on jumping due to slight touch of palm or finger.

I have written a script to disable/enable touchpad (which is using synaptics driver).





#!/bin/bash

## Disable touchpad if USB Mouse is attached

SYNAPTICS=`which synclient`

if [[ "$SYNAPTICS" == "" ]]
then
echo "$0: please install synaptics touchpad driver."
echo "Also make sure that 'Option \"SHMConfig\" \"on\"'"
echo " is added in Touchpad device Section in /etc/X11/xorg.conf"
exit
fi

USB_mouse_present=`grep -ic "usb.*mouse" /proc/bus/input/devices`
# if no USB Mouse; enable touchpad
if [ $USB_mouse_present -eq 0 ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi

# if any parameter [on|off] is given, override previous command
if [ $# -ge 1 ]
then
if [ "$1" = "on" ]
then
$SYNAPTICS TouchpadOff=0
else
$SYNAPTICS TouchpadOff=1
fi
fi

exit 0



Sample Run:
Turn on touchpad

$ ./touchpad.sh on

Turn off touchpad

$ ./touchpad.sh off

If we have plugged USB mouse, then just give following command

$ ./touchpad.sh

On removing USB mouse, give following, the touchpad will be enabled automatically. :)

$ ./touchpad.sh


PS: The configuration for synaptics driver can be referred from here or here.

Friday, September 4, 2009

IP Masquerade and Network Address Translation (NAT)

If we want to connect multiple computers to the Internet using single public IP Address, Masquerading (A form of NATing) helps us.

NAT describes the process of modifying the network addresses contained with datagram headers while they are in transit. IP masquerade is the name given to one type of network address translation that allows all of the hosts on a private network to use the Internet at the price of a single IP address.

IP masquerading allows you to use a private (reserved) IP network address on your LAN and have your Linux-based router perform some clever, real-time translation of IP addresses and ports. When it receives a datagram from a computer on the LAN, it takes note of the type of datagram it is, “TCP,” “UDP,” “ICMP,” etc., and modifies the datagram so that it looks like it was generated by the router machine itself (and remembers that it has done so). It then transmits the datagram onto the Internet with its single connected IP address. When the destination host receives this datagram, it believes the datagram has come from the routing host and sends any reply datagrams back to that address. When the Linux masquerade router receives a datagram from its Internet connection, it looks in its table of established masqueraded connections to see if this datagram actually belongs to a computer on the LAN, and if it does, it reverses the modification it did on the forward path and transmits the datagram to the LAN computer.

I have written a shell script, which converts a Linux box into a router. The script is written as:




#!/bin/bash

## Output interface: connected to Internet
out_iface=ppp0

## Run as root always
user_id=`whoami`

if [[ "$user_id" != "root" ]]
then
echo "$0: please run this script as root user."
exit
fi

## Checking existance of iptables
IPTABLES=`which iptables`

if [[ "$IPTABLES" == "" ]]
then
echo "$0: please install iptables."
exit
fi

if [ $# -ge 1 ]
then
case "$1" in
status)
$IPTABLES -t nat -L
exit 0
;;
stop)
## Disabling Packet forwarding in kernel
echo 0 > /proc/sys/net/ipv4/ip_forward
echo "Flushing NAT MASQUERADE Entries"
$IPTABLES -t nat -F
exit 0
;;
restart)
$0 stop
if [ $# -ge 2 ]
then
$0 start $2
else
$0 start
fi
;;
start)
if [ $# -ge 2 ]
then
out_iface=$2
fi
## Enabling Packet forwarding in kernel
echo 1 > /proc/sys/net/ipv4/ip_forward

## Enabling NAT Masquerade, if not enabled
if [ -z "`$IPTABLES -t nat -L | grep MASQUERADE`" ]
then

$IPTABLES -t nat -A POSTROUTING -o $out_iface -j MASQUERADE
fi
;;
*)
echo "USAGE: $0 <start|status|restart|stop> [internet_interface]"
exit 1
;;
esac
else
echo "USAGE: $0 <start|status|restart|stop> [internet_interface]"
exit 1
fi

exit 0



Sample Runs:

$ ./NAT_Masquerade.sh
./NAT_Masquerade.sh: please run this script as root user.
$ sudo ./NAT_Masquerade.sh
USAGE: ./NAT_Masquerade.sh [internet_interface]
$
Here, internet_interface is the interface which is connected to internet.
By default, ppp0 (Dial up) interface is taken.

$ sudo ./NAT_Masquerade.sh status
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$
Since Masquerade is not yet applied, Chain POSTROUTING rule is empty.

Applying IP Masquerade to internet_interface eth0.
$ sudo ./NAT_Masquerade.sh start eth0
$ sudo ./NAT_Masquerade.sh status
Chain PREROUTING (policy ACCEPT)
target prot opt source destination

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
$ sudo ./NAT_Masquerade.sh stop
Flushing NAT MASQUERADE Entries
$

Monday, August 31, 2009

Fixing Wired Network (eth0) Ethernet

While using Debian based systems(like Ubuntu), I often found that network manager just got into the way whenever I wanted to fix an internet connection. So, first thing to do is to kill the associated processes (kill -9) after you find the process ID by using ps. Or you can totally get rid of these packages (usually network-manager, network-manager-gnome in GNOME or knetworkmanager in KDE).

Then, edit /etc/network/interfaces file.

$ sudo vi /etc/network/interface

auto eth0

iface eth0 inet dhcp

again assuming that we want to have a DHCP based connection.

Then restart the network connection by issuing the following command:

$ sudo /etc/init.d/networking restart

Friday, August 14, 2009

Determining Bits of CPU and OS (32/64-bits)

In order to determine whether OS/CPU is 32-bit or 64-bit,
you can use this shell script.


#!/bin/bash
echo -n "Running "

RES=`uname -a | grep 64`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo -n "operating system on a "

RES=`cat /proc/cpuinfo | grep " lm "`
if [ $? -eq 0 ]; then

echo -n "64-bit "
else
echo -n "32-bit "
fi
echo "machine"




Sample Run:

$ ./os_cpu.sh
Running 64-bit operating system on a 64-bit machine

Wednesday, August 5, 2009

Let Linux Speak Time and Day for You

I have written a single line command that tells time of the day:

$ espeak "Time is `/bin/date \"+%H
hours %M minutes %S seconds\"`"




You need not to type this command every time, just run the

following commands only one time:


$ echo "alias speak_time='espeak \"Time is \`/bin/date \"+%H hours %M minutes %S seconds\"\`\"'" >> ~/.bashrc

$ source ~/.bashrc




Now, you will be able to run speak_time any number of time at any time, and you will get exact time. :-)


$ speak_time



PS: Requirement of above command is "espeak"


$ sudo apt-get install espeak


For speaking day, just add following alias in your .bashrc file

alias speak_day='espeak "Today is `/bin/date \"+%A, %d %B 20%y\"`"'

Monday, July 27, 2009

Mount Windows Shared Folder onto Linux Directory

Suppose you want to mount Windows shared folder (say shared_folder) on windows machine (IP address 10.226.194.169) on a folder (say /local/smbshare/shared_folder), you just need to give following
command:


$ sudo mount -t smbfs -o username=domainname
\\yourname,password=yoursecretpassword -o gid=users,dmask=777,fmask=777,rw //10.226.194.169/shared_folder /local/smbshare/shared_folder

You can verify whether the above command is successful or not, by using:
$ mount | grep shared_folder

If the above command shows some output, it means mount is successful :)