Change server name linux

How to change your hostname in Linux

Image by Pixabay

More Linux resources

Your hostname is a vital piece of system information that you need to keep track of as a system administrator. Hostnames are the designations by which we separate systems into easily recognizable assets. This information is especially important to make a note of when working on a remotely managed system. I have experienced multiple instances of companies changing the hostnames or IPs of storage servers and then wondering why their data replication broke. There are many ways to change your hostname in Linux; however, in this article, I’ll focus on changing your name as viewed by the network (specifically in Red Hat Enterprise Linux and Fedora).

Background

A quick bit of background. Before the invention of DNS, your computer’s hostname was managed through the HOSTS file located at /etc/hosts . Anytime that a new computer was connected to your local network, all other computers on the network needed to add the new machine into the /etc/hosts file in order to communicate over the network. As this method did not scale with the transition into the world wide web era, DNS was a clear way forward. With DNS configured, your systems are smart enough to translate unique IPs into hostnames and back again, ensuring that there is little confusion in web communications.

Modern Linux systems have three different types of hostnames configured. To minimize confusion, I list them here and provide basic information on each as well as a personal best practice:

  • Transient hostname: How the network views your system.
  • Static hostname: Set by the kernel.
  • Pretty hostname: The user-defined hostname.

It is recommended to pick a pretty hostname that is unique and not easily confused with other systems. Allow the transient and static names to be variations on the pretty, and you will be good to go in most circumstances.

Working with hostnames

Now, let’s look at how to view your current hostname. The most basic command used to see this information is hostname -f . This command displays the system’s fully qualified domain name (FQDN). To relate back to the three types of hostnames, this is your transient hostname. A better way, at least in terms of the information provided, is to use the systemd command hostnamectl to view your transient hostname and other system information:

Before moving on from the hostname command, I’ll show you how to use it to change your transient hostname. Using hostname (where x is the new hostname), you can change your network name quickly, but be careful. I once changed the hostname of a customer’s server by accident while trying to view it. That was a small but painful error that I overlooked for several hours. You can see that process below:

Читайте также:  Miracast windows 10 нет звука

It is also possible to use the hostnamectl command to change your hostname. This command, in conjunction with the right flags, can be used to alter all three types of hostnames. As stated previously, for the purposes of this article, our focus is on the transient hostname. The command and its output look something like this:

The final method to look at is the sysctl command. This command allows you to change the kernel parameter for your transient name without having to reboot the system. That method looks something like this:

GNOME tip

Using GNOME, you can go to Settings -> Details to view and change the static and pretty hostnames. See below:

Wrapping up

I hope that you found this information useful as a quick and easy way to manipulate your machine’s network-visible hostname. Remember to always be careful when changing system hostnames, especially in enterprise environments, and to document changes as they are made.

Want to try out Red Hat Enterprise Linux? Download it now for free.

Источник

Linux setting hostname and domain name of my server

How to use hostnamectl command

If you are using systemd based distro such as RHEL/CentOS 7.x, Fedora latest, Ubuntu Linux 16.04 LTS, Debian 9.x+, Arch Linux and more, use hostnamectl command. To see current hostname, run:
$ hostnamectl
To change hostname to foo, run:
$ hostnamectl set-hostname ‘foo’
$ hostnamectl

A note about GUI tools

If you are using Red Hat or Fedora Linux (older version) use redhat-config-network GUI tool. Type following command and click on DNS tab > Setup hostname and domain name:
# redhat-config-network
On other hand you can edit a text file. Find out and set up the value for HOSTNAME in the file /etc/sysconfig/network:
# vi /etc/sysconfig/network
Setup/replace HOSTNAME
HOSTNAME=web.nixcraft.com
Where, web is hostname and nixcraft.com is your DNS domain name.

If you are using Debian Linux

You need to edit /etc/hostname file and put hostname.domain.com in this file:
# vi /etc/hostname
Set or replace hostname:
web.nixcraft.com

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Debian, Suse and other Linux distro support GUI tool called network-admin. Type following command and click on DNS tab > Setup hostname and domain name:
$ network-admin

Please note that you must have properly configured DNS server to work FQDN based hostname. See how to configure Linux as a dns client.

See also:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

thank you bro. it is really helpfull

I think we have to restart the network service after making the changes.

service network restart

Thank you so much for this information. It was very useful for me!

Do you also want to tell in your artice about restarting services so the new changes can come to effect? At the moment the article is half baked.

