What are alternatives to hostname command?

The `hostname` command is commonly used in Linux to display or set the system's hostname. However, there are alternatives that can also provide this information or modify the hostname. Here are some alternatives to the `hostname` command:

  • uname: The `uname` command can show the node name (hostname) when used with the `-n` flag.
  • cat /proc/sys/kernel/hostname: This command reads the current hostname directly from the system files.
  • getent hosts: This command retrieves the hostname by querying the hosts database.
  • nmcli: If you're using NetworkManager, `nmcli` can be used to manage network connections and can also show the hostname in the context of network settings.

Example usage to get the hostname:

uname -n

hostname Linux alternatives to hostname command uname /proc/sys/kernel/hostname getent nmcli