Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Other Things Gentoo
  • Search

[SOLVED] bash prompt not showing the hostname correctly

Still need help with Gentoo, and your question doesn't fit in the above forums? Here is your last bastion of hope.
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
mdoko
n00b
n00b
User avatar
Posts: 15
Joined: Thu Mar 23, 2006 6:37 pm

[SOLVED] bash prompt not showing the hostname correctly

  • Quote

Post by mdoko » Wed Nov 19, 2025 2:11 am

So, I'm having the silliest problem.

Look at this:

Code: Select all

mdoko@localhost ~ $ echo $PS1
\u@\h \w \$
mdoko@localhost ~ $ cat /etc/hostname
kuki
I have no idea what's going on. Why is the prompt ]mdoko@localhost and not mdoko@kuki?

Everything used to be fine. I'm totally at a loss.

Is there another place where bash looks for the value of \h?
Last edited by mdoko on Fri Nov 21, 2025 1:33 am, edited 1 time in total.
Top
rab0171610
l33t
l33t
Posts: 721
Joined: Sat Dec 24, 2022 1:41 am

  • Quote

Post by rab0171610 » Wed Nov 19, 2025 4:38 am

I do not use the hostname in my bash prompt. As a test, I added both /h and /H to my PS1. Both return my hostname as found in /etc/hostname.
What is the output of both:

Code: Select all

hostname 
and

Code: Select all

hostname -f
On my machine hostname returns my hostname as found in /etc/hostname and hostname -f returns the string 'localhost' as found in /etc/hosts.
To answer your question about where does bash get the hostname from, I don't think that it parses /etc/hostname directly. I believe from reading what others state about bash source code that it is using the glibc gethostname()function. I cannot verify this as true and someone here may have a better understanding of that.
It might be that bash is returning a fully qualified hostname which in my case returns 'localhost'. This is the same output you would get with 'hostname -f'. That is just a hypothesis. Someone else maybe be able to clarify that.
A temporary workaround would be to use the desired hostname as a string in your PS1 bash prompt variable rather than /h.
Obviously if the local hostname were to change that string would have to be modified accordingly, but in the meantime it would get the desired bash prompt until have a better understanding of how bash is determining the hostname when using /h.
Lastly, what is the output of:

Code: Select all

echo $HOSTNAME
In my case, that returns the hostname as found in /etc/hostname. If this returns your desired hostname, you can use '$HOSTNAME' instead of /h in the bash PS1 variable string as a temporary workaround.
Top
RumpletonBongworth
Apprentice
Apprentice
User avatar
Posts: 155
Joined: Mon Jun 17, 2024 1:17 am

Re: bash prompt not showing the hostname correctly

  • Quote

Post by RumpletonBongworth » Wed Nov 19, 2025 10:47 pm

mdoko wrote:So, I'm having the silliest problem.

Look at this:

Code: Select all

mdoko@localhost ~ $ echo $PS1
\u@\h \w \$
mdoko@localhost ~ $ cat /etc/hostname
kuki
To begin with, what is the output of sysctl kernel.hostname?
Top
mdoko
n00b
n00b
User avatar
Posts: 15
Joined: Thu Mar 23, 2006 6:37 pm

  • Quote

Post by mdoko » Fri Nov 21, 2025 1:32 am

I seem to have figured it out. The culprit seems to have been NetworkManager.


After adding

Code: Select all

[main]
hostname-mode=none
to /etc/NetworkManager/NetworkManager.conf everything seems to work fine.

The final clue came from this forum post I stumbled upon. I have to say I'm still not fully understanding why has this suddenly become an issue. ¯\_(ツ)_/¯
Top
rab0171610
l33t
l33t
Posts: 721
Joined: Sat Dec 24, 2022 1:41 am

  • Quote

Post by rab0171610 » Fri Nov 21, 2025 8:32 am

I realize you consider this solved and have found a workaround of sorts. While you had a limited concern about the hostname value as reflected in your bash prompt and setting the 'hostname-mode=off' fixed that, the bigger question is why NetworkManager does not intially recognize that you have a static hostname and instead is configuring a transient hostname:

Code: Select all

hostname-mode
Set the management mode of the hostname. This parameter will affect only the transient hostname. If a valid static hostname is set, NetworkManager will skip the update of the hostname despite the value of this option. A hostname empty or equal to '(none)' is considered invalid . . .
none: NetworkManager will not manage the transient hostname and will never set it.

https://manpages.debian.org/unstable/ne ... .5.en.html
Instead of finding and using the locally configured static hostname, your machine is configured to ask the DHCP server for the hostname. The transient hostname is set to 'localhost' either by design or defaults to 'localhost' if it is not given by the DHCP server. The 'hostmode-mode=off' entry does prevent asking the DHCP server for the hostname but it should only do that if configured to do so.
It sounds like this is specific to your network setup.
This would make sense if:
A) You did not set the value for the hostname locally (i.e. /etc/localhost was empty)
B) Your host machine is inside of a VM or container or using a network bridge
C) There is more than one network manager or DHCP service running on the the machine which conflicts with NetworkManager.
D) Your system is configured not to send the static hostname to the DHCP server and to instead ask the DHCP server for the hostname.

