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

by | Apr 22, 2016 | RHEL / CentOS

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.

Related Articles….