How To Change Hostname & Static IP in Ubuntu Server 16.04
We know that there are two classes of IP addresses that we have to take into account to understand the configuration of our network:
Dynamic IP addresses
This type of address changes regularly due to the renewal of the concession that exists in the setting of the DHCP (Dynamic Host Configuration Protocol) and with this addressing, every IP address has a limit of use before being renewed
Static IP addresses
This type of address remains fixed and can only be modified manually by the administrator.
This tutorial will analyze how we can assign a static IP address to Ubuntu Server in a simple way.
Know the name of the Ubuntu Server Network Adapter
First of all, it is vital that we have clear what is the name that has been assigned to our network adapter, for this we will execute the following command using the preferred editor (vi or nano).
Ubuntu Server by default establishes DHCP addressing on the computer, in this case, we can see that our adapter is called enp0s3.
sudo nano /etc/network/interfaces
We will use the ifconfig command to validate the current IP address of Ubuntu Server which will display information such as IP address, Gateway, network mask, etc.:
Assign Static IP Address in Ubuntu Server
To set the static IP address in Ubuntu Server 2016 we must go back to the sudo nano path /etc/ network/ interfaces to add some lines in the server interface; we will add the following:
auto enp0s3 iface enp0s3 inet static address 192.168.0.15 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1 dns-nameservers 192.168.0.1
In this configuration we have added the following parameters:
- Editing in the line iface enp0s3 inet of the line dhcp to static so that the IP address is established as static.
- Added the new IP address (Address field).
- Added IP network, Gateway and broadcast addresses.
- Assignment of the network mask.
- DNS IP address which is provided by the provider or if we wish it is possible to add a public DND like the one offered by Google (188.8.131.52 / 184.108.40.206).
Once these values are defined, we save the changes using the key combination Ctrl + O, and we left the editor using Ctrl + X.
Now we must update the resolv.conf file with the IP address assigned in the DNS and for that we go the following route:
sudo nano /etc/resolv.conf
There we will enter the DNS IP in the first-row nameserver, save the changes using the following keys Ctrl + O and exit the editor.
The next step is to remove the DHCP client using the following line:
sudo apt-get remove dhcp-client
Finally, we restart the network components using the following command:
sudo /etc/init.d/networking restart
Validate Connectivity in Ubuntu Server
Once we have completed the process, the next step is to ping a website to validate connectivity in the network:
We can see that everything is correct. If we use the ifconfig command again, we can see that the IP address is updated with the one we have assigned.
Modify the hostname in Ubuntu Server
Assigning a specific name to the team gives us the possibility of having much more precise control over it since its identification will be much better.
To know the current name of the hostname, we will use the following command.
For its modification we have two options:
Modify the name with a system reboot
With this option, it will be necessary to restart the system. To do this, we will first access the hostname file using the preferred editor:
sudo nano /etc/hostname
There it will be required to delete the current name of the equipment and enter the new name.
We save the changes using the key combination Ctrl + O, and we left the editor using Ctrl + X.
Now we will access the hosts file using the desired editor:
sudo nano /etc/hosts
There it will be necessary to change the name of the current server to the new one in the lines where it appears.
We save the changes and leave the editor. Now all that remains is to restart the system using the sudo reboot command.
Modify the name without restarting the system
If we do not want to reboot the operating system, we will follow the following process. We will execute the following syntax:
sudo hostname (new name)
Now we must merely access the sudo nano /etc/hostname and sudo nano/etc/hosts routes and modify the new name there.