Avatar of Dwight Baer
Dwight Baer
Flag for Canada asked on

Editing /etc/hostname and /etc/hosts - recommended?

I've cloned a linux (ubuntu 20.04) workstation.

I think I need to edit /etc/hosts and /etc/hostname to rename the cloned disk.

Is there anything else I need to change/edit?

or is it better to use the hostname command?

Thanks
LinuxLinux NetworkingUbuntuLinux OS DevLinux Distributions

Avatar of undefined
Last Comment
David Favor

8/22/2022 - Mon
Seth Simmons

using either hostname command or modifying the files manually is fine
the hostname command just makes it easier for those who don't know how to change manually
don't forget to change the IP address (if not using DHCP)
Dwight Baer

ASKER
Thanks Seth.  I was just sortof double-checking.  But I'll leave this open until tomorrow in case somebody knows of anything else I should check.  I'm using DHCP;  it's not a server or anything in need of a static IP address.
noci

The hostname file is used to setup the new systemname after boot. so it will provide a new hostname.
/etc/hosts needs to contain the hostname as self reference for Debian systems to it need to stay in sync.
Check that your system has a different MAC address.. (unless the real system isn;t used anymore then there is no noeed to change anything and you may actually need to ensure it has the same MAC address).


Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
David Favor

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Dwight Baer

ASKER
David,
My /etc/hosts just has a machine name, not a full-qualified domain name.  Comments?
Also, it has:
127.0.0.1   localhost
127.0.1.1   machine-name

So the purpose of the two sed lines is to change that and input a full-qualified domain name?

Thanks
David Favor

Have an /etc/hosts entry will likely be most useful when you're running code that constantly does lookups for your machine name.

I normally place an entry of the following form on all machines I setup...

127.0.0.1	net17.faststablehosting.com net17

Open in new window


So 127.0.0.1 returns for the fully qualified hostname + also the short hostname.

If you have a local DNS caching server running, likely this is overkill + if not, this will make a big difference when you're doing command line work where you reference your own machine name.

Er... There was a typo above... Should be 127.0.0.1 so 127.0.1.X crept into my script at some point...

To many long nights working on hosting problems + email delivery/inboxing problems... :-)
David Favor

Hum... I see several problems with this script.

I'll fix it + attach a copy.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Favor

That's what I get for using the same script for 10-20+ years, with no review.

Rewrote this from scratch to implement various best practices which have developed of the years.
set-hostname.pl
Dwight Baer

ASKER
Thank  you very much!
David Favor

You're welcome!

Thanks to your question also, as it surfaced... several... oddities in old code I've been using...
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy