4

I have a ubuntu machine and want to change the IP address, the requirements are as follow:-

  • using only cli to change the IP address, since the HW is not accessible and the script is changing the IP address through SSH, so it has to be the only cli
  • The changes to be permanent, the IP address shouldn't be lost on restart
  • This should be applicable to all the version of ubuntu

The ubuntu in question is a VM with the default static IP already configured can be accessed with the SSH.

Is there any way to change the IP address of ubuntu with the above requirement?

preferably it should be supported from version 14 - 20 (only stable build)

4
  • How did you get the original address, the one you are trying to change? DHCP, ifupdown, static, netplan ... May 26, 2020 at 5:27
  • it is snapshot of VM, hence IP is pre-configured, need to change that. May 26, 2020 at 5:28
  • yes, its static IP allocation May 26, 2020 at 5:29
  • To start with, which release number? Different releases have different tools for us to recommend. Please click edit & add that to your question, so all facts we need are in the question. Please don't use Add Comment, since that's our one-way channel to you. All facts about your PC should go in the Question with edit as this is a Q&A site, not a general forum, so things work differently here.
    – K7AAY
    May 26, 2020 at 17:04

2 Answers 2

4

Two (of many ways) to do it, say you want the new IP to be 192.168.80.10 with netmask 255.255.255 and your interface is eth0

$ sudo ifconfig eth0 192.168.80.10 netmask 255.255.255.0

or, with the newer ip command, you remove the old address and add the new one:

$ sudo ip address del old.ip.address/netmask dev eth0
$ sudo ip address add 192.168.80.10/24 dev eth0
4
  • will it, instantly apply the IP address? May 26, 2020 at 5:42
  • @rahulKushwaha yes May 27, 2020 at 20:50
  • 3
    This won't be permanent though, it won't survive a reboot
    – Bob Vale
    Oct 19, 2022 at 9:42
  • As @BobVale stated, this didn't survive a reboot. So I found a way and shared it here
    – Pedram
    May 1, 2023 at 9:23
1

I did try with the ip method shared here, but it did not survive reboots. So, I figured, since the Network Manager was managing stuff, things went back to default 🌊

Change ip and netmask with nmcli

Network Manager is installed by default on ubuntu machines so you have access to nmcli. Here is what might work for you:

Check current settings with executing only nmcli. Take note on connection type (e.g. ethernet), DNS Configuration (i.e. servers), and the interface you are connecting to.

Create a new connection

Let's assume you want to create a connection which has the following properties:

  1. Name: SOME_NAME
  2. Interface: enp0s31f6
  3. The interface enp0s31f6 is a cable connection, so type is: ethernet
  4. You want to set the static ip 172.18.1.30, so set it as ip4 value and also set ipv4.type as manual.
  5. The default gateway and dns are already shown when entering nmcli, if you are not planning to change it, just copy them here as values after gw4 and ipv4.dns,

Note that autocomplete should work, so you can simply check for things such as interface-name by pressing TAB twice.

nmcli # check the previous settings
sudo nmcli connection add connection.id "SOME_NAME" \
                          connection.interface-name enp0s31f6 \
                          type ethernet \
                          ip4 172.18.1.30 gw4 172.18.1.1 \
                          ipv4.dns "8.8.8.8" \
                          ipv4.method manual
sudo nmcli connection up SOME_NAME
nmcli # check the current settings
sudo systemctl restart networking.service

Now, the changes should be permanent 🔒

This answer was inspired by this HTG post.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 1, 2023 at 10:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .