One liner command to insert IP and hostname in /etc/hosts

This a very useful One liner command to insert IP and hostname into /etc/hosts file and is particularly useful when it comes to kickstart scripts wherein you need to have /etc/hosts entry for the newly provisioned VM and get it enrolled to various applications that include Red Hat IDM/ FreeIPA and Puppet to start with.

Run the following command to insert IP and hostname in /etc/hosts

# echo -n `ifconfig eth0 | grep inet | awk 'NR==1{print $2}' | cut -c6-18`" ";hostname;

Output:

172.16.66.212 rasst.vh.local

echo -n = "Does not output the trailing newline and thats why you can print IP and Hostname in 1 line".

That's it and please do let us know via Contact Us if you come across any issues.

Most Read Articles

Add/Detect a new disk in ...

Written By Farooq Mohammed Ahmed on Sunday, 11 January 2015 19:22
Add/Detect a new disk in ...

Replace SSL Certificates ...

Written By Farooq Mohammed Ahmed on Friday, 16 December 2016 08:05
Replace SSL Certificates ...

Using awk in Alias Comman...

Written By Farooq Mohammed Ahmed on Friday, 19 February 2016 14:22
Using awk in Alias Comman...

Script to Monitor Nagios ...

Written By Farooq Mohammed Ahmed on Monday, 18 July 2016 22:48
Script to Monitor Nagios ...

Generate/Create a SHA2/SH...

Written By Farooq Mohammed Ahmed on Tuesday, 25 October 2016 13:56
Generate/Create a SHA2/SH...