Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
NetworkManager and hostname config files
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Networking & Security
View previous topic :: View next topic  
Author Message
brendlefly62
Tux's lil' helper
Tux's lil' helper


Joined: 19 Dec 2009
Posts: 86

PostPosted: Wed Dec 06, 2017 3:08 pm    Post subject: NetworkManager and hostname config files Reply with quote

I'm a little confused by tips I've seen in old posts on the subject of "my machine now boots up as new-host-3" and such... a bit first for context, then a couple simple questions at the bottom:

Background:
most of the gentoo systems I've operated have been servers with no graphical interface (i.e. no X); however, a couple years ago, I started experimenting with desktop and laptop builds using gnome and kde, more recently switching to kde5/plasma. In previous work, I struggled to get networking (wifi only) to function properly using networkmanager, so I tended to just bring up the interface in my default runlevel and run a custom script to use wpa_cli and wpa_supplicant commands to bring up wifi and start dhcpcd for the interface. This worked, but cooler things like plasma's nm app didn't like that, so I switched to networkmanager about a month ago. I first had to find a couple kernel settings and per-package USE flags that I had to fix to get networkmanager working properly. Then, as a start point for the observations and questions below, I noticed that although the system seemed to boot up happily, having brought up wifi, gotten an ip addr from the dhcp server, and everything else looked good -- I now saw that the system seemed to have asked the dhcp server what hostname it should have instead of assigning it per /etc/conf.d/hostname. In one case, the hostname was "new-host-3" though the numeral differed on a couple other systems. So, I tried to fix that...

First, following this https://forums.gentoo.org/viewtopic-t-821571-start-0.html , I tried applying recommendations to fix this in /etc/NetworkManager/nm-system-settings.conf (which I then discovered, if you use it, should be now moved to /etc/NetworkManager/NetworkManager.conf ... with content:
Code:
[main]
plugins=keyfile

[keyfile]
hostname=myHostName
... but that alone did not work.

While using dhclient, I tried following this https://wiki.gentoo.org/wiki/NetworkManager advice to send the hostname to the dhcp server by adding to /etc/dhcp/dhclient.conf ...
Code:
send host-name "yourhostname";
... but again this alone did not work.

In another old forum discussion, a Gentoo developer had recommended building networkmanager with USE=dhcpcd -dhclient and switching to dhcpcd... again that alone did not work.

Then, while reading the actual /etc/init.d/hostname script, I noticed that it was looking for hostname to be set in /etc/hostname and appeared to have backward compatibility code to accept a host name from /etc/conf.d/hostname
FILE /etc/init.d/hostname
Code:
...
   if [ -s /etc/hostname ] && [ -r /etc/hostname ]; then
      read h x </etc/hostname
      source="from /etc/hostname"
   else
      # HOSTNAME variable used to be defined in caps in conf.d/hostname.
      # It is also a magic variable in bash.
      h=${hostname:-${HOSTNAME}} # checkbashisms: false positive (HOSTNAME var)
   fi
...


So, I created /etc/hostname with a single word of content (myHostName), and suddenly it started working. At this point I was using dhcpcd so I don't have an /etc/dhclient.conf that actually contains another setting of the hostname (though I don't know that it would not also work with dhclient if you configured it that way -- just why would you want to have to put the host name into so many conf files?) and I don't have ANY /etc/NetworkManager/NetworkManager.conf file (again avoiding the need to put the hostname into another conf file).

In all of the old discussions I saw in the forums on "my machine now boots up as new-host-3" and such, I didn't see anyone say "oh by the way, ignore what the handbook says about configuring the system by setting hostname in /etc/conf.d/hostname and set it in /etc/hostname instead...

So my questions are:
(1) did I get this working by accident, or is this actually the simplest way to have the hostname set properly when using networkmanager?

(2) why does the handbook direct the user to configure the system by putting hostname in /etc/conf.d/hostname when the /etc/init.d/hostname script seems to treat that as a legacy approach, seemingly preferring to find it in /etc/hostname?

Cheers, and Thanks!
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1332
Location: United Kingdom

PostPosted: Thu Dec 07, 2017 1:30 am    Post subject: Reply with quote

I am also using NetworkManager instead of netifrc:

Code:
# rc-update show -v | grep -i net
       NetworkManager |      default
                local |      default nonetwork
           net-online |
         net.enp4s0f1 |
               net.lo |
             netmount |      default

