5

On my Fedora 19 system, I am able to change the system hostname with hostnamectl. This allows me to set several things, such as the static (normal) hostname, as well as a "pretty" hostname.

Is there a simple command that retrieves the pretty hostname, from a bash prompt?

hostname returns the static hostname, and the man page shows no options to recover the pretty one.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Redoubts
  • 151
  • 1
  • 7

3 Answers3

6

As per man hostnamectl:

The static host name is stored in /etc/hostname, see hostname(5) for more information. The pretty host name, chassis type and icon name are stored in /etc/machine-info, see machine-id(5).

Therefore, if you have set a pretty hostname using the command

hostnamectl set-hostname --pretty YourHostname

you can retrieve it using a tool like awk:

awk -F= '/PRETTY/ {print $2}' /etc/machine-info
jasonwryan
  • 71,734
  • 34
  • 193
  • 226
  • @Evgeny Vereschagin Piping awk to sed is an ugly as it is redundant: `awk -F= '/PRETTY/ {gsub(/"/,"");print $2}' /etc/os-release` is simpler... – jasonwryan Aug 28 '15 at 09:43
  • I agree. But that doesn't work with my example. Try `hostnamectl set-hostname --pretty '"MyPretty\\Name"'`, `awk -F= '/PRETTY/ {gsub(/"/,"");print $2}' /etc/machine-info`. The result is wrong. – Evgeny Vereshchagin Aug 28 '15 at 10:14
  • @EvgenyVereshchagin Who would ever set `'"MyPretty\\Name"'` as a *pretty* name? – jasonwryan Aug 28 '15 at 10:16
  • well. this is an extreme case. try `sudo hostnamectl --pretty set-hostname "Lennart's Laptop"` (example from the manpage) on Fedora 19. `awk -F= '/PRETTY/ {gsub(/"/,"");print $2}'` prints wrong result `Lennart\'s Laptop`. – Evgeny Vereshchagin Aug 28 '15 at 10:26
  • I don't like `awk ... | sed ...` too. the `sed`-only solution: `sed 's/^[^=]*=//; s/^"//; s/"$//; s/\\\(.\)/\1/g' /etc/machine-info` – Evgeny Vereshchagin Aug 28 '15 at 12:43
  • 1
    @EvgenyVereshchagin Yes: my point is that piping awk to sed is *always* wrong. If you want to post a sed answer, then you are welcome to do that... – jasonwryan Aug 28 '15 at 19:14
  • https://unix.stackexchange.com/a/433245/5132 explains why `awk` and `sed` are poor ideas for this type of configuration file. – JdeBP Jul 11 '18 at 20:45
5

hostnamectl --pretty will directly print the pretty hostname out. Tested on systemd version 239.

炸鱼薯条德里克
  • 1,337
  • 1
  • 12
  • 31
0

For my system, hostnamectl --static was the ticket. It is worth first running hostnamectl to see how your system is set up. From there, you can determine what steps make the most sense to make it pretty.