If you’ve ever used a command line interface to access a computer, then you may have noticed a name at the start of each line – the system hostname:

linux bash command line

Have you ever wondered what it is, and why it’s there? If so then you’re in the right place. In this post, you will learn everything you need to know about hostnames in a Linux environment, and how to change them. But first, let’s understand what a hostname actually is.

What is a Hostname?

A hostname is a unique name that identifies a computer or device on a network. It can be used to communicate with other devices, access shared resources, or identify the owner or purpose of the device. A hostname can be any alphanumeric string – for example: laptop, server, or runcloud.io.

When accessing a resource over a network, it’s not immediately obvious what computer is being used. If you have a graphical user interface, you can make a good guess by looking at the wallpaper or the time of the device, but it is very difficult when using a command line interface – and this problem only gets worse as the number of computers increases in the network.

Having a hostname makes it possible to distinguish one computer from another. Here are some examples where hostname is useful:

  • When you connect to a wireless network, your device’s hostname may appear on the router’s client list or other devices on the same network. Changing the hostname to something more descriptive can help you identify your device easily and avoid confusion with other devices that may have similar default hostnames.
  • When you use remote access tools such as SSH or VNC to connect to your device from another computer, you may need to enter the hostname or IP address of your device. Changing the hostname to something more memorable can make it easier to remember and type the address, especially if your device has a dynamic IP address that changes frequently.
  • When you use file sharing or streaming services such as Samba or DLNA to share or access media files on your device, your device’s hostname may appear as the name of the server or the source of the files. Changing the hostname to something more meaningful can help you organize and find your files more quickly and conveniently.

How is Hostname Different from MAC Address and IP Address?

You may be wondering why we need hostnames when we already have MAC addresses, and IP addresses – both of which are different ways of identifying a device on a network. They serve different purposes and operate at different layers of the network – let’s see how.

What is a MAC Address?

A MAC address, also known as a hardware address, is a physical address that is assigned by the manufacturer of the network interface card (NIC) of the device. It is a 48-bit hexadecimal number that uniquely identifies the device at the data link layer (layer 2) of the network. It looks something like 00:E0:4C:18:89:3B and it is used by switches and other devices to forward data packets to the correct destination based on the MAC address of the sender and the receiver.

A MAC address cannot be changed easily and it doesn’t depend on the network configuration or location of the device. Moreover, if your computer has more than one way to connect to a network then it will have a separate MAC address for each of them. For example, many laptop computers can connect to a network via both Wi-Fi and wired connections.

What is an IP Address?

An IP address is a logical address that is assigned by the network administrator or the internet service provider (ISP) of the device. It is a 32-bit (IPv4) or 128-bit (IPv6) number that uniquely identifies the device at the network layer (layer 3) of the network. For example, 192.168.100.1 and fe80::1 are IPv4 and IPv6 addresses.

It is used by routers and other devices to route data packets to the correct network or subnet based on the IP address of the sender and the receiver. An IP address can be changed dynamically and it depends on the network configuration and location of the device.

How is the Hostname Different?

A hostname is a human-readable name that is assigned by the user or the system administrator of the device. A hostname can be changed easily and it doesn’t depend on the physical or logical address of the device. We need hostnames because they are easier to remember and use than MAC addresses or IP addresses, which are long and complex numbers.

Hostnames also allow us to access devices or websites using meaningful names rather than numerical addresses. For example, it is easier to type www.google.com than 172.217.160.78 in the browser. Hostnames also enable us to change the physical or logical address of the device without affecting the name of the device or the service it provides. For example, if a server crashes, you can immediately spin up a new server with the same hostname and if everything is configured correctly, nobody would notice the crash.

Is Hostname the Same as Domain Name?

A hostname and a domain name are both labels used to identify and locate resources on the internet, but they are not exactly the same. A hostname is a name assigned to a specific device or server on a network, while a domain name is a name assigned to a website or a group of servers.

Additionally, hostname is supposed to be unique in a network, but you can have more than one computer with the same hostname. However, domain names are globally unique, – you have to pay money to a domain registrar such as Namecheap or Cloudflare to register and use a domain name.

For example, www.example.com is a domain name that represents a website, and www is a hostname that identifies a specific server within the domain. A domain name can have multiple hostnames, but a hostname can only belong to one domain name. A hostname can also be a domain name if it is registered as such. For example, google.com is both a hostname and a domain name.

How to Check the Current Hostname on Linux

To check the current hostname, you can use the hostname command in the terminal. It will display the name of the device as configured in the system:

hostname
checking hostname in linux

In the above example, we can see that my-server is the hostname of the computer.

How to Set or Change the Hostname Temporarily

To set or change the hostname temporarily, you can use the hostname command with a new name as an argument. This will change the name of the device only for the current session, and it will not affect other devices on the network.

hostname newname
Changing hostname in Linux

In the above example, we can see that when we execute the hostname command with superuser privileges, we can change the hostname of the computer.

How to Set or Change the Hostname Permanently

To set or change the hostname permanently, you need to edit two files: /etc/hostname and /etc/hosts. The /etc/hostname file contains the name of the device, and the /etc/hosts file contains the mapping between the name and the IP address of the device. You need to have root privileges to edit these files.

If you are not sure how to edit files via terminal, we recommend you read our in-depth post on editing files via Nano.

sudo nano /etc/hostname
# change the name to newname and save the file
sudo nano /etc/hosts
# change the line that starts with 127.0.0.1 to include newname and save the file

For example, if you change your hostname to runcloud-sandbox then your hosts file should look like this:

127.0.0.1 localhost runcloud-sandbox

Wrapping Up

In this post, we have learned how to set or change the system hostname in Linux. We have also learned what a hostname is, and how it differs from a MAC address and an IP address.

We have limited our discussion to Linux computers only. However, you should know that other devices such as Windows and Android phones also have hostnames, but they may use different methods to set or change them.

For example, Windows allows you to change the hostname from the System Properties dialog box, while Android requires you to use the terminal or edit the system files. Some devices may also have additional names or identifiers, such as Bluetooth name, device model, or network name, that are not the same as the hostname.

If you’re looking for a way to make managing servers much easier and save time, we recommend you take a look at RunCloud.

RunCloud is a cloud server management platform that allows you to deploy, configure, and manage your web applications on any cloud provider.

With RunCloud, you can easily set up your server, install your web stack, secure your server, monitor your performance, and automate your tasks. RunCloud supports various web applications such as WordPress, Laravel, Drupal, Joomla, and more. RunCloud also supports various cloud providers such as DigitalOcean, AWS, Google Cloud, Azure, and more.

Get started with RunCloud and sign up for a free trial.