Total visit on this blog

Monday 30 July 2012

compgen: An Awesome Command To List All Linux Commands

compgen is bash built-in command and it will show all available commands, aliases, and functions for you. The syntax is:
compgen option

compgen command examples

To list all the commands available to you, enter:
compgen -c
Sample outputs:
ls
if
then
else
elif
fi
....
mahjongg
sol
gtali
sl-h
gnobots2
gnotravex
iagno
fortune
gnect
gnome-sudoku
LS
glchess
gnuchess
gnuchessx

You can search or count the commands:
compgen -c | grep find
compgen -c | wc -l
echo "$USER user can run $(compgen -c | wc -l) commands on $HOSTNAME."
Sample outputs:
vivek user can run 3436 commands on wks01.

To list all the bash shell aliases available to you, enter:
compgen -a
Sample outputs:
..
...
....
.....
.4
.5
bc
cd..
chgrp
chmod
chown
cp
dnstop
egrep
ethtool
fastping
fgrep
grep
iftop
l.
ll
ln
ls
mcdflush
mcdshow
mcdstats
mount
mv
pscpu
pscpu10
psmem
psmem10
rm
tcpdump
update
updatey
vnstat
wget
which


Other options are as follows:
 
########################################
# Task: show all the bash built-ins
########################################
compgen -b
########################################
# Task: show all the bash keywords
########################################
compgen -k
########################################
# Task: show all the bash functions
########################################
compgen -A function

How To Masquerade On Linux (Internet Connection Sharing)

IP Masquerade is a networking function in Linux similar to the one-to-many (1:Many) NAT (Network Address Translation) servers found in many commercial firewalls and network routers. For example, if a Linux host is connected to the Internet via PPP, Ethernet, etc., the IP Masquerade feature allows other “internal” computers connected to this Linux box (via PPP, Ethernet, etc.) to also reach the Internet as well. Linux IP Masquerading allows for this functionality even though these internal machines don’t have an officially assigned IP address.
MASQ allows a set of machines to invisibly access the Internet via the MASQ gateway. To other machines on the Internet, the outgoing traffic will appear to be from the IP MASQ Linux server itself. In addition to the added functionality, IP Masquerade provides the foundation to create a HEAVILY secured networking environment. With a well built firewall, breaking the security of a well configured masquerading system and internal LAN should be considerably difficult to accomplish.
Follow the following steps for performing masquerade:

Pre-Requirement

Machine 1 which is connected to WLAN(or dial-up connection) and also connected to machine 2 via LAN cable.
Machine 2 which is connected to machine 1 Via Lan cable

Steps for Machine 1 (which is connected to internet using a wifi connection i.e wlan0 or dial-up connection)

1. Open System -> Administration -> Firewall, and under ‘Masquerading’ select wlan0 (scroll down, if it’s not there add it), then click ‘Apply’
2. Set eth0 to ip 192.168.1.2, either via Network Manager or from the command line with
Code:
ifconfig eth0 192.168.1.2/24
(Assuming your wifi connection is in 192.168.1.* range)

Machine 2  (Connected to Machine 1 via ethernet cable from eth0)

1. Stop NetworkManager service since it’s easier without:
Code:
service NetworkManager stop
2. Set eth0 ip address to  and default gateway to 192.168.0.2:
Code:
ifconfig eth0 192.168.0.2
route add default gw 192.168.1.2
Check you can ping 192.168.1.2.
3. Now just set a DNS address in /etc/resolv.conf
Code:
nameserver 8.8.8.8
or
nameserver in the resolve.conf of Machine 1
now check. you can ping google.com.

Now enjoy the internet sharing. :) 

Friday 13 July 2012

rsync over ssh

rsync is used to perform the backup operation in UNIX / Linux. rsync is a great tool for backing up and restoring files. rsync is basically a synchronous tools which is used synchronize the files and directories from one location to another in an effective way. rsync behaves much same as of rcp but it has many more options.

Important features of rsync

Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
Security: rsync allows encryption of data using ssh protocol during transfer.
Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
Privileges: No special privileges are required to install and execute rsync
Some of the additional features of rsync are:
  • support for copying links, devices, owners, groups and permissions
  • exclude and exclude-from options similar to GNU tar
  • a CVS exclude mode for ignoring the same files that CVS would ignore
  • can use any transparent remote shell, including rsh or ssh
  • does not require root privileges
  • pipelining of file transfers to minimize latency costs
  • support for anonymous or authenticated rsync servers (ideal for mirroring)

GENERAL

