Configure HostName on Ubuntu Server

6,598

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

Share:
6,598
riki
Author by

riki

Updated on September 18, 2022

Comments

  • riki
    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
      guiverc over 6 years
      if 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
      riki over 6 years
      @guiverc I have already added what you also say that, but still do not go
    • guiverc
      guiverc over 6 years
      changing 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
      riki over 6 years
      @guiverc does not respond to ping
    • riki
      riki over 6 years
      @guiverc I've already tried to rewind it, but the result is the same!
    • derHugo
      derHugo over 6 years
      Which server is your DNS nameserver in this network? Is it the same server which's hostname you are changing?
    • riki
      riki over 6 years
      yes it's same..
    • Panther
      Panther over 6 years
      @guiverc reboot is not required, you can set a hostname with hostname but. IMHO, hostnamectl is preferred
  • riki
    riki over 6 years
    It’s not works....
  • Panther
    Panther over 6 years
    If you need help, update your question with the commands you ran and any error messages you get. Both methods works to set hostname.
  • CodingInTheUK
    CodingInTheUK over 4 years
    set-hostname doesnt affect the hosts file. (ubuntu 18.04) you need to edit that manually after you run the update command.