Forrest logo
back to the hostnamectl tool

hostnamectl:tldr:e755d

hostnamectl: Set a pretty hostname for the computer.
$ sudo hostnamectl set-hostname --static "${hostname-example-com}" && sudo hostnamectl set-hostname --pretty "${hostname}"
try on your machine

This command is used to set the hostname of a Linux system using the hostnamectl command with sudo privileges.

Let's break down the command:

  1. sudo: This is a command used in Unix-like systems to execute a command with administrative/root privileges. It allows the user to run a command as another user, usually the superuser.

  2. hostnamectl: This is a command-line tool used in Linux systems to control and set the system's hostname.

  3. set-hostname: This option, when used with hostnamectl, allows us to set or change the hostname of the system.

  4. --static "${hostname-example-com}": This part of the command sets the static hostname of the system to the value of the variable hostname-example-com. The double quotes are used to make sure the value is interpreted as a single string. You would replace ${hostname-example-com} with the desired hostname, such as example-com.

  5. &&: This is a command separator in Linux, used to execute the next command only if the previous command succeeds.

  6. sudo hostnamectl set-hostname --pretty "${hostname}": This part of the command sets the pretty hostname of the system to the value of the variable hostname. The double quotes are used to ensure the value is interpreted as a single string. You would replace ${hostname} with the desired hostname.

In summary, this command sets the static hostname and the pretty hostname of a Linux system using the hostnamectl command with administrative privileges (sudo). The static hostname is a persistent name that doesn't change across reboots, while the pretty hostname is a more human-readable variant.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the hostnamectl tool