There are six different ways of using rsync. They are:
  • for copying local files. This is invoked when neither source nor destination path contains a : separator
  • for copying from the local machine to a remote machine using a remote shell program as the transport (such as rsh or ssh). This is invoked when the destination path contains a single : separator.
  • for copying from a remote machine to the local machine using a remote shell program. This is invoked when the source contains a : separator.
  • for copying from a remote rsync server to the local machine. This is invoked when the source path contains a :: separator or a rsync:// URL.
  • for copying from the local machine to a remote rsync server. This is invoked when the destination path contains a :: separator.
  • for listing files on a remote machine. This is done the same way as rsync transfers except that you leave off the local destination.
Note that in all cases (other than listing) at least one of the source and destination paths must be local.

Syntax

$ rsync options source destination
Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Examples:

sync example for backing up/copying from remote server to local Linux computer:
rsync -arv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
(/home/testuser/user01backup/ is a local Linux folder path)
Here is what the “-arv” option does:
a = archive – means it preserves permissions (owners, groups), times, symbolic links, and devices.
r = recursive – means it copies directories and sub directories
v = verbose – means that it prints on the screen what is being copied

rsync -rv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
This example will copy folders and sub-folder but will not preserve permissions, times and symbolic links during the transfer

sync -arv –exclude ‘logs’ user01@serve.example.com:/home/user01/ /Users/testuser/user01backup/
This example will copy everything (folders, sub-folders, etc), will preserver permissions, times, links, but will exclude the folder /home/user01/logs/ from being copied

Use of “/” at the end of path:
When using “/” at the end of source, rsync will copy the content of the last folder.
When not using “/” at the end of source, rsync will copy the last folder and the content of the folder.
When using “/” at the end of destination, rsync will paste the data inside the last folder.
When not using “/” at the end of destination, rsync will create a folder with the last destination folder name and paste the data inside that folder.

Thursday 12 July 2012

rsync – Great Backup Tool

sync is used to perform the backup operation in UNIX / Linux. rsync is a great tool for backing up and restoring files. rsync is basically a synchronous tools which is used synchronize the files and directories from one location to another in an effective way. rsync behaves much same as of rcp but it has many more options.

Important features of rsync

Speed: First time, rsync replicates the whole content between the source and destination directories. Next time, rsync transfers only the changed blocks or bytes to the destination location, which makes the transfer really fast.
Security: rsync allows encryption of data using ssh protocol during transfer.
Less Bandwidth: rsync uses compression and decompression of data block by block at the sending and receiving end respectively. So the bandwidth used by rsync will be always less compared to other file transfer protocols.
Privileges: No special privileges are required to install and execute rsync
Some of the additional features of rsync are:
  • support for copying links, devices, owners, groups and permissions
  • exclude and exclude-from options similar to GNU tar
  • a CVS exclude mode for ignoring the same files that CVS would ignore
  • can use any transparent remote shell, including rsh or ssh
  • does not require root privileges
  • pipelining of file transfers to minimize latency costs
  • support for anonymous or authenticated rsync servers (ideal for mirroring)

GENERAL

There are six different ways of using rsync. They are:
  • for copying local files. This is invoked when neither source nor destination path contains a : separator
  • for copying from the local machine to a remote machine using a remote shell program as the transport (such as rsh or ssh). This is invoked when the destination path contains a single : separator.
  • for copying from a remote machine to the local machine using a remote shell program. This is invoked when the source contains a : separator.
  • for copying from a remote rsync server to the local machine. This is invoked when the source path contains a :: separator or a rsync:// URL.
  • for copying from the local machine to a remote rsync server. This is invoked when the destination path contains a :: separator.
  • for listing files on a remote machine. This is done the same way as rsync transfers except that you leave off the local destination.
Note that in all cases (other than listing) at least one of the source and destination paths must be local.

Syntax

$ rsync options source destination
Source and destination could be either local or remote. In case of remote, specify the login name, remote server name and location.

Examples:

sync example for backing up/copying from remote server to local Linux computer:
rsync -arv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
(/home/testuser/user01backup/ is a local Linux folder path)
Here is what the “-arv” option does:
a = archive – means it preserves permissions (owners, groups), times, symbolic links, and devices.
r = recursive – means it copies directories and sub directories
v = verbose – means that it prints on the screen what is being copied

rsync -rv user01@server.example.com:/home/user01/ /home/testuser/user01backup/
This example will copy folders and sub-folder but will not preserve permissions, times and symbolic links during the transfer

sync -arv –exclude ‘logs’ user01@serve.example.com:/home/user01/ /Users/testuser/user01backup/
This example will copy everything (folders, sub-folders, etc), will preserver permissions, times, links, but will exclude the folder /home/user01/logs/ from being copied

Use of “/” at the end of path:
When using “/” at the end of source, rsync will copy the content of the last folder.
When not using “/” at the end of source, rsync will copy the last folder and the content of the folder.
When using “/” at the end of destination, rsync will paste the data inside the last folder.
When not using “/” at the end of destination, rsync will create a folder with the last destination folder name and paste the data inside that folder.