Facebook Twitter Instagram
    Trending
    • Bash Scripting Introduction
    • How to change your hostname
    • Configuring a static IP on AlmaLinux
    • How to migrate from CentOS 8 to a supported distro
    • Hands on with Rocky Linux 8.3 RC
    • AlmaLinux is Here!
    • AlmaLinux launches in 1 week
    • How to transfer files with SCP command
    Facebook Twitter Instagram
    The Linux Lab
    • Home
    • AlmaLinux

      How to change your hostname

      April 11, 2022

      Configuring a static IP on AlmaLinux

      February 12, 2022

      AlmaLinux is Here!

      March 30, 2021

      AlmaLinux launches in 1 week

      March 24, 2021

      AlmaLinux Beta 1 is here!

      February 2, 2021
    • CentOS
    • Rocky Linux
    • Tutorials
    • About Us
    The Linux Lab
    You are at:Home»AlmaLinux»How to change your hostname

    How to change your hostname

    0
    By Travis on April 11, 2022 AlmaLinux, CentOS, Red Hat, Rocky Linux, Tutorials
    Advertisement

    There are times where you might want to change the hostname of your Linux system. Perhaps you changed your domain name or when you first provisioned the system, you called it something else and the name just doesn’t seem to work anymore. Changing the hostname is a common procedure, and I’ll walk you through the steps.

    What is a hostname?

    As the name suggests, it’s a name for your host. If you’re from the Windows world, this is the “Computer Name”. At the end of the day, both are fundamentally the same thing – a way to uniquely identify the system on the network in a human-friendly manner.

    Advertisement
    Linux terminal showing hostname
    This is where the hostname is commonly shown in a Linux terminal – it’s the text right after @ and your username in the prompt.

    When I say unique, I don’t mean in the sense like an IP address or MAC address. You can easily have two systems called “webserver” on the same network. But it is good to make these unique so that you can easily identify the systems. Additionally, hostnames and DNS go hand in hand. If you have two “webserver” machines within the same DNS zone, your requests could go to either one and you won’t easily know. By setting a unique hostname, you know exactly where your requests are going.

    Why change a hostname?

    Sometimes you might miss where the hostname is set during system installation and you end up with a hostname like “localhost.localhost” or some installers might not even give you the option to set a hostname and will randomly generate one for you. Or maybe you just mistyped or you have changed a domain name.

    Changing a hostname is actually very easy on Linux with the hostnamectl and hostname commands.

    How to change your hostname

    First, we’re going to cover using hostnamectl which is used on modern systems with systemd. Let’s go ahead and change your hostname! To get started, check your current hostname:


    user@system1$ hostnamectl

    You should see something like the following:


    Static hostname: system1.example.com
    Icon name: computer-vm
    Chassis: vm
    Machine ID: 8bde47de792244a1bcb2b77e05a76545
    Boot ID: 698e3ddfc67441f6a7dea1e2a6365348
    Virtualization: kvm
    Operating System: CentOS Linux 7 (Core)
    CPE OS Name: cpe:/o:centos:centos:7
    Kernel: Linux 3.10.0-1160.53.1.el7.x86_64
    Architecture: x86-64

    This tells us that the computer hostname is system1.example.com.

    To update our hostname, run the following:


    user@computer$ hostnamectl set-hostname ‘web-server-01.example.com’

    Note: You won’t see your terminal prompt update unless you log out and log back in.

    Next, update your hosts file. This is good practice because it ensures that the server is getting its own hostname from itself. Open your hosts file:


    user@computer$ sudo vi /etc/hosts

    To edit a file in vi, press the I key. Use your arrow keys to move the cursor and make your changes. Once you’re done, press the ESC key and then type :wq! and press enter. This tells vi to write the changes to the file and quit. It’s best to learn the basics of vi because not every system ships with nano and you may find yourself on an air-gapped system or a system where you do not have sudo permission to download it.

    You should see two lines like the following:

    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

    Update the lines to include your new hostname at the end:

    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 web-server-01 web-server-01.example.com
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 web-server-01 web-server-01.example.com

    Save the file and you’re done. Reboot your system to be sure the hostname properly applies and no programs are caching it.

    What if you have an older system or one that doesn’t use systemd? You’ll use the hostname command. This command requires some additional work, but it’s not too bad. First, check your current hostname by typing:


    user@computer$ hostname

    You’ll see your hostname returned. For example:


    system1.example.com

    Now, update your hostname.

    Red Hat/Fedora/CentOS systems:


    user@computer$ sudo vi /etc/sysconfig/network

    and then find the HOSTNAME line and change it to your new hostname:

    HOSTNAME=web-server-01.example.com

    Save the file and exit.

    Debian/Ubuntu systems:


    user@computer$ sudo vi /etc/hostname

    Delete your old hostname and replace it with your new hostname.

    web-server-01.example.com

    Save the file and exit.

    Now, exit the system hosts file, find all instances of your old hostname and update them accordingly. Save the file and exit. You may need to reboot your system for programs to pick up the new hostname.

    facebookShare on Facebook
    TwitterTweet
    FollowFollow us

    Advertisement
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleConfiguring a static IP on AlmaLinux
    Next Article Bash Scripting Introduction
    Travis

    I am a Cloud Engineer currently working in the financial and web hosting industry. My passion has been Linux since I first discovered it in 2005. Throughout my career, I have always implemented Linux wherever possible and am an advocate of using open source software over proprietary whenever possible. I strongly believe in the UNIX philosophy of "Do One Thing and Do It Well". I started The Linux Lab as a way for people to discover Linux (and UNIX in general, even macOS users) and to find solutions to problems and learn about Linux.

    Related Posts

    Bash Scripting Introduction

    Configuring a static IP on AlmaLinux

    How to migrate from CentOS 8 to a supported distro

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Buy Me a Coffee

    Recent Posts
    • Bash Scripting Introduction
    • How to change your hostname
    • Configuring a static IP on AlmaLinux
    • How to migrate from CentOS 8 to a supported distro
    • Hands on with Rocky Linux 8.3 RC
    Recent Comments
    • Travis Newton on Zabbix 4.4 on CentOS 8 Installation
    • Gulman on Zabbix 4.4 on CentOS 8 Installation
    • Sam Andrews on SSH Clients – Which is best?
    • gila on Zabbix 4.4 on CentOS 8 Installation
    • Alex on Zabbix 4.4 on CentOS 8 Installation
    Categories
    • AlmaLinux
    • Bash Scripting
    • CentOS
    • Red Hat
    • Rocky Linux
    • Security
    • Software
    • SSH
    • Tutorials
    • Ubuntu
    • VPN
    • Zabbix
    Copyright © 2022. The Linux Lab.

    Type above and press Enter to search. Press Esc to cancel.

    This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
    Privacy & Cookies Policy

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
    Non-necessary
    Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
    SAVE & ACCEPT