Code:
# rc-update show -v | grep -i dhcp
               dhcpcd |
                dhcpd |

Code:
# grep -v ^# /etc/conf.d/netmount
rc_need="NetworkManager"

I have the netmount service enabled for when I want to use network-attached file shares at home or in one of the various office locations where I work.

At various times in the past I have used NetworkManager with either dhcpcd or dhclient, and have had a problem with both DHCP clients at different times. But for the last couple of years I have been using dhclient without any trouble. A couple of years ago NetworkManager developer Dan Williams' blog (and a NetworkManager bug report, IIRC) stated that dhclient is the preferred DHCP client for NetworkManager. Indeed, the Gentoo Linux Wiki article on NetworkManager currently states the following:

Quote:
It is a good idea to use dhclient from net-misc/dhcp instead of net-misc/dhcpcd as 1.0.0 version is already doing. While dhcpcd in standalone mode provides some interesting features over dhclient, those will not be used by NetworkManager. Most NetworkManager developers do use dhclient and it is therefore much better tested with NetworkManager and is generally a better DHCP client to be used with NetworkManager. NetworkManager does not use the IPv6 support of dhcpcd and instead relies on dhclient for this.


I am using NetworkManager with dhclient on my Clevo W230SS laptop running Gentoo Stable, and the hostname (I'll call it 'myhostname' here) is specified in the following three files:

Code:
# cat /etc/hosts
127.0.0.1 myhostname localhost
::1 myhostname localhost


Code:
# cat /etc/conf.d/hostname
# Set to the hostname of this machine
hostname="myhostname"


Code:
# cat /etc/dhcp/dhclient.conf
send host-name "myhostname";
supersede host-name "myhostname";


In earlier days it was necessary to specify the hostname in /etc/NetworkManager/NetworkManager.conf but that is no longer required. According to NetworkManager.conf(5) man page, 'This key is deprecated and has no effect since the hostname is now stored in /etc/hostname or other system configuration files according to build options.' I just left it in the file because it does no harm:

Code:
# cat /etc/NetworkManager/NetworkManager.conf
[main]
plugins=keyfile
rc-manager=none
dhcp=dhclient
no-auto-default=*

[keyfile]
hostname=myhostname

_________________
Clevo W230SS: amd64, OpenRC, nvidia-drivers & xf86-video-intel.
Compal NBLB2: ~amd64, OpenRC, xf86-video-ati, dual booting with Win 7 Pro 64-bit.
KDE on both laptops.

Fitzcarraldo's blog
Back to top
View user's profile Send private message
brendlefly62
Tux's lil' helper
Tux's lil' helper


Joined: 19 Dec 2009
Posts: 86

PostPosted: Fri Dec 08, 2017 2:52 am    Post subject: Reply with quote

Fitzcarraldo - thanks. I'll try that way too, even though it seems clunky to have to put the hostname in four different files; five counting /etc/hostname :wink:
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1332
Location: United Kingdom

PostPosted: Fri Dec 08, 2017 8:33 pm    Post subject: Reply with quote

You're welcome.

rendlefly62 wrote:
I'll try that way too, even though it seems clunky to have to put the hostname in four different files;

Three files, not four. And it's not clunky really, because they serve different purposes:

The unqualified system hostname resides in the kernel, and -- in the case of systems running OpenRC -- the initscript /etc/init.d/hostname sets the unqualified system hostname in the kernel at boot by taking the hostname specified in /etc/conf.d/hostname.

/etc/hosts is used by programs on your machine to look up hostnames that you wish to specify against static IP addresses. It maps hostnames and aliases to static IP addresses, and the file can contain more than one hostname.

The hostname specified in /etc/dhcp/dhclient.conf is the hostname the DHCP client dhclient sends to your router (the DHCP server) so that the router knows your machine's hostname.

rendlefly62 wrote:
five counting /etc/hostname

Four if you were to count /etc/hostname. However, the file /etc/hostname does not even exist on my laptop running Gentoo Stable. The system hostname to be loaded into the kernel when the machine boots is specified in the file /etc/conf.d/hostname.

As far as /etc/NetworkManager/NetworkManager.conf is concerned, all I really need in the file is:

Code:
[main]
rc-manager=none
dhcp=dhclient
no-auto-default=*


EDIT: By the way, the supersede statement in dhclient.conf has the following purpose:

man dhclient.conf(5) wrote:
supersede [ option declaration ] ;

If for some option the client should always use a locally-configured value or values rather than whatever is supplied by the server, these values can be defined in the supersede statement.

_________________
Clevo W230SS: amd64, OpenRC, nvidia-drivers & xf86-video-intel.
Compal NBLB2: ~amd64, OpenRC, xf86-video-ati, dual booting with Win 7 Pro 64-bit.
KDE on both laptops.

Fitzcarraldo's blog
Back to top
View user's profile Send private message
brendlefly62
Tux's lil' helper
Tux's lil' helper


Joined: 19 Dec 2009
Posts: 86

PostPosted: Sat Dec 09, 2017 12:34 am    Post subject: Reply with quote

All good! :D and thanks again. Just to enumerate:
1) /etc/hosts
2) /etc/dhclient.conf
3) /etc/NetworkManager/NetworkManager.conf
4) /etc/hostname
5) /etc/conf.d/hostname

I get it - for your setup, you really only need to put the hostname into three configuration files: 1,2, and 5 -- and you do it that way because dhclient is the recommended dhcp client for use with networkmanager. I'm just saying - as I had it (albeit using dhcpcd instead of the "recommended" client), I only needed to put the host name in ONE place (/etc/hostname). Also, it still looks to me like the /etc/init.d/hostname script it expects to find the hostname in /etc/hostname (and if it's not there, it will accept whatever it finds in the ${HOSTNAME} variable). i.e. it looks like it's something else other than the /etc/init.d/hostname script that populates the ${HOSTNAME} variable with the hostname found in /etc/conf.d/hostname -- do you know what that is?

Code:
$ cat /etc/init.d/hostname
#!/sbin/openrc-run
# Copyright (c) 2007-2015 The OpenRC Authors.
# See the Authors file at the top-level directory of this distribution and
# https://github.com/OpenRC/openrc/blob/master/AUTHORS
#
# This file is part of OpenRC. It is subject to the license terms in
# the LICENSE file found in the top-level directory of this
# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
# This file may not be copied, modified, propagated, or distributed
# except according to the terms contained in the LICENSE file.

description="Sets the hostname of the machine."

depend()
{
   after clock
   keyword -docker -lxc -prefix -systemd-nspawn
}

start()
{
   local h source x
   if [ -s /etc/hostname ] && [ -r /etc/hostname ]; then
      read h x </etc/hostname
      source="from /etc/hostname"
   else
      # HOSTNAME variable used to be defined in caps in conf.d/hostname.
      # It is also a magic variable in bash.
      h=${hostname:-${HOSTNAME}} # checkbashisms: false positive (HOSTNAME var)
   fi
   if [ -z "$h" ]; then
      einfo "Using default system hostname"
      return 0
   fi
   ebegin "Setting hostname to $h $source"
   hostname "$h"
   eend $? "Failed to set the hostname"
}
Back to top
View user's profile Send private message
Fitzcarraldo
Veteran
Veteran


Joined: 30 Aug 2008
Posts: 1332
Location: United Kingdom

PostPosted: Sat Dec 09, 2017 5:27 am    Post subject: Reply with quote

Well, I'm no expert in OpenRC or Bash, but my understanding is as follows...

Firstly, if I read you correctly you believe that, if /etc/hostname does not exist, /etc/init.d/hostname sets the system hostname from the variable HOSTNAME. However, that is not the case precisely. Consider the following line in /etc/init.d/hostname:
Code:
h=${hostname:-${HOSTNAME}} # checkbashisms: false positive (HOSTNAME var)


That line means the following in pseudocode:
Code:
h = IF $hostname exists and is not null
    THEN
      $hostname <---- The variable from /etc/conf.d/hostname
    ELSE
      $HOSTNAME <---- An automatically-set Bash variable
    ENDIF


So if the variable $hostname is assigned in /etc/conf.d/hostname then h=$hostname

The following line near the end of /etc/init.d/hostname then sets the system hostname:
Code:
hostname "$h"


Later on, Bash will automatically read the system hostname and assign the Bash variable HOSTNAME from it. So you can do the following in a Bash shell, for example:
Code:
clevow230ss fitzcarraldo # echo $HOSTNAME
clevow230ss


