hostname keeps returning to linux if I set localhost.localdomain

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post please read how to get help
Post Reply
randallrodriguezw
Level 1
Level 1
Posts: 3
Joined: Wed Jul 25, 2018 12:47 pm

hostname keeps returning to linux if I set localhost.localdomain

Post by randallrodriguezw » Wed Jul 25, 2018 1:14 pm

Hi
I formatted my laptop that was in Fedora (after 10 years on Fedora) I reinstalled with Mint 19. Everything is perfect, even better that 18 that gave me troubles with multiple monitors and other hardware issues.

I need help because since forever, my OS was named (I know, my mistake) as localhost.localdomain, and I have several cloud servers with ssh authentication disabled and I have access only through ssh keys.

I restored /etc/ssh/ and .ssh home directory but I need to set the hostname as localhost.localdomain to gain access to those servers. At the installation process, I didn't remembered and entered "randall" as hostname. After that, now, I change hostname:
/etc/hosts
/etc/hostname
hostname localhost.localdomain
sudo hostnamectl set-hostname localhost.localdomain

If I try to restart hostname it gives me this:

root@linux:/home/rrodriguez/kororaviejo# systemctl restart hostname
Failed to restart hostname.service: Unit hostname.service is masked.

the whole process is I guess with some protection policy that keeps getting back the laptop to
root@linux instead of root@localhost
If I check the hostname it will thel "linux"
If I repeat the process without reboot it will still show "root@linux" but the hostname will output "localhost.localdomain" but do not connect to my servers

If I cannot solve this I will have to return to Fedora and I am very happy with my new Mint 19

thanks

gm10
Level 12
Level 12
Posts: 4100
Joined: Thu Jun 21, 2018 5:11 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by gm10 » Wed Jul 25, 2018 1:29 pm

randallrodriguezw wrote:
Wed Jul 25, 2018 1:14 pm
After that, now, I change hostname:
/etc/hosts
/etc/hostname
hostname localhost.localdomain
sudo hostnamectl set-hostname localhost.localdomain

If I try to restart hostname it gives me this:
These two are all that you need and you don't even need to sudo it (strangely, tbh), and strictly speaking you don't even need to change the second one, although it's better to change it, too:

Code: Select all

hostnamectl set-hostname localhost.localdomain
/etc/hosts
You don't need to restart anything then, just open a new terminal window and you'll see.
Last edited by gm10 on Wed Jul 25, 2018 1:52 pm, edited 1 time in total.

randallrodriguezw
Level 1
Level 1
Posts: 3
Joined: Wed Jul 25, 2018 12:47 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by randallrodriguezw » Wed Jul 25, 2018 1:50 pm

hello gm10, and thanks for your post
every way I change the hostname it shows "root@linux". If I reboot the computer that asks for the hostname it says "linux". If I changed back to localhost.localdomain and look for /etc/hosts and /etc/hostname the contents is correctly localhost.localdomain but it only temporarily changes to "localhost.localdomain" if I set it up with "hostname localhost.localdomain" but it is getting back to "linux" after reboot.

Anyway, with "linux" or temporarily changed with hostname command, I do not get access to my servers with ssh keys

gm10
Level 12
Level 12
Posts: 4100
Joined: Thu Jun 21, 2018 5:11 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by gm10 » Wed Jul 25, 2018 2:09 pm

While I seem to be able to set it successfully with just hostnamectl, I'd guess the problem is that localhost.localdomain isn't technically a correct hostname. localhost is the hostname, localdomain is the domain.

So try setting the hostname to just localhost via hostnamectl and then set up the local domain where you're supposed to, in /etc/hosts: 127.0.0.1 localhost.localdomain localhost (that should be the only entry defining localhost). If you did it correctly, hostname --fqdn should then output localhost.localdomain.

rene
Level 8
Level 8
Posts: 2226
Joined: Sun Mar 27, 2016 6:58 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by rene » Fri Jul 27, 2018 8:34 pm

gm10 wrote:
Wed Jul 25, 2018 1:29 pm
[ .... ] and you don't even need to sudo it (strangely, tbh)
Permission is granted through Polkit to "admin" users, on our systems defined to be, c.f. /etc/polkit-1/localauthority.conf.d/, unix-user:0 (root), unix-group:sudo (administrative users on Mint) and unix-group:admin.

gm10
Level 12
Level 12
Posts: 4100
Joined: Thu Jun 21, 2018 5:11 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by gm10 » Sat Jul 28, 2018 2:45 am

rene wrote:
Fri Jul 27, 2018 8:34 pm
gm10 wrote:
Wed Jul 25, 2018 1:29 pm
[ .... ] and you don't even need to sudo it (strangely, tbh)
Permission is granted through Polkit to "admin" users, on our systems defined to be, c.f. /etc/polkit-1/localauthority.conf.d/, unix-user:0 (root), unix-group:sudo (administrative users on Mint) and unix-group:admin.
Thanks. What I meant was I find it strange that it's set up like that, the hostname isn't something you trivially change, it makes changes to system files, it should really ask for you a password to authorize this change. It goes against the usual *nix security paradigm to set it up otherwise IMHO.

rene
Level 8
Level 8
Posts: 2226
Joined: Sun Mar 27, 2016 6:58 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by rene » Sat Jul 28, 2018 9:33 am

I'll wait a bit to agree nor disagree until poster has had his issue dealt with. In which sense...

I just tested this and while hostnamectl set-hostname localhost.localdomain is accepted without issue and correctly sets the (static) hostname to localhost.localdomain, including over reboot, it's then NetworkManager that upon starting a connection decides semi-correctly that localhost.localdomain nor, by the way, localhost is an acceptable hostname for a connected system and sets the transient hostname to either the hostname it got from DHCP, or if none, the reverse-resolved hostname for the connection IP. As far as I've been able to see no current method exists to tell Networkmanager to just leave well enough alone.

It's this transient, current hostname that is shown in the prompt and poster finding it getting reset to "linux" means that either his DHCP server (i.e., home router) provides the hostname "linux" or "linux.something" to the system or that he has a line e.g. 192.168.1.123 linux ... in his /etc/hosts file, with of course 192.168.1.123 replaced by whichever IP the at boot automatically started interface gets.

Given that poster mentioned to have named the system "randall" rather than "linux" during install, I expect the former. If so and if -- and it's somewhat of an if indeed -- the router allows it, poster could change the hostname from "linux" to "localhost.localdomain" in the router. If the router is not involved after all it's a simple matter of changing /etc/hosts to have 192.168.1.123 localhost.localdomain localhost ... and comment out any other entries for localhost (i.e., 127.0.0.1) that may be present.

Yes, that's a wrong solution seeing as how nothing other than 127.0.0.1, the loopback interface, should ever be named localhost or even localhost.localdomain but seeing as how poster seems to for now need this to get some historical fault continued, oh well; the proper solution is to change the hostname to something sensible instead.

So, to summarise:

1. Check your router to see if it's handing out the "linux" or "linux.something" hostname as part of the DHCP protocol; try to change it there if so.
2. If not, simply add 192.168.1.123 localhost.localdomain localhost ... to /etc/hosts and comment out any other entries for localhost.localdomain and localhost. Replace 192.168.1.123 with the IP that the/a automatically at boot by NetworkManager started connection gets.

2 has been verified to work here:

Code: Select all

rene@localhost ~ $ hostname
localhost.localdomain
rene@localhost ~ $ hostname -f
localhost.localdomain
rene@localhost ~ $ hostnamectl --static
localhost.localdomain
rene@localhost ~ $ hostnamectl --transient
localhost.localdomain
rene@localhost ~ $ hostnamectl --pretty

rene@localhost ~ $
Don't let the fact that only "localhost" is displayed in the prompt confuse matters: that's just how the prompt is setup on Mint:

Code: Select all

rene@localhost ~ $ PS1=${PS1//\h/\H}
rene@localhost.localdomain ~ $
Change all instances of \h to \H in the various PS1= lines in ~/.bashrc if you insist on making that permanent.

rene
Level 8
Level 8
Posts: 2226
Joined: Sun Mar 27, 2016 6:58 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by rene » Sat Jul 28, 2018 11:04 am

Noticing upon rereading that a slightly different alternative scenario is more likely; poster indeed having a 192.168.1.123 linux ... line in his /etc/hosts but this being a left-over from back when 192.168.1.123 in fact was the machine "linux"; the IPs having changed since then.

I.e., if you don't yet have your router setup for "static IP via DHCP" by reserving certain IP addresses for certain MAC addresses in the router configuration you will want to so now to guarantee that, e.g., 192.168.1.123 is always provided to you; get the MAC address of your interface from the link/ether part for your interface in the output of ip addr. You can then do the "2" thing without issue...

gm10
Level 12
Level 12
Posts: 4100
Joined: Thu Jun 21, 2018 5:11 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by gm10 » Sat Jul 28, 2018 11:21 am

rene wrote:
Sat Jul 28, 2018 11:04 am
Noticing upon rereading that a slightly different alternative scenario is more likely; poster indeed having a 192.168.1.123 linux ... line in his /etc/hosts but this being a left-over from back when 192.168.1.123 in fact was the machine "linux"; the IPs having changed since then.
A hostname different from yours for an IP different from yours should not have any impact on the issue at hand though, or what am I not considering? The "linux" hostname displayed in the terminal isn't pulled from the hosts file, either.

Otherwise I believe we are in agreement with our suggestion and you raised some good additional points.

rene
Level 8
Level 8
Posts: 2226
Joined: Sun Mar 27, 2016 6:58 pm

Re: hostname keeps returning to linux if I set localhost.localdomain

Post by rene » Sat Jul 28, 2018 12:06 pm

gm10 wrote:
Sat Jul 28, 2018 11:21 am
The "linux" hostname displayed in the terminal isn't pulled from the hosts file, either.
It is/can be in this case, as explained through the intermediate step of NetworkManager setting the (transient) hostname upon considering localhost.localdomain invalid when bringing up a connection, to either a DHCP-received hostname if any, or the reverse-resolved one for the IP it just brought up otherwise, i.e., gethostbyaddr(), i.e., from /etc/hosts if the address is specified there.

The difference between your suggestion above and my suggestion 2 is relevant; while you suggest 127.0.0.1 for localhost.localdomain and localhost -- and while that's obviously the only correct thing to do -- poster is seemingly in a situation where he for now needs his non-loopback IP linked to the localhost.localdomain name. With NetworkManager being a pain in the butt this needs doing through either router if applicable and/or /etc/hosts otherwise.

[edited]
You can verify this being a NetworkManager thing by hostnamectl set-hostname localhost.localdomain, disable automatic connections in NetworkManager (the network icon in your systray), reboot and see the localhost.localdomain hostname to have stuck. By then starting the network connection manually and seeing the transient hostname reverting back to the DHCP/hosts name.
[/edited]

Anyways, the idea was to postpone talking among our ourselves until poster's issue was dealt with, so let's try and wait for him to show up again... :)

Post Reply

Return to “Other topics”