Thursday, May 7, 2015

Chain Operators in Linux

1. Ampersand  (&)
   With the help of Ampersand we can run any linux command in background and go and grab 
   coffee.

   abhishek@myfedora:~$ ping ­c5 www.abhishekamralkar.in &

   Run two command in background, simultaneously:

   abhishek@myfedora:/home/abhiz# yum update -y & service httpd status &

2. semi-colon (;)
   With the help of semi-colon we can run, several linux commands in a single go and the 
   execution of command occurs sequentially.

   abhishek@myfedora:/home/abhishek# yum update -y ; service httpd status ; mkdir test

   The above command combination will first execute update instruction, then httpd service 
   status instruction and finally will create a ‘test‘ directory under the current working directory.

3. AND (&&)
   With the help of AND Operator (&&) we can execute the second command only, if the 
   execution of first command success. This command is very useful in checking the execution 
   status of last command.

   For example, I want to visit website www.abhishekamralkar.in using lynx command, in 
   terminal but before that I need to check if the host is live or not.

   abhishek@myfedora:/home/abhishek# ping -c3 www.abhishekamralkar.in && lynx 
   www.abhishekamralkar.in

4. OR (||)
   With the help of OR Operator (||) we can execute second command only if the execution of 
   first command fails, i.e., the exit status of first command is ‘1‘.

   For example, I want to execute ‘yum update -y‘ from non-root account and if the first 
   command fails, then the second ‘lynx www.abhishekamralkar.in‘ command will execute.

   abhishek@myfedora:~$ yum update -y || lynx www.abhishekamralkar.in
   
5. NOT (!)
   With the help of NOT Operator we can execute all commands command except the condition 
   provided. To understand this, create a directory abhishek in your home directory and ‘cd‘ to it.

   abhishek@myfedora:~$ mkdir abhishek  
   abhishek@myfedora:~$ cd abhishek
   Next, create several types of files in the folder abhishek.

   abhishek@myfedora:~/abhishek$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html
   See we’ve created all the new files within the folder abhishek.

   abhishek@myfedora:~/abhishek$ ls 

   a.doc  a.html  a.pdf  a.xml  b.doc  b.html  b.pdf  b.xml
   Now delete all the files except ‘html‘ file all at once, in a smart way.

   abhishek@myfedora:~/abhishek$ rm -r !(*.html)
   Just to verify, last execution. List all of the available files using ls command.

   abhishek@myfedora:~/abhishek$ ls 

   a.html  b.html
6. AND – OR operator (&& – ||)
    Its a combination of ‘AND‘ and ‘OR‘ Operator. 

   For example, let’s do ping to www.abhishekamralkar.in, if success echo ‘Verified‘ else echo 
   ‘Host Down‘.

   abhishek@myfedora:~/abhishek$ ping -c3 www.abhishekamralkar.in && echo "Verified" || 
   echo "Host Down"

7. PIPE (|)
   With PIPE output of first command acts as an input to the second command. For example, 
   pipeline the output of ‘ls -l‘ to ‘less‘ and see the output of the command.

   abhishek@myfedora:~$ ls -l | less

8. Command Combination {} 
    Combine two or more commands, the second command depends upon the execution of the 
    first command.

   For example, check if a file ‘linux.txt‘ and ‘unix.txt‘ is available under my Downloads directory 
   or not, and output corresponding output.

   abhishek@myfedora:~$ [ -f /home/abhishek/Downloads/linux.txt ] || echo “The file does not 
   exist”
   abhishek@myfedora:~$ [ -f /home/abhishek/Downloads/unix.txt ] || echo “The file does not 
   exist” 

   “The file does not exist”

9. Precedence ()
   The Operator makes it possible to execute command in precedence order.

   Command1 && Command2 || Command3 && Command4.

   In the above pseudo command, what if the Command1 fails? Neither of the Command2, 
   Command3, Command4 would executed, for this we use Precedence Operator, as:

   (Command1 && Command2) || (Command3 && Command4)
  
   In the above pseudo command, if Command1 fails, Command2 also fails but Still Command3 
   and Command4 executes depends upon exit status of Command3.

10. Concatenation (\)
      With the help of  Concatenation Operator (\) as the name specifies, is used to concatenate 
      large commands over several lines in the shell. For example, The below command will open       text file test(1).txt.

      abhishek@myfedora:~/Downloads$ nano test\(1\).txt

Wednesday, April 22, 2015

Percona Toolkit

Percona is an open source software company specializing in MySQL Support, Consulting, Managed Services, and Training. 
We use Percona MySQL for our production database and we written wrapper over Percona Xtrabackup utility for hotbackups(Full + Differential backups with slave position capture) for production database backup.
Recently we started using Percona Toolkit and we found it useful for MySQL administration.
How to instal it ?
wget  Percona.com/get/Percona-toolkit.tar.gz
wget  Percona.com/get/Percona-toolkit.rpm
wget  Percona.com/get/Percona-toolkit.deb
How to get started with Percona Toolkit :- Percona Toolkit have good documentation for how to use it , below is the link
Note:- Please read Percona Toolkit documentation before using it on Production environment.

Monday, March 23, 2015

Google Authenticator with SSH

Google Authenticator is PAM module, we can use it with SSH for two-factor authentication.

We need to enter secret code each time we want to SSH the server.

Attention: If you activate the google-authenticator for a normal user but not for root you can't login with the root user directly anymore. You will need to login as the new user first, then switch to the super user with the su command to get root.

Before you do anything on your server, install the Google Authenticator application, it is available for Android, iOS and BlackBerry.  After this connect to your server and switch to the root user.

For Deb based nix

apt-get install libpam-google-authenticator


libqrencode3 will be installed automatically and will allow you to use the camera of your phone to scan the qr-code directly from the console.

To use the module you have to edit two configuration files.

nano /etc/pam.d/sshd

Add the following line on top of the file:

auth required pam_google_authenticator.so

One more file to edit:

nano /etc/ssh/sshd_config

Find and change the following line:

ChallengeResponseAuthentication yes

Activate the Two-Factor Authentication For a User

google-authenticator

you will get qr-code

Finally restart the SSH server.

/etc/init.d/ssh restart