So, the short answer is: Bash but /etc/init.d/hostname assigns the hostname from the value assigned in /etc/conf.d/hostname, not from the Bash variable HOSTNAME (unless, that is, /etc/hostname does not exist AND [$hostname in /etc/conf.d/hostname does not exist OR $hostname in /etc/conf.d/hostname is NULL]).

Bash experts and/or experts in OpenRC are of course welcome to correct me if my understanding is incorrect.

Regarding the conditional code relating to /etc/hostname in /etc/init.d/hostname, you might find the following Gentoo developer discussion thread from 2016 of interest:

https://groups.google.com/forum/#!topic/linux.gentoo.dev/xQrKAEkIKcc

William Hubbs, 22/08/2016 wrote:
All,

it looks like app-emulation/docker expects /etc/hostname to exist.

On Gentoo, this file does not exist, so I'm wondering how we can make it
exist?

I know in OpenRC I can read it and use the value there as the hostname
instead of /etc/conf.d/hostname if it exists,but I'm not sure whether
OpenRC should populate /etc/hostname if it does not exist or whether
something else should do that.

Thoughts?

William


Below are the contents of /etc/init.d/hostname from openrc-0.21.1, for example (notice there is no mention of /etc/hostname):

Code:
#!/sbin/openrc-run
# Copyright (c) 2007-2015 The OpenRC Authors.
# See the Authors file at the top-level directory of this distribution and
# https://github.com/OpenRC/openrc/blob/master/AUTHORS
#
# This file is part of OpenRC. It is subject to the license terms in
# the LICENSE file found in the top-level directory of this
# distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
# This file may not be copied, modified, propagated, or distributed
# except according to the terms contained in the LICENSE file.

description="Sets the hostname of the machine."

depend() {
        keyword -prefix -systemd-nspawn -lxc
}

start()
{
        # HOSTNAME variable used to be defined in caps in conf.d/hostname.
        # It is also a magic variable in bash.
        hostname=${hostname-${HOSTNAME-localhost}} # checkbashisms: false positive
        ebegin "Setting hostname to $hostname"
        hostname "$hostname"
        eend $? "Failed to set the hostname"
}


As you will see if you read the posts in the above-mentioned thread, not all Gentoo Linux developers approved of the addition of code to check if /etc/hostname exists and (if it does) to use it instead of /etc/conf.d/hostname, because a) files in /etc/conf.d/ are the standard OpenRC way of providing data for the initscripts in /etc/init.d/, and b) the potential for confusion or mismatches.

Anyway, in the end, William Hubbs, the Gentoo developer responsible for maintaining OpenRC decided the following:

William Hubbs, 28/08/2016 wrote:
Ok all,

here is what openrc-0.22 is going to do in terms of setting the host
name.

If /etc/hostname exists, the first word of that file will be used as the
host name.
Otherwise, if the value is set in /etc/conf.d/hostname it will be used.
Otherwise, OpenRC will not touch the hostname.

One advantage of this, on Linux, is that you can set your host name in
the kernel and this setting will be respected.

Also, this means the script by default can run in containers if they
allow overriding the hostname inside the container.

William

To which the only reply in that thread was:

Daniel Campbell, 31/08/2016 wrote:
I didn't see anyone else reply to this so I will. I think that's the
best way of supporting both without requiring (much) putzing around. If
a user changes /etc/conf.d/hostname and it doesn't change, the fix would
be to delete (or update) /etc/hostname and all is good in the world again.

Supporting kernel-level hostname as a side effect is even better. No
complaints here!


Personally I find that approach a tad messy, as /etc/hostname is not the standard way of doing things in Gentoo Linux running OpenRC. For example, until you started this thread I was not even aware it was a possibility (/etc/hostname does not even exist in my Gentoo Linux installations). Anyway, it makes no difference in my case as my installations do not have /etc/hostname and I will stick with the standard OpenRC approach of using /etc/conf.d/ for OpenRC initscript configuration files.
_________________
Clevo W230SS: amd64, OpenRC, nvidia-drivers & xf86-video-intel.
Compal NBLB2: ~amd64, OpenRC, xf86-video-ati, dual booting with Win 7 Pro 64-bit.
KDE on both laptops.

Fitzcarraldo's blog
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Networking & Security All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum