Use full Linux commands

Here are some of Linux usefull general commands

View all Environment variables

env command can be used to view all enviroment variable and its value for current logged user via terminal.

ubuntu@ip-xx-xx-xx-xx:~/maven/JavaProj$ env
SSH_CLIENT= 54904 22

Set Environment Variables on Linux

You can modify each environmental or system variable using the export command. Set the PATH environment variable to include the directory where you installed the bin directory with perl and shell scripts:

export ANT_HOME=/path/to/ant/dir
export PATH=${PATH}:${ANT_HOME}/bin:${JAVA_HOME}/bin

Above command will set value for variable ANT_HOME and append it to PATH

Unset a environment variable

Enviroment variable can be removed from current user environment dynamically without any reboot and affecting other user or same user any other environment using unset command as follows

unset ANT_HOME

Above command remove the ANT_HOME variable and it's value from current user enviroment without affecting same user other enviroment.

View verbose output for git command

Normal git command will not show the output of communication performed with GIT server while executing clone/pull/push command. To view the output of full culr response header value, we need to set the verbose output as true while executing every git command as follows.

GIT_CURL_VERBOSE=1 git clone

command will output the request and response for curl command performand by git command.

ubuntu@ip-xxx-xx-xx-xxx:~/maven/JavaProj$ GIT_CURL_VERBOSE=1 git pull
* Couldn't find host in the .netrc file; using defaults
*   Trying
* Connected to ( port 443 (#0)
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* found 596 certificates in /etc/ssl/certs
* ALPN, offering http/1.1
* SSL connection using TLS1.2 / ECDHE_RSA_AES_128_CBC_SHA1
*        server certificate verification OK
*        server certificate status verification SKIPPED
*        common name: (matched)
*        server certificate expiration date OK
*        server certificate activation date OK
*        certificate public key: RSA
*        certificate version: #3
*        subject:
*        start date: Thu, 14 Dec 2017 00:00:00 GMT
*        expire date: Fri, 14 Dec 2018 12:00:00 GMT
*        issuer: C=US,O=Amazon,OU=Server CA 1B,CN=Amazon
*        compression: NULL
* ALPN, server did not agree to a protocol
> GET /v1/repos/PHPWordPress/info/refs?service=git-upload-pack HTTP/1.1
User-Agent: git/2.7.4
Accept: */*
Accept-Encoding: gzip
Accept-Language: en-US, *;q=0.9
Pragma: no-cache
< HTTP/1.1 401 Unauthorized



Tags: GIT, CURL, Env, Export

Shell script mask or replace ip-address with string

Using regular expression in shell script we can easily search and replace all ip address to a specific character or a string using sed command.

$ sed -e 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/x.x.x.x/g' file.txt

above command will give the output by replace the all ip-address using the matching patter and replace it with x.x.x.x. Transfer the output to a tmporay files and replace the file.

Using grep we can get the list of all IP-Address available in a file

$ grep -Eo "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" file.txt

above command will extract all IP-Address with in the file.


Tags: Sed, Grep Command

Run command on remote server via ssh

If you need to run a command or shell script on a remote server via ssh


ssh <username>@<hostname> '<commands>'
             ssh command uses three mandatory input, 
             username = username to login to remote system
             hostname = Remote system name or ip address
             commands = single command or list of commands seperated by semicolon

Command will login to remote system with the provided username and password and executes the listed command and retrives the return data and shows in terminal or you can redirected to a file using redirect sysmbol.

Instead of execute a command, you can also execute a shell script or any executable remotely.


ssh user@system1 'ls -l'

above command will execute the ls command in home folder of the user and returns a long list of file lists available.

ssh user@system 'pwd; ls; whoami'

above command will execute the commands one by one. 

pwd - current directory - home directory of the logged user, ls - list files in home directory, whoami - shows the current logged in user name.

Executing a remote shell script or a executable

ssh -t user@remotesystem 'sudo service mysql restart'

To start or to stop a service or executable with sudo user access we need to use -t option to Force pseudo-terminal allocation.

-t      Force pseudo-terminal allocation.  This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful,

e.g. when implementing menu services.  Multiple -t options force tty allocation, even if ssh has no local tty.


Tags: SSH, Remote Command

Copy files using scp from one system to remote system

Linux administrator should be familiar with CLI environment. Since GUI mode in Linux servers is not a common to be installed. SSH may the most popular protocol to enable Linux administrator to manage the servers via remote in secure way. Built-in with SSH command there is SCP command. SCP is used to copy file(s) between servers in secure way.

scp stands for secure cp (copy), which means you can copy files across ssh connection. That connection will be securely encrypted, it is a very secure way to copy files between computers. scp uses by default the port 22, and connect via an encrypted connection or secure shell connection. (ssh for short).


scp [-12346BCEpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2


Copy one single local file to a remote destination

scp /path/to/source-file user@host:/path/to/destination-folder/
scp user@host:/path/to/source-file /path/to/destination-folder

Command copy's local fource file to remote system /path/to/destination-folder/ folder and also vice versa.

Copy one single file from a remote server to another remote server

scp user1@server1:/path/to/file user2@server2:/path/to/folder/

above command copy a file from one server to another server. This command is executed from thrid server.

Copy multiple files with one command

scp file1.txt file2.txt file3.txt

Copies multiple files with file name mentioned, to another server named

Copy all files with wildcard

scp /path/to/folder/*.ext user@server:/path/to/folder/

Command copies list of file with extension .txt with in a folder.

Copy all files in a folder recursively to a remote server

scp -r /path/to/source-folder user@server:/path/to/destination-folder/

Command copies all files and folders with-in source-folder recursively to remote server with same folder structure.

For more scp command options use man page using below man command.

man scp


Tags: SCP, Remote Copy, SSH

How to reset root MySQL

If you have forgotten MySQL root password and have root access to linux system follow the below steps.

  • Stop the MySQL server process as root users using command
$ sudo service mysql stop
  • Start MySQL serve rin safe mode with skip grant table command
$ sudo mysqld_safe —skip-grant-tables —skip-networking &
If it throughs any error saying mysqld folder not found. Create the folder
$ sudo mkdir -p /var/run/mysqld
  • login with MySQL server using command, it won't ask for password.
$ mysql -u root -p
  • Once logged in follow below MySQL sql query to change new password
mysql> use mysql;
​mysql> update user set authentication_string=password('NEWPASSWORD') where user='root';
​mysql> flush privileges;
​mysql > quit
  • Now stop MySQL server using kill command
$ killall mysqld_safe
  • Start MySQL server using default service and login with new password.
$ sudo service mysql start  


$ sudo /etc/init.d/mysql start

Also can be reset using temporary file using below steps

  1. Kill/Stop running MySQL server first
$ sudo  kill `cat /mysql-data-directory/`


$ sudo killall mysqld
  1. Create a temporary file (init_mysql.tmp) with following sql with new password in place of "MyNewPassword" in current folder.
    1. For MySQL 5.7.6 and later:
      ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword';
    2. For MySQL 5.7.5 and earlier:
      SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
  2. Start MySQL server using temporary file created in previous step.
$ mysqld --init-file=/home/me/init_mysql.tmp &
  1. After the server has started successfully delete the temporary file /home/me/init_mysql.tmp
  2. Now login in to MySQL server with the pemoprary password give in file and reset using the below SQL commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'     WHERE User = 'root' AND Host = 'localhost';
  1. Once set restart the MySQL server and all done.

Tags: MySQL, Reset Password

Linux find which process is using which port

Below commands can be used to find what are the ports currently used and associated process.

$ netstat -ant

Which tells the list of ports currently used by the system

$ lsof -i:8080

Above command lists the process name with details which uses the port 8080. Also shows process id PID and in which user the process is running.

 $ fuser 80/tcp

Also can find the process/service listening on a particular port by running the command below (specify the port).

Tags: Linux, Networking Commands

Useful GIT commands

Here are the list of major git commands and their short descriptions.

git init

    Initializes a git repository – creates the initial ‘.git’ directory in a new or for an existing project.

git config

    Configure git with username,  password, email-id, and server details. It can be configured --global / --system / --local

git clone

    Download a copy of project from GIT server (Eg: git clone

git add

    Add the files to local repository. Eg: git add file.txt / git add . - remove all changed files.

git rm

    Remove files from local repository. Eg: git rm file.txt / git rm . - remove all changed files.

git commit

    Save the added files to local repository, creates a new commit object pointing to it and sets the branch to point to that        new commit. Eg: git commit -m ‘committing added changes’ git commit -a -m ‘committing all changes, equals to git add      and git commit’

git status

    Show the current status of changes. Changed not add/commited/push to local.

git branch

    Show the list of local branches. In the output it will highlight the current active branch.

git push

    Push the commited changes from local repository to GIT server. Need to authenticate using username/password if it's       a private.

git checkout

    Switch for one branch to another. need to mention the branch name. Eg: git branch

git pull

    Fetches the files from the remote repository and merges it with your local one. This command is equal to the git fetch        and the git merge sequence. Example: git pull origin.

git diff

    Generates patch files or statistics of differences between paths or files in your git repository, or your index or your              working directory. Example: git diff

Tags: GIT Commands, GIT

How to connect GitHub from Linux using private key

  1. Login to ternimal 
  2. Create ssh public key using the following command 
    ssh-keygen -t rsa -b 4096 -C ""
    Will ask for id.rsa file number. jus hit enter.
    Next will ask for passphrase, If you need to secure you git repository provide a phrase. Otherwise just hit enter. (remember it, will ask every time when you try to commit and push the code)
  3. ~/.ssh/ file is created in your user home directory.
  4. using vi ~/.ssh/ or gedit ~/.ssh/ edit it and copy the content of the file.
  5. Login in to
  6. Select your repository => "Settings" in top menu.=> Click "Deploy keys"
  7. Input "Title" for your reference, paste the copied key in to "Key" textarea.
  8. It will ask you to confirm you account password.
    Now your are ready to connect to you repository.
  9. Now create a folder and using "git clone" command.

During git push it will ask for passphrase, If you provided during the private key generation.(@Step 2)

Tags: Linux, GitHub, SSH Private Key, GIT

cmake prefix equivalent to configure

How to mention prefix path for "cmake" same as ./configure --prefix=/home/dir1

cmake -DCMAKE_INSTALL_PREFIX:PATH=/home/dir1 . && make all install

This command will set the prefix path (install destionation) for build source, same as configure command.

Tags: Cmake, Prefix, Configure