I am providing additional information to consider should you or someone else using systemd with a similar problem that stumbles upon this decide to further inspect their network setup with regards to DHCP hostname.

NetworkManager relies on the systemd-hostnamed service to read and write the static host name as stored in the /etc/hostname. It would be useful to check the status of the systemd-hostnamed service:

Code: Select all

systemctl status systemd-hostnamed
If the service was loading at boot and working correctly, the output would look something similar to:

Code: Select all

○ systemd-hostnamed.service - Hostname Service
     Loaded: loaded (/usr/lib/systemd/system/systemd-hostnamed.service; static)
     Active: inactive (dead) since Fri 2025-11-21 02:53:21 EST; 7min ago
   Duration: 30.041s
 Invocation: 1642684f79664249b246e83eed986c63
TriggeredBy: ● systemd-hostnamed.socket
       Docs: man:systemd-hostnamed.service(8)
             man:hostname(5)
             man:machine-info(5)
             man:org.freedesktop.hostname1(5)
    Process: 221063 ExecStart=/usr/lib/systemd/systemd-hostnamed (code=exited, status=0/SUCCESS)
   Main PID: 221063 (code=exited, status=0/SUCCESS)
     Status: "Shutting down..."
   Mem peak: 2.4M
        CPU: 26ms

Nov 21 02:52:51 tux systemd[1]: Starting Hostname Service...
Nov 21 02:52:51 tux systemd[1]: Started Hostname Service.
Nov 21 02:53:21 tux systemd[1]: systemd-hostnamed.service: Deactivated successfully.
When under those conditions, It might also be insightful to see if the output of:

Code: Select all

hostnamectl
list both a static hostname and a transient hostname:

Code: Select all

Static hostname: localhost.localdomain
Transient hostname: home.domain.example
(My output only lists a static hostname.)

For configuration of network connections I only have NetworkManager installed and built without dhclient or dhcpcd :

Code: Select all

- - dhclient           : Use dhclient from net-misc/dhcp for
                          getting an IP via DHCP
- - dhcpcd             : Use net-misc/dhcpcd for getting an IP
I use KDE Network Manager gui (plasma-nm) to configure interfaces, which uses the underlying nmcli to configure network interfaces.
In the KDE network configuration (plasma-nm) IPV4 tab (IPV6 disabled here) there is an advanced options which opens a dialog that has options for send hostname (checked) and a DHCP hostname field populated with my machines hostname as found in /etc/hostname.

Network connection configuration can also be done via nmtui. Nmtui can be used to set hostname options. If someone is using dhcpcd or dhcpcd as a backend for NetworkManager, then those might be configured to set the hostname. For example:
https://wiki.gentoo.org/wiki/NetworkMan ... a_hostname

Code: Select all

Setting a hostname

If NetworkManager was built with the dhclient USE flag enabled a hostname can be set using the following command:
FILE /etc/dhcp/dhclient.conf

send host-name "customhostname";
With that in mind, It might be helpful to check for multiple conflicting DHCP hostname configurations on the system.

When using NetworkManager, systemd users should make sure that systemd-networkd service is disabled and not running as this can also interfere with NetworkManager and could cause conflicts with the DHCP hostname settings. At one time (it may still be the case), systemd-networkd was in the list of default systemd services. When following the gentoo installation handbook the command 'systemctl preset-all' would enable systemd-networkd service on new installations. For users that would later be installing and using NetworkManager, they would need to disable systemd-networkd. They may not realize that systemd-networkd conflicts with NetworkManager or that they had even originally enabled it when enabling the default selection of systemd's preset services.
In addition to systemd-networkd, it may be necessary to review the enabled systemd services to look for and disable any other network managers or DHCP servers which would conflict with NetworkManager. To see a list of services on systemd and their status, use:

Code: Select all

systemctl --type=service


Nmtui configuration tool does have an option to set the hostname as well:
https://networkmanager.dev/docs/api/latest/nmtui.html
Nmcli provides for several hostname configuration settings, specifically the dchp-hostname property (one could also choose to send a fully qualified domain name instead):

Code: Select all

ipv4.dhcp-hostname
If the "dhcp-send-hostname" property is TRUE, then the specified name will be sent to the DHCP server when acquiring a lease. This property and "dhcp-fqdn" are mutually exclusive and cannot be set at the same time.
https://networkmanager.dev/docs/api/lat ... nmcli.html

With all of that in mind, it might be helpful to check any of the NetworkManager connection-specific configuration files for any DHCP hostname conflicts. In my case, those files are found in /etc/NetworkManager/system-connections/ .

For further reading, an interesting and relatively recent breakdown of some of the changes in the default order of the hostname assignment in NetworkManager:

Code: Select all

A quick overview of NetworkManager 1.40 hostname assignment handling
Published: 16 Jul, 2024 
https://networkmanager.dev/blog/address ... ager-1-40/
Top
Post Reply

5 posts • Page 1 of 1

Return to “Other Things Gentoo”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic