Automatic IP address change on Ubuntu
Note
The automatic method ensures that the current address remains unchanged. The current dynamic IP address will be set to static.
To change the address:
wget docs.ibatyr.kz/dynamic-to-static-ip.sh
sudo bash dynamic-to-static-ip.sh
If you want to immediately set a specific address to the server, enter this address separated by a space after the script name, for example like this:
sudo bash dynamic-to-static-ip.sh 192.168.1.132
Manually changing IP address on Ubuntu
Warning
Relevant only for version 4.7 and higher
In Ubuntu, the Netplan utility is used to set the server’s static IP address.
1. Install Netplan
apt install netplan
All Netplan settings are performed only as user root. To do this, enter root mode using the sudo su - command and enter the standard user password:
sudo su
2. Disable Cloud-init
After installing Netplan, disable Cloud-init.
Note
Cloud-init is a tool for configuring servers during startup. On servers with static IP addresses, Cloud-init can reset manually configured network settings. To keep network settings unchanged, disable network management for Cloud-init.
Create a file:
sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
Add a line to the file that disables network management for Cloud-Init:
network: {config: disabled}
To prevent network settings from being overwritten, delete the configuration file that Cloud-init creates for Netplan:
sudo rm -f /etc/netplan/50-cloud-init.yaml
After this, you can create your own configuration.
3. Create a configuration
Determine which file contains the current configuration. To do this, look at the contents of the /etc/netplan folder using the ls command:
ls /etc/netplan
In this example, the configuration is in the file 01-netcfg.yaml. Launch the Nano editor with the parameter – the name of this file:
nano /etc/netplan/01-netcfg.yaml
An editor window will open.
Add the snippet to the file:
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: no
dhcp6: no
addresses: [192.168.124.200/24]
gateway4: 192.168.124.1
nameservers:
addresses: [192.168.124.1, 8.8.8.8, 8.8.4.4]
In Ubuntu 22.04, the configuration format has changed slightly. Instead of the deprecated gateway4, set the route through the key via.
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses: [192.168.124.200/24]
routes:
to: 0.0.0.0/0
via: 192.168.124.1
nameservers:
addresses: [192.168.124.1, 8.8.8.8, 8.8.4.4]
Where:
replace the address
192.168.124.200in the first lineaddresseswith the address of your server;mask
/24corresponds to255.255.255.0. If it matches your mask, don’t change anything. Otherwise, replace with yours;address
192.168.124.1in linegateway4orvia- to your gateway address;the values of the DNS server addresses in the second line
addresses- to the values of the addresses of their DNS servers;There must be at least one empty line at the end of the text.
After making changes, exit edit mode using Ctrl+X. The editor will ask you to save the modified file:
Answer by pressing Y (yes). The editor will suggest a file name to save. Press Enter to agree.
Apply the configuration:
netplan apply
If the output is without errors, everything was done correctly. To be sure, reboot using the reboot command and check that you have received an IP address:
ifconfig -a