Thanks a lot from a CentOS newbie.

hi I want to learn linux .what step should i follow?

Step1: Change the New Values in the Host file
[root@erp

]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
192.168.0.1 erp.myappsdba.com erp
[root@erp

Step2: Change the New Values in the network file
[root@erp

]# cat /etc/sysconfig/network
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=erp.myappsdba.com
GATEWAY=192.168.0.3

Step3: Change the New Values in the sysctl.conf and resolv.conf files
[root@erp

]## cat /etc/sysctl.conf
kernel.hostname=erp.myappsdba.com
kernel.domainname=myappsdba.com

[root@erp etc]# cat /etc/resolv.conf
search myappsdba.com
nameserver 192.168.0.7
nameserver 192.168.0.2
option attemts : 5
option timeout :15
[root@erp etc]#

Step4:Ping the Host Name or Changed IP Address.
[root@erp etc]# ping myappsdba.com
PING myappsdba.com (192.168.0.1) 56(84) bytes of data.
64 bytes from fortedc.myappsdba.com (192.168.0.1): icmp_seq=1 ttl=128 time=1.06 ms

Step5:Restart the Network or Reboot the system for effecting the changes
[root@erp

]# service network restart

Thanks for the good article. @Nilesh Thanks dude

Please help me connect to server with a Linux netbook. There is a box for the name of the server… but I don’t know it.

Источник

Linux change my hostname / computer system name

How to display current hostname

Just type hostname command to print the name of the system on screen:
$ hostname
Sample outputs:

Change the Linux hostname

Set hostname to desktop.nixcraft.com:
# hostname desktop.nixcraft.com
# hostname
Sample outputs:

Change hostname permanently on a Debian/Ubuntu Linux

You need to edit a file called /etc/hostname:
# vi /etc/hostname
Set new hostname:

Save and close the file. You need to reboot the system or run any one of the following command:
# /etc/init.d/hostname.sh start
OR
# invoke-rc.d hostname.sh start

Linux change hostname using hostnamectl command ( systemd only)

Most modern Linux distribution comes with systemd. If you are using systemd based distro try hostnamectl command.

Query hostname with hostnamectl

Let us print out current hostname:
$ hostnamectl

Change hostname with hostnamectl

The syntax is as follows to set hostname to ‘viveks-laptop’:
$ hostnamectl set-hostname ‘viveks-laptop’
Sample outputs:

Fig.01: hostnamectl in action on a Linux based system

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Set the deployment environment description

ENVIRONMENT must be a single word without any control characters. One of the following is suggested: “development”, “integration”, “staging”, “production”. The syntax is:
$ hostnamectl set-deployment ENVIRONMENT
$ hostnamectl set-deployment production

How to set the location string for the system, if it is known

The syntax is:
$ hostnamectl set-location LOCATION
$ hostnamectl set-location «NYC Home»
$ hostnamectl set-location «DC 2, right rack, 2nd shelf»

A note about an RHEL (Red hat) / CentOS / Fedora Linux users

If you are using CentOS or Fedora or Redhat (RHEL) Linux, see this FAQ.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Thanks. That’s exactly what I needed!

hostname papa.nixcraft.com —temp changing the hostname
For Permanent
vim /etc/hosts
vim /etc/sysconfig/network

i did as you tell but can’t ping to hostname. My hostname is linuxserver, when i use command ping it appear connect: Network is unreachable.

after changing, type “bash” and the prompt will change to the newest hostname your just changed.

this just doesn’t work, it seems like it does but you wont be able to use “sudo” anymore…..

hostname change command

Hi,
Thanks for pointing me to the right direction.
As for me and perhaps ppl using certain distro, may have to edit the /etc/hosts file as well.
sudo gedit /etc/hosts
127.0.0.1 localhost
127.0.1.1 newhostnamehere
——
Look for the line that says:
127.0.1.1 originalname
and change it to the newhostname

Regards,
Bob
PS: until I fix this, sudo took a long time trying to but fail to “resolve” the name, before asking for password and the GUI ‘update’ taskbar icon and GUI package manager stop working.

The same thing happened to me, thanks for the comment

hi
my need change host name in centos 5.7 ??

Really from my own point of view it is so amazing to use it please i need some e-book how to use it.
Thanks’
Martin

Setting up a CentOS 6 box today and was able to use the hostname command successfully without errors. Not sure why using the other FAQ is suggested.

Источник

Читайте также:  Драйвер для creative под windows
Оцените статью