Ask Your Question

Add the loopback entry to the first line in the /etc/hosts file

asked 2017-04-10 01:09:16 -0500

monalsharma gravatar image


I need to create a module which checks if the /etc/hosts file has the first line with the loopback entry.

If it does, do nothing; else, insert localhost localhost.localdomain

in the first line of the file right after the comments.

I tried using augeas but even if I write something using node(0), it doesn't let me save that file. I tried with augtool using the Hosts lens. Can someone please help me if this is possible or not.

Thanks, Monal

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-04-10 09:21:01 -0500

DarylW gravatar image

I would suggest using the native host resource.

host { 'localhost':
  ensure       => 'present',
  ip           => '',
  host_aliases => ['localhost.localdomain'],

( Similar answer to )

edit flag offensive delete link more


But it doesn't make the entry at the first line, if, • When entry is not there - it is inserted in the last • When entry is in some other position other than the first - it makes changes to the entry content but doesn't move to first line

SundarKid gravatar imageSundarKid ( 2017-04-13 04:11:48 -0500 )edit

Thanks for your help DarylW. Unfortunately, the ask is to add the loopback entry as the "first" line in the file. With your solution, here's what happens: When loopback entry does not exist in file: It gets added as the last line (incorrect solution) If it exists (1st or any line), it does nothing.

monalsharma gravatar imagemonalsharma ( 2017-04-17 05:51:04 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-04-10 01:09:16 -0500

Seen: 146 times

Last updated: Apr 10