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