Quick tutorial for those who want to change the Hostname (Computer name) in Ubuntu 16.04 Server or Desktop without a restart.
1. To change hostname temporarily so it works until you reboot the machine:
In terminal / command console, run the command:
sudo hostname NEW_NAME_HERE
2. To permanently change your computer name, you may edit the “/etc/hostname” and “/etc/hosts” files.
Open terminal and run command:
gksudo gedit /etc/hostname /etc/hosts
You may need to install gksu first via command sudo apt install gksu
.
For Ubuntu Server, use two nano
commands instead (Press Ctrl+X, followed by Y, and then Enter to save changes):
sudo nano /etc/hostname sudo nano /etc/hosts
When the files open, set the new hostname:
- /etc/hostname is a simple one line file, change the name to whatever your want.
- /etc/hosts maps IP addresses to host names, change the name in second line and make it SAME to the name in /etc/hostname.
Without restarting your machine, just run the command below to restart hostname service to apply changes:
systemctl restart systemd-logind.service
3. There’s also hostnamectl command with set-hostname flag, which will alter the pretty, the static, and the transient hostname alike:
hostnamectl set-hostname NEW_NAME_HERE
ISSUE: After changing hostname, gedit and other graphical apps use X11/Mir protocol won’t launch from terminal via gksudo
, you’ll get something like below:
Failed to connect to Mir: Failed to connect to server socket: No such file or directory
Unable to init server: Could not connect: Connection refused(gedit:20511): Gtk-WARNING **: cannot open display: :0
To fix it:
run xauth list
command to check authentication cookie.
add a matching cookie for the new hostname:
xauth add "NEW_HOSTNAME/unix:0" MIT-MAGIC-COOKIE-1 cookie-id-here
Hi Ji,
following command not working to take effect of changed hostname :
systemctl restart systemd-logind.service
Can you please update?
Thanks
@Ravi
Hi, it is the right command. If you’re not root you’ll need to use it with sudo though. Like so:.
# sudo systemctl restart systemd-logind.service
@Ji
Thanks for the nice tutorial. Clear & concise
Why they decided to change a simple administrative task, which should need only one command, to be so awkward?
the hostname manpage has no indicator whatsoever that it’s effects are only temporary. Who needs this anyway?
Thanks for your article.
Hi ji m,
Thanks for your very clear and concise instructions.
Worked OK first time.
I have seen many of your articles and used quite a few. All without problems for me, so its time I made a comment.
Keep up your good work.
Cheers
I followed the 2 step method and it worked perfectly. Thanks!
It changed the hostname but after rebooting, I am unable to login.