Showing posts with label Basic Linux. Show all posts
Showing posts with label Basic Linux. Show all posts

Wednesday, 4 May 2016

Multiple IP configuration on multiple NIC on same server


#  First Set IP For Both LAN Card


In short, there are two options:
1) edit the file "/etc/sysctl.conf"

change the value from 1 to 2 in the following line:

net.ipv4.conf.default.rp_filter = 1

Add this line:

net.ipv4.conf.all.rp_filter = 2
After edit, it should look like this:
net.ipv4.conf.default.rp_filter = 2
net.ipv4.conf.all.rp_filter = 2
reload the configuration by typing
sysctl -p 

Wednesday, 27 April 2016

Add Swap Space Using dd, mkswap and swapon on Linux



Linux: 2 Ways to Add Swap Space Using dd, mkswap and swapon
===========================================================


Question: I would like to add more swap space to my Linux system. Can you explain with clear examples on how to increase the swap space?

Answer: You can either use a dedicated hard drive partition to add new swap space, or create a swap file on an existing filesystem and use it as swap space.


1. First check system RAM, how much?

free -tm
free -tg
free -g

Swapon command with option -s, displays the current swap space in KB.
# swapon -s


Swapon -s, is same as the following.
# cat /proc/swaps

Output:
# cat /proc/swaps
Filename                        Type            Size    Used    Priority
/dev/sda2                       partition       4192956 0       -1

===========================================================================================



Method 1: Use a Hard Drive Partition for Additional Swap Space:
===============================================================

If you have an additional hard disk, (or space available in an existing disk), create a partition using fdisk command. Let us assume that this partition is called

/dev/sdc1

Now setup this newly created partition as swap area using the mkswap command as shown below.

# mkswap /dev/sdc1

Enable the swap partition for usage using swapon command as shown below.
# swapon /dev/sdc1

To make this swap space partition available even after the reboot, add the following line to the /etc/fstab file.
# cat /etc/fstab
/dev/sdc1               swap                    swap    defaults        0 0


Verify whether the newly created swap area is available for your use.

# swapon -s

# free -tm
# free -g


Note: In the output of swapon -s command, the Type column will say “partition” if the swap space is created from a disk partition.

==================================================================================================================================

Method 2: Use a File for Additional Swap Space:
===============================================

If you don’t have any additional disks, you can create a file somewhere on your filesystem, and use that file for swap space.

The following dd command example creates a swap file with the name “myswapfile” under /root directory with a size of 1024MB (1GB).


# dd if=/dev/zero of=/root/swapmemory bs=1M count=5120                   <-------  we can increase ram size here instead of 1024=1GB, 2048=2GB, etc......

1024+0 records in
1024+0 records out

# ls -l /root/swapmemory
-rw-r--r--    1 root     root     1073741824 Aug 14 23:47 /root/myswapfile


Change the permission of the swap file so that only root can access it.

# chmod 600 /root/swapmemory

Make this file as a swap file using mkswap command.

# mkswap /root/swapmemory
Setting up swapspace version 1, size = 1073737 kB


Enable the newly created swapfile.

# swapon /root/swapmemory

To make this swap file available as a swap area even after the reboot, add the following line to the /etc/fstab file.

# cat /etc/fstab

/root/swapmemory               swap                    swap    defaults        0 0

:wq

Verify whether the newly created swap area is available for your use.


# swapon -s
Filename                        Type            Size    Used    Priority
/dev/sda2                       partition       4192956 0       -1
/root/swapmemory                file            1048568 0       -2


# free -tm
# free -g

Note: In the output of swapon -s command, the Type column will say “file” if the swap space is created from a swap file.

If you don’t want to reboot to verify whether the system takes all the swap space mentioned in the /etc/fstab, you can do the following, which will disable and enable all the swap partition mentioned in the /etc/fstab

# swapoff -a

# swapon -a



Ref:
http://www.thegeekstuff.com/2010/08/how-to-add-swap-space/
===========================================================



######  How To Test RAM By Using Script ##################
==========================================================

Now, we need to test whether added swam memory working or not without rebooting server.



How to produce high CPU load, memory, I/O or stress test Linux server


2 Memory Regression Perl Scripts for Linux:
===========================================


During performance testing of your application you might want to perform some sort of memory regression testing. This article contains two memory regression scripts that are written in perl which will occupy a specific amount of memory for a certain amount of time, for your testing.

1. Basic Memory Regression Script

Note: Don’t execute this script on any critical system. Be careful while running this script. Don’t give a large memory value to this script. If the amount of memory given is huge or not available, your system might hang.

# vim memtest.pl

# store and validate the command line parameter
$mb = $ARGV[0];
unless ( defined $mb and $mb =~ /^\d+$/ and $mb >= 1)  {
    die "Usage: $0 <occupy MB>\nEx: $0 100 - occupies 100 MB memory\n"
}
# convert it to bytes.
$b = $mb * 1024 * 1024;

# open in-memory file, and seek to size specified to get memory from OS.
open MEM, '>', \$memfile;
seek MEM, $b - 1, 0;
print MEM 'A';
close MEM;
printf "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;

# till here the memory is occupied by this program.
undef $memfile;
printf "Memory released";

:wq


To execute this script, do the following:

# perl memtest.pl 100               <-------   we can add ram load here 200MB=200, 300MB=300 

2. Advanced Memory Regression Script:
=====================================

This memtest-adv.pl works the same way as basic script, but you can also specify percentage as an input. When your system has total of 2GB of physical memory, you can specify 25%, which will occupy 500MB of memory for testing.

First argument can be either the amount of memory in MB or percentage of memory to be occupied, where percentage represents the percentage against total primary memory available.

Note: This Perl script occupies approximately the given amount of memory. On the system we tested, it took a maximum of 5MB more. So, do not use this on any critical system. Use this script only on a test system, as the system might hang.

Do ‘vi memtest-adv.pl’ and copy/paste the following perl code to create this file.

# vim memtest-adv.pl

# calculate memory to be occupied from percentage given
sub find_memto_occupy
{
    $pc = $_[0];
    die "Wrong percentage given $pc\n" if ($pc > 100);

    open MEMINFO, '<', '/proc/meminfo' or die "Unable to open /proc/meminfo to find available memory\n";
    my $mem = <MEMINFO>;

    if ( $mem =~ /^MemTotal:\s+(\d+)\s.*$/ )  {
        $mem = $1;
    } else {
        die "Unable to find the available memory\n";
    }

    $mem = ( $mem / 100 ) * $pc;
    return int($mem / 1024);
}

# main script
{
    $num = $ARGV[0];
    unless ( defined $num and $num =~ /^\d+%?$/ and $num >= 1)  {
             die "Usage: $0 <occupy MB>\nEx: $0 100 - occupies 100 MB memory\n"
    }

    if ( $num =~ /^(\d+)%$/ )  {
        # convert percentage to bytes.
        $pc = $1;
        $mb = find_memto_occupy($pc);
    } else {
        $mb = $num;
    }

    $b = $mb * 1024 * 1024;
    open MEM, '>', \$memfile;
    seek MEM, $b - 1, 0;
    print MEM chr(0);
    close MEM;

    print "$mb MB memory is occupied, press ENTER to release: "; <STDIN>;
    undef $memfile;
    print "Memory released";
}

:wq


To execute this script, do the following:

# perl memtest-adv.pl 250                      <------  we can specify ram here = 250MB,300MB,... 600MB etc.....!!

(or)

# perl memtest-adv.pl 25%



Ref:
www.thegeekstuff.com/2011/11/2-memory-regression-perl-scripts-for-linux/
   

Thursday, 14 April 2016

Tuesday, 12 April 2016

How To Enable Direct SSH To EC2 Linux Instance Without Keypair File


To enable the direct ssh to linux EC2 instance without using keypair file we have to edit the sshd_config file.

Follow the given below steps

Step 1 : Login into linux EC2 instance with the help of keypair. Use the user name which is related to your instance. for eg

     example:

    ssh -i keypar_file.pem ubuntu@ec2-1.1.1.1.compute-1.amazonaws.com


