automatically generate the ip/hostname entry for the /etc/hosts in the current system

echo "$(ip addr show dev $(ip r | grep -oP 'default.*dev \K\S*') | grep -oP '(?<=inet )[^/]*(?=/)') $(hostname -f) $(hostname -s)"
I use it after a clean CentOS 7 minimal server installation to automatically populate the /etc/hosts file. Not sure why the installation does not add this entry by itself. Tested on CentOS 7 with the simplest use case: 1 static ip address and the hostname provided during installation.
Sample Output
[root@dns-server ~]# echo "$(ip addr show dev $(ip r | grep -oP 'default.*dev \K\S*') | grep -oP '(?<=inet )[^/]*(?=/)') $(hostname -f) $(hostname -s)"
10.0.2.10 dns-server.mydomain.lab dns-server

[root@dns-server ~]# echo "$(ip addr show dev $(ip r | grep -oP 'default.*dev \K\S*') | grep -oP '(?<=inet )[^/]*(?=/)') $(hostname -f) $(hostname -s)" >> /etc/hosts

[root@dns-server ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.2.10 dns-server.mydomain.lab dns-server

0
By: acavagni
2019-06-15 16:40:38

What Others Think

I have tried to continue rsync in the absence of an internet connection because of the lack of programming skills. Now I would copy all of your mentioned programming code to properly use it. Well, I check this link right here now where I found super casino promo codes that really amazed me and I am going to have wonderful weekend with my friends.
Jadyn11 · 60 weeks and 3 days ago
This is a very useful command as it has brought more convenience in my life. It is properly functioning on my PC. After giving this command for one time only, I do not have to worry about generating an IP/hostname. This command also helped me to reach to the website of gambling to read the best anime about it easily one can read the full info here about this I was surprised after reading these facts about gambling.
HayleyRice · 59 weeks and 3 days ago
You don't have to leave your home to get the vivid feeling of a good game. After I read this article https://newcasinos-au.com/ I decided to try playing at an online casino and as it turned out it is very pleasant and fun, in no way inferior to a real game.
Wernita · 17 weeks and 3 days ago
Thank you for such useful program code. This information will help many people who do not know much about programming. Now you can not look for a solution to computer problems and spend the saved time playing online casino games.And for safe withdrawal of funds, I advise to use only verified system. Such as https://depositpp.com/. Thanks again for sharing such a necessary tool!
HollySmith · 1 week and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: