This article describes how to add an additional ip address to your ubuntu server
Step 1
Log in to your server via SSH as the root user.
Step 2
Once logged in, you will need to collect some information required to configure the network correctly.
Network device name
To display the default gateway of the active interface, enter the following command:
ls /sys/class/net
Here is an example output:
eth0 eth1 lo tun0 usb0
Ignore the ‘lo’ device, but make note of the other network device in the list, in the example above ours is ‘eth0, eth1’
DNS nameserver
To display the DNS servers used, enter the following command:
resolvectl --no-pager |grep Server
Here is an example output:ls
Current DNS Server: 212.227.123.16
DNS Servers: 212.227.123.16 212.227.123.17
DNS Servers: 2001:8d8:fe:53:72ec::1 2001:8d8:fe:53:72ec::2 212.227.123.16
Make a note of the ‘DNS Servers’.
Default gateway
To display the default gateway of the active interface, enter the following command:
ip route show | grep 'default'
Take note of the IP address listed directly after the ‘default via‘ part, for example:
default via 10.255.255.1 dev eth0 proto static onlink
In this example, the default gateway is 10.255.255.1
Step 3
Locate the existing network configuration file. Use this command to navigate into the folder where the file exists:
cd /etc/netplan
You can then list the files in the folder using this command:
ll
Make a note of the name of the configuration file. In this example, the name of the configuration file is 00-Public_network.yaml.
root@localhost:~# cd /etc/netplan
root@localhost:/etc/netplan# ll
-rw-r--r-- 1 root root 87 Apr 3 18:33 00-Public_network.yaml
Step 4
Once you have the name of the current configuration file, you can rename it by adding .bak to the end of the file name.
Use the command below to do this. Replace 00-Public_network.yaml with the name of the file from the last step.
mv 00-Public_network.yaml 00-Public_network.yaml.bak
This will allow you to keep it as backup configuration file which you know works.
Step 5.
Use a text editor to create and open your new configuration file. For this example we will use nano:
nano 00-Public_network.yaml
Step 6
copy and paste the text below into the new empty file:
Please be aware that the formatting of the file is very specific due to the use of the YAML language, please use spaces for the indentations
network:
renderer: networkd
ethernets:
NETWORKDEVICENAME:
addresses:
- PRIMARY-IP/32
- SECONDARY-IP/32
nameservers:
addresses: [PRIMARY-NS, SECONDARY-NS]
routes:
- on-link: true
to: default
via: GATEWAY
version: 2
Step 7
To fill out the configuration file you will need to know the IP addresses associated with your server (available in your control panel) along with the information from step 1.
- Replace NETWORKDEVICENAME with the network device name from step 1.
- Replace PRIMARY-IP with the main IP address of your server.
- Replace SECONDARY-IP with the new IP address you are adding. You can add more lines, in the same format, for additional IPs.
- Replace PRIMARY-NS and SECONDARY NS with the DNS servers from step 1.
- Replace GATEWAY with the default gateway from step 1.
For our example, the final configuration looks like this:
network:
renderer: networkd
ethernets:
eth0:
addresses:
- 77.68.125.213 /32
- 77.68.125.216 /32
nameservers:
addresses:
- 212.227.123.16
- 212.227.123.17
routes:
- on-link: true
to: default
via: 10.255.255.1
version: 2
Save the file and close your text editor.
If you are using nano, you can save the file with CTRL+O then use CTRL+X to exit.
Step 8
To apply this new configuration, run the following command:
netplan --debug apply
ou may receive warnings about the permissions of the new file, but the new configuration should still work.
To correct the permission warnings, run the following command, replacing 00-Public_network.yaml with the name of your configuration file:
chmod 600 /etc/netplan/00-Public_network.yaml
This will make the file only readable and writable by root
Step 9
To verify that your configuration is correct, enter the following command:
ip addr
Look for the line that starts with your device name, you should see your IP addresses:
[root@localhost:/etc/netplan]# ip addr
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:50:56:2c:11:e9 brd ff:ff:ff:ff:ff:ff
altname enp11s0
inet 77.68.28.139/32 scope global ens192
valid_lft forever preferred_lft forever
inet 77.68.114.76/32 scope global ens192
valid_lft forever preferred_lft forever