0

I'm looking for an exact replacement of ifconfig <interface> <ip_address> command using the ip command.

For example, if I want to change eth0's IP address to 10.0.0.1 (not caring about the netmask), I can just type:

ifconfig eth0 10.0.0.1

For the ip command though, I've looked around a lot and anything I can find is just adding or removing a network, not modifying the current one. So, to modify the current network, I end up having to use the following (or similar) script:

new_ip=$1
ip_addr=`ip addr show eth0 | grep "inet" | cut -d: -f2 | awk '{print $2}'`

ip -4 addr delete $ip_addr dev eth0
ip -4 addr add $new_ip dev eth0

Because if I don't do it this way, when the primary interface ip is deleted later on, it takes all the secondary ip addresses with it.

I'm doing this from inside a program and there are too many system calls that could go wrong here. Is there a way to do this without having to use so many commands?

ip addr change and ip addr replace also does the same thing: enter image description here

Note: I can add any open source program into the system as I want. So, don't shy away from solutions that use other open source programs.

1 Answer 1

0

ip addr change ... is what you are looking for.

do ip addr help to see details

Edit: For future readers - this is wrong answer, see comments.

2
  • That just gives me a secondary IP address. Jul 31, 2018 at 6:58
  • 1
    You are right, my bad. Looks like change and replace allow to change just flags, not IP itself.
    – marosg
    Jul 31, 2018 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.