Tuesday, March 10, 2015

Useful Commands, Code snippets & other Tips and Tricks

A code snipped to add a user with sudo access, that forces the user to reset password on the first login:

USER=admin_user
useradd -d /home/$USER -m -s /bin/bash $USER
sudo usermod -a -G sudo $USER
sudo echo "$USER   ALL=(ALL:ALL) ALL" | sudo tee -a /etc/sudoers 2>&1 > /dev/null
echo -e "password\npassword" | passwd $USER
chage -d 0 $USER
--------------------------------------------------------------------------------------------------------------------------

To find a string and replace it with another string globally inside a file (this is using “vi” or “vim” editor only)

# vi filename
:%s/find_string/replace_string/g
--------------------------------------------------------------------------------------------------------------------------

To find all files with a specific string within a folder (or path) using grep
# grep -rnw /path_to_folder


This will list out the lines with that mentioned string with-in the path, including the line number in which it is present.
--------------------------------------------------------------------------------------------------------------------------

To display all "uncommented" lines in a file using grep
grep -v '#' rm_erlang.sh

for i in `cat /home/appuser/erlang_pkg.txt`;
do

sudo rpm -e $i ;

done

This will display all the lines which are not commented ('#') from the file rm_erlang.sh. The "-v" will exclude all the lines staring with '#' in the file. But if you note we have blank lines in between the other un-commented lines.
--------------------------------------------------------------------------------------------------------------------------

To display all lines in a file excluding blank lines and comments (commented lines) using grep

To get all the lines excluding the lines which are commented (starts with '#') and those which are blank lines, we need to use the following command:

$ cat rm_erlang.sh | grep -v '#' | grep -v '^$'

for i in `cat /home/appuser/erlang_pkg.txt`;
do
sudo rpm -e $i ;
done
--------------------------------------------------------------------------------------------------------------------------

To display all lines which start with any specific string(s) using grep

To get all the lines in the file rm_erlang.sh, which start with the string "for" and the string "sudo", we need to use the following command:

$ egrep '^for|^sudo' rm_erlang.sh

for i in `cat /home/appuser/erlang_pkg.txt`;
sudo rpm -e $i ;
--------------------------------------------------------------------------------------------------------------------------

Finding external ip address of my server with "curl" command:

$ curl ifconfig.me
or
$ curl ifconfig.co
or
dig +short myip.opendns.com @resolver1.opendns.com
or
dig TXT +short o-o.myaddr.l.google.com @ns1.google.com
--------------------------------------------------------------------------------------------------------------------------

Create functions in Linux to make our redundant work easy:

Syntax:
function function_name { arguments; }

Example:
Function to find internal IP  address of our server :

$ function internal_ip { /sbin/ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}'| cut -d":" -f 2; }

Run the function as follows:
$ internal_ip

Output will give your server's internal IP address.
---
Same way we can write a function to determine the external IP address of our server as follows:

$ function external_ip { /usr/bin/curl ifconfig.me; }

Run the function as follows:
$ external_ip

Output will give your server's external IP address.
--------------------------------------------------------------------------------------------------------------------------

Using "awk" command to  print columns & text:

Syntax:
awk '{print $column_number}'

Example:
Let us take an example to print the IP address using "awk" command with "ifconfig":

We can run the following command to get the IP address:
$ /sbin/ifconfig | grep "inet " | grep -v "127.0.0.1" | awk '{print $2}' | cut -d":" -f 2

Output:
$ xxx.xxx.xxx.xxx

Where as if we want to use any specific delimiter to split the columns, then we can use the option "-F" with awk command. As shown below:

$ /sbin/ifconfig | grep "inet " | grep -v "127.0.0.1" | awk -F ":" '{print $2}' | awk -F " " '{print $1}'

This is something simillar to "-d " option in "cut" command - as shown in the example above.

Output:
$ xxx.xxx.xxx.xxx

We can add more meaning to the output if we can print some text to add meaning with the output which says "IP address: " or so, so that it is very clear what it is.. We can achieve it using "awk" command as follows:

$ /sbin/ifconfig | grep "inet " | grep -v "127.0.0.1" | awk -F ":" '{print $2}' | awk -F " " '{print "IP addr: " $1}'

Output:
$ IP addr: xxx.xxx.xxx.xxx

What if we just want to print some text alone with some command's output...!!
Yes, we can do that as well with awk, as follows:

$ whoami | awk '{ print " Hello... " $1}'

Output:
$ Hello... user
--------------------------------------------------------------------------------------------------------------------------

Using "pkexec" command 

If you happened to screw-up your /etc/sudoers file and you don't have a root access to the box. Then this command "pkexec" comes handy in fixing such issues...

Example Scenario:
I try to add "NOPASSWD:ALL" option to /etc/sudoers file for my name so that I can avoid typing password everytime I use "sudo". Save and exit the file and try using "sudo su -" command and I get nowhere and I get an error message as follows:

sriti@myserver:~$ sudo su -
sudo: >>> /etc/sudoers: syntax error near line 20 <<<
sudo: parse error in /etc/sudoers near line 20
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
sriti@myserver:~$ 

I try different user accounts which have "sudo" access, but have no luck.
I tried copying the file so that I can edit it and replace it ---> but no luck as it still requires "sudo" to copy the file from "/etc/"......


sriti@myserver:~$ cp -p /etc/sudoers /var/tmp/
cp: cannot open `/etc/sudoers' for reading: Permission denied
sriti@myserver:~$ cat /etc/sudoers

cat: /etc/sudoers: Permission denied

I such scenario, we may use the command "pkexec", which is a real lifesaver. 

Syntax
$ pkexec  

We can use pkexec (Policy Kit EXECution command) with your preferred editor as shown below (to exit, save & exit) :

sriti@myserver:~$ pkexec vi /etc/sudoers
==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/bin/vi' as the super user
Multiple identities can be used for authentication:
 1.  sriti
 2.  user2
 3.  user3
 4.  user4
 5.  user5
 6.  user6
Choose identity to authenticate as (1-6): 1
Password:
==== AUTHENTICATION COMPLETE ===
sriti@myserver:~$ sudo su -
[sudo] password for sriti:

root@myserver:~#