3 Ways To Setup Static IP Address in Ubuntu.
Assigning static IP addresses to each network device can be cumbersome. We advise you to do this on your router as it makes things easier. However, assigning a static IP address at the device level will override the router settings. Moreover, there are times when you do not have access to the router. For these scenarios, we have a detailed article on how to assign a static IP address to various devices. We’ll take a look at Ubuntu separately as it has so much more complexity.
So here are 3 ways to set static IP in Ubuntu or any Unix based OS.
1. Setup Ubuntu static IP address using the GUI
Here we’ll be using Ubuntu for the demonstration. But most of the configuration and settings will be similar to any Unix-based OS.
So, there are several ways to deal with the network manager, the simplest of which is the graphical interface. If you are new to Ubuntu and don’t like the command line, I would suggest changing it through the GUI. First, we’ll need a subnet mask before we start setting up a static IP address. To get these values, right-click anywhere on the desktop to open a terminal.
When the terminal window opens, we need to run the following command. ifconfig -a
Make a note of the netmask and inet value that we will need in the next steps. In my case, the inet value is 10.0.2.15 and the netmask is 255.0.0.0.
After you have written down the subnet mask, we also need the gateway address. This is usually the first address on the network, but to be sure, let’s just check it. Run the following command on the terminal. route -n
This command will show you the IP routing table. The first entry in the table will tell you the gateway address. In my case, the first entry is 10.0.0.1, which in turn is the gateway address.
It’s pretty simple now. Open your network settings. To do this, click the network icon in the upper right corner of your desktop.
Now go to the active network. I am connected via LAN cable, so for me it will be Ethernet. In case you are connected to Wi-Fi, you should get the same in the tray. Make sure you click on the active network and expand the menu to see more options. Click Wired Network Settings in the Advanced Menu to open Network Settings.
When you open the Network Settings window, make sure you are on the Networking tab. Click the Options icon next to the active network to open the adapter properties.
When the wired network settings popup opens, go to the IPv4 tab to change the IP settings.
On the IPv4 tab, select the radio button labeled “Manual”. A new window will appear with 3 text fields – Address, Netmask and Gateway. Now, before applying the changes, we need to understand what these values ​​are.
The IP value you set must be in the range of your previous IP address. For example, if your IP address was 10.0.0.2, your static IP should be in the range 10.0.0.1 – 10.255.255.255. Likewise, if it is 192.168.16.1 then your static IP address should be in the range 192.168.16.1-192.168.16.255. If you enter values ​​outside the range of IP addresses, you may have problems connecting to the Internet.
A netmask or subnet mask is used to divide a network into subnets. In simple terms, the subnet mask determines the number of IP addresses allocated on the network. As I mentioned at the beginning of this tutorial, my subnet mask is 255.0.0.0.
Finally, the gateway is the IP address of the router. This is usually the first IP address allocated for the network, or you can find out using the route – n command as we did above. In my case, this is gateway 10.0.0.1.
Once you have all these values, you need to enter the IP address, netmask and gateway address that we wrote down earlier. After confirmation, you can click the “Apply” button to register the changes.
You will need to restart the network manager again for the changes to take effect. You can do it manually via the command line by simply using the following command sudo service network-manager restart
Alternatively, you can simply turn the network adapter off and on. Then run ifconfig on a terminal to check the IP address. This must be the same IP address that you specified in your IPv4 settings.
2. Setup Ubuntu static IP address via command line
The fastest way to do this is through the command line. If you are good with commands, then this method should help. However, to set a static IP through the terminal, we need the subnet mask, as before, as well as the adapter name. Run ifconfig -a to get these values.
After receiving the values, run the following command. sudo ifconfig enp0s3 10.0.0.3 netmask 255.0.0.0
In this command eng0s3 should be replaced with the name of your adapter; 10.0.0.3 at your static IP followed by a netmask.
When you run the following command, the entry will not be written to the config file. It is not standard practice if you are managing the server.
3. Set Ubuntu static IP address via configuration file
The problem with all the above methods is that you don’t have any documented stuff. It is absolutely not recommended to make changes in this way on the server. The recommended way is to edit the config file. This way, all changes are documented and you can simply undo the previous change by commenting out the lines.
The configuration file is / etc / network / interface. You need administrator rights to edit this file. But first, we need a lot of values, such as subnet mask, gateway address, broadcast address, and adapter name. Just run ifconfig -a on a terminal to get the adapter name, subnet mask, and broadcast address values.
For the gateway address, run the following command on the terminal. route -n
This command will show you the IP routing table. The first entry in the table will tell you the gateway address. In my case, the first entry is 10.0.0.1, which in turn is the gateway address.
Now we need to open the config file to make changes. To do this, run the following command. Sudo vi / etc / network / interface
If you are not familiar with vi editor and controls, you can also use gedit. gedit is a graphical interface for editing files. To open the file in gedit run the following command sudo gedit / etc / network / interface
After opening the file, it should be empty or have previous configurations. If you have the configurations defined earlier, just comment them out instead of deleting them. Prefix all lines with “#”, and now the lines will become useless. Now we need the adapter name, subnet mask, and broadcast address that we wrote down earlier.
Press “i” if you are using vi to enter insert mode. Enter the following code and replace the values ​​accordingly with your own values.
auto enp0s3
iface enp0s3 inet static
address 10.0.0.6
netmask 255.255.255.0
net 10.0.0.1
broadcast 10.0.0.255
gateway 10.0.0.1
But before entering these lines, you need to understand the code. “Auto enp0s3” will automatically restart the interface when the system boots. Iface enp0s3 is a suffix to define the enp0s3 interface. “Inet” refers to IPv4 and “inet static” refers to specifying a static IP address for the specified network adapter. The rest of the lines speak for themselves.
If you want to revert to DHCP, you can change it to “inet dhcp”.
After successfully adding the code, press Esc and enter “: wq” to save your changes. Now we need to restart the network for the changes to take effect. Run the following command to restart the network adapter. sudo service network-manager restart
After a reboot, the changes will take effect. To see the changes, run ifconfig again and view the following values. It must match the values ​​that you defined in the config file.
If you want to make changes, comment out the previous lines and add the changes.
Wrap-around: Set up a static IP address in Ubuntu
If you are on a home network, the recommended method is to define static IP addresses through a router. In case of exceptions, you can try assigning a static IP address in Ubuntu through the GUI or command line. If you are managing a server, follow the guidelines and define a static IP address in the configuration file.
Once you have defined a static IP address, your device should use the specified IP address and override the changes made at the router level. Alternatively, you can try user management in Ubuntu, or try changing the GRUB 2 splash screen in Ubuntu 12.04.
If you have any problems or questions, write about it in the comments, and I will contact you.