Announcement

Collapse
No announcement yet.

How to change the hostname on Ubuntu without a restart?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to change the hostname on Ubuntu without a restart?

    I want to change the OS hostname without restart. I have edited /etc/hostname but it needs a restart to get implemented.

    How to do this?


  • #2
    Use the following command in a terminal:
    Code:
    sudo hostname new-name 
    It will set the hostname to new-name till you restart.

    After a restart your changes in /etc/hostname will be used, so you have still use
    Code:
    sudo -H gedit /etc/hostname
    (or some other editor) so that file contains the hostname.

    You need also edit /etc/hosts and change the line which reads:
    Code:
    127.0.1.1     your-old-hostname

    Comment


    • #3
      Thanks for your reply Arthur.

      But I don't want to restart.

      Comment


      • #4
        I know. I simply needed to clear up, that the "hostname" command does just change the hostname until you restart/crash/ etc. A short time later it will read the name from the document again.

        Comment


        • #5
          Thanks. That means we have to restart after changing the hostname. Right?

          Comment


          • #6
            Depends. If you entered the command you don't have to restart. The hostname is changed as of now. Be that as it may, just UNTIL your next restart.

            Comment


            • #7
              Note that you also need to change the /etc/hosts

              Comment


              • #8
                As Ubuntu 13.10 you need to use hostnamectl:

                sudo hostnamectl set-hostname 'new-hostname'
                The benefits are many with one command to make all those changes sinking glitches with typos or sudo (you can simply break sudo if you make errors) and no reboot is required.

                Comment


                • #9
                  How can I restore original host name without restart?

                  Comment


                  • #10
                    You have to run the following command in a terminal.
                    sudo hostname your-original-hostname

                    Comment


                    • #11
                      I think this is not available in all versions. Just observed on my Ubuntu 14.04 LTS ARM-7 installation and hostnamectl does not exist. Guess some versions of Ubuntu are not using systemd yet.

                      Comment


                      • #12
                        Originally posted by Stephanie View Post
                        I think this is not available in all versions. Just observed on my Ubuntu 14.04 LTS ARM-7 installation and hostnamectl does not exist. Guess some versions of Ubuntu are not using systemd yet.
                        The commands are part of the systemd group, but they are existing individually. In this case, don't use systemd as init, by the command is existing.

                        Comment


                        • #13
                          Try the following script that changes the hostname in the recommended way. It ensures that not only sudo but also X11 applications continue to function with no restart necessary.

                          Usage: sudo ./change_hostname.sh new-hostname

                          Code:
                          #!/usr/bin/env bash
                          NEW_HOSTNAME=$1
                          echo $NEW_HOSTNAME > /proc/sys/kernel/hostname
                          sed -i 's/127.0.1.1.*/127.0.1.1\t'"$NEW_HOSTNAME"'/g' /etc/hosts
                          echo $NEW_HOSTNAME > /etc/hostname
                          service hostname start
                          su $SUDO_USER -c "xauth add $(xauth list | sed 's/^.*\//'"$NEW_HOSTNAME"'\//g' | awk 'NR==1 {s

                          Comment


                          • #14
                            Hi Elliott,
                            Just curious. What does the last line in the script do?
                            su $SUDO_USER -c "xauth add $(xauth list | sed 's/^.*\//'"$NEW_HOSTNAME"'\//g' | awk 'NR==1 {sub($1,""&""); print}')"

                            Comment


                            • #15
                              It takes a correct X11 authentication token (xauth list) and replaces the old hostname with the new hostname (sed). At that time awk puts quotes around the first argument to xauth add since xauth's input and output format are not symmetric.

                              Comment

                              Working...
                              X