Step 2: After login ,edit the /etc/ssh/sshd_config file and make the value of parameter called PasswordAuthentication as yes

    vi /etc/ssh/sshd_config

    change the PasswordAuthentication value as yes

    PasswordAuthentication yes

    :wq
    save and exit

Step 3: Restart the ssh service.

    In Debian or Ubuntu instance:

    /etc/init.d/ssh restart

    In CentOS or Red Hat instance:

    /etc/init.d/sshd restart

Step 4: Open new terminal in your PC or laptop. ( Do not take the risk :),playing safe side )

Now try direct ssh without using keypair file.

for eg.

    ssh ubuntu@ec2-1.1.1.1.compute-1.amazonaws.com

Done. :)



Ref:

http://sharadchhetri.com/2013/03/29/how-to-enable-direct-ssh-to-ec2-linux-instance-without-keypair-file/

Sunday, 13 March 2016

Rsync command to sync files from one server to another server


server1 and server2

From Server 1 :


cat /bin/datacopy


#awsmomsweb3 - server1

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/womlive ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/application ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/js ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/css ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/cron ec2-user@172.31.10.90:/var/www/html/


/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/images ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/cmsuat ec2-user@172.31.10.90:/var/www/html/


/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/system ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/*.php ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/.htaccess ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/*.xml ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/*.html ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/*.txt ec2-user@172.31.10.90:/var/www/html/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html/hospitals ec2-user@172.31.10.90:/var/www/html


/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html ec2-user@54.169.82.120:/var/www/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html ec2-user@52.74.59.135:/var/www/


/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html ec2-user@52.74.65.37:/var/www/

/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" /var/www/html ec2-user@52.74.34.40:/var/www/



:wq


# chmod +x /bin/datacopy

# datacopy


/usr/bin/rsync -avz --delete -e "ssh -i /home/ec2-user/.ssh/serverkey.pem" ec2-user@54.255.146.142:/var/www/html/ /var/www/html/

Saturday, 12 March 2016

Install Canon Printer On Linux


Install CANON Printer On Linux :

1. Download Printer Drivers with proper architecture.
2. Drivers Name :  cndrvcups-capt_2.70-1_i386 & cndrvcups-common_3.20-1_i386

Package Installation Commands:

dpkg -i package-name-here.deb
dpkg -i package-name-here.deb


3. Then Fire Below Commands Step By Steps:

# sudo /usr/sbin/lpadmin -p LBP2900 -m CNCUPSLBP2900CAPTK.ppd -v ccp://localhost:59787 -E
# sudo /usr/sbin/ccpdadmin -p LBP2900 -o /dev/usb/lp0
# sudo service ccpd start
# sudo service ccpd status
# captstatusui -P LBP2900


Finished........................!!!

Now If you want to set on boot script while machine reboot. Please do set cron jobs for it as below.


On home create one script as below :-

# vim /home/administrator/Desktop/printer.sh

sudo /usr/sbin/lpadmin -p LBP2900 -m CNCUPSLBP2900CAPTK.ppd -v ccp://localhost:59787 -E
sudo /usr/sbin/ccpdadmin -p LBP2900 -o /dev/usb/lp0
sudo service ccpd start
sudo service ccpd status
captstatusui -P LBP2900

:wq


# chmod +x printer.sh

# crontab -e

@reboot /bin/sh /home/administrator/Desktop/printer.sh >> /home/administrator/Desktop/printer.txt

:wq


and finally reboot machine and test printer....!!

===================================
===================================



Install
1. cndrvcups-common_3.20-1_i386.ppd
2. cndrvcups-capt_2.70-1_i386.ppd
1) Create the following directories/file if they are missing:
sudo mkdir /var/ccpd
sudo mkfifo /var/ccpd/fifo0
sudo mkdir /var/captmon
2) Register the printer:
sudo /usr/sbin/lpadmin -p LBP2900 -m CNCUPSLBP2900CAPTK.ppd -v
ccp:/var/ccpd/fifo0 -E
3) Register the printer with ccpd daemon:
sudo /usr/sbin/ccpdadmin -p LBP2900 -o /dev/usb/lp0
Auto Starting
1. sudo gedit /etc/udev/rules.d/85-canon-capt.rules
Put inside :
KERNEL=="lp*", SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="04a9",
RUN+="/etc/init.d/ccpd start"
KERNEL=="lp*", SUBSYSTEM=="usb", ACTION=="remove", RUN+="/etc/init.d/ccpd stop"
3) Create an Upstart Job :
Create the file:
sudo gedit /etc/init/ccpd-restart.conf
Put inside:
# ccpd-restart - if printer is ON before PC.
description "restart daemon ccpd for Canon printer LBP-serie"
start on started cups
stop on runlevel [016]
script
if [ -e /dev/usb/lp* ]; then
/etc/init.d/ccpd stop
sleep 3
/etc/init.d/ccpd start
fi
end script
Restart System and check.
If still printing issue:
cd /dev/usb/
ln -s lp1 lp0
captstatusui -P LBP2900


Enjoyeed ..................!!!!!

Create Server Login Name Aliase


Put ssh keys under /root/.ssh/

vim .bashrc


alias serverftp='ssh -i /root/.ssh/ftpserverkey ec2-user@10.20.30.10'

alias nfsserver='ssh -i /root/.ssh/nfsserverkey  ec2-user@10.20.30.11'


Password Less Login SSH


1. Create private and public keys on source server.

    [root@source~]# ssh-keygen
       
        Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/mykey <ENTER>
        Created directory '/root/.ssh'
        Enter passphrase (empty for no passphrase): <ENTER>
        Enter same passphrase again: <ENTER>

2. Copy public key to remotehost.

    [root@source~]# ssh-copy-id -i /root/.ssh/mykey.pub root@192.168.0.100
    root@192.168.0.100's password: <PASSWORD FOR REMOTEHOST>

    This command will copy contents of 'mykey.pub' file and appends into file "/root/.ssh/authorized_keys" on remotehost 192.168.0.100. This command also creates structure "/root/.ssh/authorized_keys" if not exists on remote host.

3. Login to remotehost.
   
    [root@source~]# ssh root@192.168.0.100
    [root@destination~]#

Friday, 11 March 2016

Setup IP On Ubuntu

root@server:~$]  cat /etc/network/interfaces



First Check which port is active :

# ip link show

p2p1 is active


# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto p2p1
iface p2p1 inet static
    address 192.168.0.121
    netmask 255.255.255.0
    network 192.168.0.0
    broadcast 192.168.0.255
    gateway 192.168.0.1
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers 10.20.30.80 8.8.8.8
    dns-search example.com

:wq

/etc/init.d/networking restart

Saturday, 5 March 2016

Set Date & Time CentOS

#!/bin/bash

# A script to Set up the date and time

service ntpd stop
ntpdate us.pool.ntp.org
hwclock -w

mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime

service ntpd start
service mysqld restart

OR

#!/bin/bash

# A script to Set up the date and time

mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime
service mysqld restart


@reboot /bin/sh /home/ec2-user/set-time.sh >> /home/ec2-user/set-time-log




Wednesday, 2 March 2016

Chane Date & Time CentOS 6.5


 want to set timezone as from UTC to  IST. Use below two commands.



First check date on server :

# date

01. Remove the localtime file.
]# rm -rf /etc/localtime

# ln -s /usr/share/zoneinfo/Asia/Kolkata /etc/localtime

# date

To change time on server :

# date
# date -s "2016 Jan 30 09:10:40"


## To change timezone in MySQL :

2. Check time_zone* tables in mysql db. if they are empty fill them using following command

# mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

Enter mysql password:
some file will skipeed...  ignore it

3. Again login to mysql:

mysql -u root -p
mysql> select now();

mysql> SET GLOBAL time_zone = "Asia/Calcutta";


OR
mysql> SET GLOBAL time_zone = "Asia/Kolkata";
mysql> select now();
mysql> \q


Ref: http://sandipmore.blogspot.in/2010/03/mysql-timezone-setting-for-ist.html