Configure HostName on Ubuntu Server
you set a new hostname with
sudo hostnamectl set-hostname new_hostname
You can also do so by editing BOTH /etc/hostname
and /etc/hosts` . Direct editing is discouraged because if you do not edit both you can break sudo.
panther@Ubuntu:~$hostname Ubuntu panther@Ubuntu:~$sudo hostnamectl set-hostname it_works
panther@Ubuntu:~$hostname
it_works
panther@Ubuntu:~$ping -c1 it_works
PING it_works (10.0.0.8) 56(84) bytes of data. 64 bytes from it_works (10.0.0.8): icmp_seq=1 ttl=64 time=0.020 ms
--- it_works ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.020/0.020/0.020/0.000 ms
panther@Ubuntu:~$sudo hostnamectl set-hostname Ubuntu
panther@Ubuntu:~$hostname
Ubuntu
riki
Updated on September 18, 2022Comments
-
riki over 1 year
then I'm using an ubuntu server with gitlab installed, I need to configure hostname the name ubuntudev, to access it from within the network! I configure the ubuntudev name in /etc/hosts but it does not work with the hostname, how can I fix this error? I put under the contents of the /etc/hosts file
--> /etc/hosts
127.0.0.1 localhost 127.0.1.1 ubuntudev # The following lines are desirable for IPv6 capable hosts ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters
-
guiverc over 6 yearsif you try the command
ping ubuntudev
you'll be pinging 127.0.1.1 so you'll likely find what you did worked, it just didn't do what you intended./etc/hosts
preceeded the dhcp protocol (ie. arpanet days) and is only a list of ip-addresses and names to access those ip.addresses. I think what you wanted to change was/etc/hostname
-
riki over 6 years@guiverc I have already added what you also say that, but still do not go
-
guiverc over 6 yearschanging the file does nothing until you reboot, or restart the required..... (the file is only read during init type processes then is accessed from memory -- you have to force re-read) note: I don't use gitlab, so do not know it significance to your issues..
-
riki over 6 years@guiverc does not respond to ping
-
riki over 6 years@guiverc I've already tried to rewind it, but the result is the same!
-
derHugo over 6 yearsWhich server is your DNS nameserver in this network? Is it the same server which's hostname you are changing?
-
riki over 6 yearsyes it's same..
-
Panther over 6 years@guiverc reboot is not required, you can set a hostname with
hostname
but. IMHO,hostnamectl
is preferred
-
-
riki over 6 yearsIt’s not works....
-
Panther over 6 yearsIf you need help, update your question with the commands you ran and any error messages you get. Both methods works to set hostname.
-
CodingInTheUK over 4 yearsset-hostname doesnt affect the hosts file. (ubuntu 18.04) you need to edit that manually after you run the update command.