how to change hostname in linux
Modifying the Linux host name requires step-by-step operation to ensure that it takes effect. First, check the current hostname or hostnamectl commands available; second, temporarily modify the sudo hostname new-hostname command, but it fails after restart; permanent modification requires editing the /etc/hostname file and updating the hostname in /etc/hosts. Some systems can use sudo hostnamectl set-hostname new-hostname to refresh the settings; finally verify the modification results through hostname, hostnamectl and cat /etc/hostname to ensure synchronous updates to avoid errors after restart.

Changing the Linux host name is actually not difficult, but many people are prone to making mistakes in the first operation or the change does not take effect. In fact, it can be done in just a few simple steps, and the key is to use the right method.

View the current host name
Before changing, it is best to confirm what the current host name is.
You can run this command directly:
hostname
Or more intuitive, use:

hostnamectl
This command is particularly useful on systemd-based systems (such as CentOS 7 /Ubuntu 16.04). It will display the current host name, operating system version, kernel information, etc.
Temporarily modify the host name (invalid after restart)
If you just want to test it, or change the alias temporarily, you can use this command:

sudo hostname new-hostname
Just change new-hostname to the name you want.
The advantage of this method is that it takes effect immediately, and the disadvantage is that it will be restored to its original state after restarting. Suitable for temporary debugging.
Permanently modify the host name (recommended method)
To permanently change the hostname, two things need to be done:
- Modify the configuration file
- Restart or refresh the hostname service
Modify /etc/hostname
Open this file:
sudo nano /etc/hostname
Delete the original name inside and replace it with a new one. Save and exit.
Update /etc/hosts
This step is often overlooked, but it is very important.
Open the /etc/hosts file:
sudo nano /etc/hosts
Found a line like this:
127.0.1.1 old-hostname
Change old-hostname to a new name.
Refresh settings (optional)
Some systems will not be automatically updated after modification. You can use the following command to let the system reread the host name:
sudo hostnamectl set-hostname new-hostname
This will take effect without restarting.
Verify that the modification is successful
Run these commands to check:
-
hostname -
hostnamectl -
cat /etc/hostname
If all the new name you set is displayed, you will basically be done.
Although the hostname looks like a small thing, it is important for server management, network communication and logging. Don't rush to type the command when changing. Remember to update the configuration file synchronously, otherwise there may be problems after restarting. Basically all is it, not complicated but it is easy to ignore details.
The above is the detailed content of how to change hostname in linux. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Clothoff.io
AI clothes remover
Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
How to install software on Linux using the terminal?
Aug 02, 2025 pm 12:58 PM
There are three main ways to install software on Linux: 1. Use a package manager, such as apt, dnf or pacman, and then execute the install command after updating the source, such as sudoaptininstallcurl; 2. For .deb or .rpm files, use dpkg or rpm commands to install, and repair dependencies when needed; 3. Use snap or flatpak to install applications across platforms, such as sudosnapinstall software name, which is suitable for users who are pursuing version updates. It is recommended to use the system's own package manager for better compatibility and performance.
The Ultimate Guide to High-Performance Gaming on Linux
Aug 03, 2025 am 05:51 AM
ChoosePop!_OS,Ubuntu,NobaraLinux,orArchLinuxforoptimalgamingperformancewithminimaloverhead.2.InstallofficialNVIDIAproprietarydriversforNVIDIAGPUs,ensureup-to-dateMesaandkernelversionsforAMDandIntelGPUs.3.EnabletheperformanceCPUgovernor,usealow-latenc
What are the main pros and cons of Linux vs. Windows?
Aug 03, 2025 am 02:56 AM
Linux is suitable for old hardware, has high security and is customizable, but has weak software compatibility; Windows software is rich and easy to use, but has high resource utilization. 1. In terms of performance, Linux is lightweight and efficient, suitable for old devices; Windows has high hardware requirements. 2. In terms of software, Windows has wider compatibility, especially professional tools and games; Linux needs to use tools to run some software. 3. In terms of security, Linux permission management is stricter and updates are convenient; although Windows is protected, it is still vulnerable to attacks. 4. In terms of difficulty of use, the Linux learning curve is steep; Windows operation is intuitive. Choose according to requirements: choose Linux with performance and security, and choose Windows with compatibility and ease of use.
Understanding RAID Configurations on a Linux Server
Aug 05, 2025 am 11:50 AM
RAIDimprovesstorageperformanceandreliabilityonLinuxserversthroughvariousconfigurations;RAID0offersspeedbutnoredundancy;RAID1providesmirroringforcriticaldatawith50�pacityloss;RAID5supportssingle-drivefailuretoleranceusingparityandrequiresatleastthre
Linux how to enable and disable services at boot
Aug 08, 2025 am 10:23 AM
To manage the startup of Linux services, use the systemctl command. 1. Check the service status: systemctlstatus can check whether the service is running, enabled or disabled. 2. Enable the service startup: sudosystemctlenable, such as sudosystemctlenablenginx. If it is started at the same time, use sudosystemctlenable--nownginx. 3. Disable the service startup: sudosystemctldisable, such as sudosystemctldisablecups. If it is stopped at the same time, use sudosystemctldisabl
Linux how to list all running processes
Aug 08, 2025 am 06:42 AM
Usepsauxforacompletesnapshotofallrunningprocesses,showingdetailedinformationlikeUSER,PID,CPU,andmemoryusage.2.Usetoporhtopforreal-timemonitoringofprocesseswithdynamicupdates,wherehtopoffersamoreintuitiveinterface.3.UsepgreporpidoftoquicklyfindthePIDs
How to clean up your Linux system
Aug 22, 2025 am 07:42 AM
Removeunusedpackagesanddependencieswithsudoaptautoremove,cleanpackagecacheusingsudoaptcleanorautoclean,andremoveoldkernelsviasudoaptautoremove--purge.2.Clearsystemlogswithsudojournalctl--vacuum-time=7d,deletearchivedlogsin/var/log,andempty/tmpand/var
Linux how to view the contents of a file
Aug 19, 2025 pm 06:44 PM
ToviewfilecontentsinLinux,usedifferentcommandsbasedonyourneeds:1.Forsmallfiles,usecattodisplaytheentirecontentatonce,withcat-ntoshowlinenumbers.2.Forlargefiles,uselesstoscrollpagebypageorlinebyline,searchwith/search_term,andquitwithq.3.Usemoreforbasi


