An IP address is a unique identification number used to identify a network device on the network. Before a system, client, or server can communicate to other devices over the network, an IP address must be configured on both the devices. We assume that you know the basics of TCP/IP addressing. Here, we will just go through the steps used to set IP address in Ubuntu server using command line tools.
Also read: How to set IP address in RHEL/CentOS Linux
On an Ubuntu Linux server, you can set an IP address using the following three methods:
- Setting Temporary IP Address in Ubuntu
- Setting Dynamic IP Address Using DHCP
- Setting Static IP Address (Manually)
Note: Here, we will not cover the GUI method to set IP address and other TCP/IP settings on a Ubuntu server.
1. Set Temporary IP Address In Ubuntu
Sometimes you may need to assign a temporary IP address on your server. You can configure temporary IP address on Ubuntu using the ifconfig command. However, once you reboot the server, the IP address would no longer be available.
To configure tempory IP address, use the following command syntax:
sudo ifconfig <interface> <ipv4address> netmask <subnetmask>
For example, the following command will set 172.16.0.1 IPv4 address with 255.255.0.0 as subnet mask on the ens38 interface.
sudo ifconfig ens38 172.16.0.1 netmask 255.255.0.0
Optionally, you can also set temporary default gateway using the route command. For example, to set 172.16.0.255 as the default gateway for the ens38 interface, use the following command.
sudo route add default gw 172.16.0.255 ens38
To verify that IP address is assigned, use the following command:
The following figure shows how to set temporary IP address and gateway in Ubuntu server.
Removing Temporary IP Settings
If you no longer need to use temporary IP address or want to assign a different temporary IP address, you can flush the current IP settings using the following command.
ip addr flush eth0
2. Set Dynamic IP Address In Ubuntu Using DHCP
DHCP is a service that provides TCP/IP settings such as IP address, subnet mask, and DNS server settings automatically to the clients. If your network has already a configured DHCP server, you can assign a dynamic IP address to your Ubuntu server. For this, you need to edit the network configuration file. In Ubuntu, it is /etc/network/interfaces.
sudo vi /etc/network/interfaces
Update the following value in the above file.
auto eth0 iface eth0 inet dhcp
After updating the network configuration file, you need to stop and start network interface to take effect. To do so, execute the following commands:
sudo ifdown eth0 sudo ifup eth0
The following figure shows how to assign IP address in Ubuntu Linux using DHCP server.
Finally, verify the IP address assigned by DHCP server using the ifconfig eth0 command.
3. Set Static IP Address In Ubuntu
If the above methods do not work or not fulfill your requirement, you can set static IP address in Ubuntu and other Linux variants. To configure static IP address in Ubuntu using the command line, edit the /etc/network/interfaces file as follows:
auto ens38 iface ens38 inet static address <ip-address> netmask <subnet-mask> gateway <gateway>
For example, to set 172.16.0.1/16 IP address with 172.16.0.255 as gateway address, the file should look like as follow:
auto ens38 iface ens38 inet static address 172.16.0.1 netmask 255.255.0.0 gateway 172.16.0.255
The following figure shows how to set static IP address in Ubuntu.
Save the network configuration file. Stop and start the network interface using the below commands.
sudo ifdown eth0 sudo ifup eth0
Finally, verify the IP address and other settings that you have set on your Ubuntu server.
In this tutorial, we have explained how to set IP address in Ubuntu using different methods. Hope, you have enjoyed and loved it. Do subscribe us for more upcoming articles.