Configure server IP address & DNS
Published on: 22nd Apr 2014
Updated on: 30th Jan 2022
Why
In cloud hosting, the public IP address will be assigned to your server upon creating a new instance. In this case, you may skip this section.
But, if you are installing an Ubuntu server in your office or home, then, you may want to configure the server IP address and DNS.
Here's how you configure it
During the installation, I chose the DHCP option to configure the server IP address. After installation is completed, assign a static IP address.
Starting from Ubuntu version 16.04, you may use netplan
to handle the network configuration.
-
First is to find out the configuration file and mine is 50-cloud-init.yaml.
ls /etc/netplan
-
Then, edit the file with nano. The configuration should look like this (as per our server).
network: version: 2 ethernets: eth0 : dhcp4: no addresses: - 192.168.1.3/24 gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4]
Notes: on YAML configuration file, no tab key is allowed. All indentation must be done with space key (usually 2 spaces).
-
After updated the configuration, save the changes and run the following command,
sudo netplan apply
The old way in the older Ubuntu version
-
In the command console,
sudo nano /etc/network/interfaces
-
Then, add these settings,
iface eth0 inet static address 192.168.1.3 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8 8.8.4.4
Press CTRL+X to confirm saving the changes and exit.
-
Install the
ifupdown
component,sudo apt install ifupdown
-
After that restart the networking service by running the following commands,
sudo ifdown -a sudo ifup -a
Operation notes
-
To view the dynamic IP address assigned by DHCP server,
ip addr | grep inet
-
To get the network settings,
ip addr
-
To get the IP address, DNS and gateway for the dynamic IP, type the following command. Please take note that you have to replace "enp0s3" (interface ID) in the following command. Notes: use
ip addr
command to find out the interface ID.netplan ip leases enp0s3
-
If you want to find out just the DNS server,
systemd-resolve --status
-
To find out the gateway,
ip route
-
Sample output: below is my test server. It's IP address is '192.168.1.53' and the gateway is '192.168.1.1'.
default via 192.168.1.1 dev enp0s3 proto dhcp src 192.168.1.53 metric 100 192.168.1.0/24 dev enp0s3 proto kernel scope link src 192.168.1.53 192.168.1.1 dev enp0s3 proto dhcp scope link src 192.168.1.53 metric 100
Reference
-
Set a Static IP: http://www.howtoforge.com/linux-basics-set-a-static-ip-on-ubuntu
-
How to Configure Static IP Address on Ubuntu: https://linuxize.com/post/how-to-configure-static-ip-address-on-ubuntu-18-04/
-
netplan
frequently asked questions: https://netplan.io/faq/
Related posts
Jump to #UBUNTU blog
Author
Lau Hon Wan, software developer.