Wednesday, 25 December 2013

Quickest way to setup - KEY-BASED AUTHENTICATION: LINUX

For example, we gonna setup key-based authentication from server 192.168.x.y to 192.168.x.z
( we will use this auth for root uesr )

1). Create SSH-Kegen Keys on – 192.168.x.y

[root@192.168.x.y~] # ssh-keygen -t rsa

2). Create .ssh Directory on – 192.168.x.z

# ssh root@192.168.x.z mkdir -p .ssh
The authenticity of host '192.168.x.z (192.168.x.z)' can't be established.
RSA key fingerprint is 3x:x7:a4:e5:af:89:c5:dx:b1:3c:9d:xx:66:47:03:xx.
Are you sure you want to continue connecting (yes/no)?  "press yes"

3). Upload Generated Public Keys to – 192.168.x.z

# cat .ssh/id_rsa.pub | ssh root@192.168.x.z 'cat >> .ssh/authorized_keys'

4). Set Permissions on – 192.168.x.z

# ssh root@192.168.x.z "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"

5). Now you can login 192.168.x.z without password:

[root@192.168.x.y~] # ssh root@192.168.x.z
Last login: xxxxxxxxxxxxxxxxxxxxxx from 'last login ip here'
[root@192.168.x.z ~]#

Done !!!

Backup and Restore of LINUX System Disk using "dd" command:

Backup and Restore of LINUX System Disk using "dd" command:

creating disk1:

# dd if=/dev/zero of=disk1 bs=1024000 count=2048

# mkdir d1

# mkfs.ext3 disk1

# mount disk1 d1 // It will end with error, so try,

# mount -o loop disk1 d1

creating disk2:

# dd if=/dev/zero of=disk2 bs=1024000 count=2048

#mkfs.ext3 disk2

# mkdir d2

# mount -o loop disk2 d2

# df -h -----  result will be,

We can also use conv=notrunc,noerror options with "dd" command ,

- The notrunc conversion option means do not truncate the output file — that is, if the output file already exists, just replace the specified bytes and leave the rest of the output file alone.

- Noerror means to keep going if there is an error. Dd normally terminates on any I/O error.

to confirm this,

# losetup -a
/dev/loop0: [fd00]:142387 (/root/disk1)
/dev/loop1: [fd00]:142390 (/root/disk2)

copy some file into d1 for example:

# wget http://ipv4.download.thinkbroadband.com/512MB.zip

shows like:

# ll /root/d1
total 541048
-rw-r--r--   1 root root  16589672 Dec 23 22:14 1GB.zip
-rw-r--r--   1 root root 536870912 May 30  2008 512MB.zip
drwxr-xr-x. 69 root root      4096 Dec 23 22:06 etc
drwx------   2 root root     16384 Dec 23 21:58 lost+found

Making image for /root/disk1:

Now it creates the image of /root/disk1.

[root@ranjith ~]# dd if=/root/disk1 of=~/d1backup.img
dd: writing to `/root/d1backup.img': No space left on device
3562841+0 records in
3562840+0 records out
1824174080 bytes (1.8 GB) copied, 47.1943 s, 38.7 MB/s

# pwd
/root/d2
# ls
lost+found

Restore the image on another partition:

# dd if=d1backup.img of=/root/disk2
3562840+0 records in
3562840+0 records out
1824174080 bytes (1.8 GB) copied, 46.6134 s, 39.1 MB/s

The above command will restore the image (d1backup.img) of /root/disk1 to /root/disk2.

# ll /root/d2
total 541048
-rw-r--r--   1 root root  16589672 Dec 23 22:14 1GB.zip
-rw-r--r--   1 root root 536870912 May 30  2008 512MB.zip
drwxr-xr-x. 69 root root      4096 Dec 23 22:06 etc
drwx------   2 root root     16384 Dec 23 22:05 lost+found

SHELL SCRIPT to generate random PASSWORD and CAPTCHA

1). create a file named "password.sh"
 
#!/bin/bash
while :
do
    clear
    cat<<EOF
                ===================
                PASSWORD GENERATOR:
                -------------------
            Enter the (P)assword length
            Enter the (C)aptcha length
                      (Q)uit
                -------------------
EOF
    read -n1 -s
    case "$REPLY" in
    "E")
    echo -e -n "\n\t: "
    read b
    a=$(tr -dc "A-Za-z0-9~!@#$%^&*-_" < /dev/urandom | head -c$b)
    echo -e "\n\n\t\t$a"
    ;;
    "C")
    echo -e -n "\n\t: "
    read b
    echo -e "\n\n\t\t" `/usr/bin/shuf -i 1-$b -z`
    ;;
    "Q")  exit 0                    ;;
    "q")  echo "case sensitive!!"   ;;
    "c")  echo "case sensitive!!"   ;;
    "e")  echo "case sensitive!!"   ;;
    esac
    sleep 2
done

2). copy & paste the below code in file: password.sh

3). chmod 755  password.sh

4). ./password.sh
output:



for PASSWORD:



 for CAPTCHA:



Press Q (not "q") to exit.

=========================
Simple script for only PASSWORD:
=========================

 #!/bin/bash
b=$1
if [ $# -gt 1 ] || [ $# -eq 0 ]
then
echo "Enter the password length:"
read b
a=$(tr -dc "A-Za-z0-9~!@#$%^&*-_" < /dev/urandom | head -c$b)
echo "$a"
else
a=$(tr -dc "A-Za-z0-9~!@#$%^&*-_" < /dev/urandom | head -c$b)
echo "$a"
fi
=========================