3

I tried to change the network mask using Linux ip command and got confused. Google search doesn't clarify it either. If someone could explain? Say I have interface enp0s31f6 on my Linux machine

rtuser@rtuser:~$ ip addr show dev enp0s31f6 2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff 

I want to assign ip address to the interface:

rtuser@rtuser:~$ sudo ip addr add 192.168.20.1/24 dev enp0s31f6 rtuser@rtuser:~$ ip addr show dev enp0s31f6 2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff inet 192.168.20.1/24 scope global enp0s31f6 valid_lft forever preferred_lft forever 

Now I realized that my network mask is not correct, I want to change

rtuser@rtuser:~$ sudo ip addr change 192.168.20.1/25 dev enp0s31f6 rtuser@rtuser:~$ ip addr show dev enp0s31f6 2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff inet 192.168.20.1/24 scope global enp0s31f6 valid_lft forever preferred_lft forever inet 192.168.20.1/25 scope global enp0s31f6 valid_lft forever preferred_lft forever 

I got couple same ip addresses on interface. Well, maybe change doesn't work lets try to replace it

rtuser@rtuser:~$ sudo ip addr replace 192.168.20.1/26 dev enp0s31f6 rtuser@rtuser:~$ ip addr show dev enp0s31f6 2: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000 link/ether 98:fa:9b:c7:37:a2 brd ff:ff:ff:ff:ff:ff inet 192.168.20.1/24 scope global enp0s31f6 valid_lft forever preferred_lft forever inet 192.168.20.1/25 scope global enp0s31f6 valid_lft forever preferred_lft forever inet 192.168.20.1/26 scope global enp0s31f6 valid_lft forever preferred_lft forever 

I would expect change/replace ip command would change properties of ip address, but it seems they all add/change/replace keep adding the same ip over and over again. Why is that?

4
  • Delete the ones you don't want by changing add to del. E.g. sudo ip addr del 192.168.20.1/24 dev enp0s31f6CommentedAug 21, 2020 at 16:08
  • Thanks for the comment. Using del command is quite obvious. It'll do what it's supposed to do - deleting ip address. I just want to understand the behavior and reasons for that behavior. Seems like add/change/replace commands do the same thing. I don't believe those smart people who developed ip command would put such duplication with no reason.
    – Yuriy
    CommentedAug 24, 2020 at 19:29
  • the object the command applies a change effect upon is not 192.168.20.1 but the whole 192.168.20.1/24. Using change (or replace) can't affect the object's value itself, only other properties of the object (such as valid_lft), though most can't be changed anyway.
    – A.B
    CommentedAug 29, 2020 at 14:57
  • This question is answered well here: serverfault.com/a/666521/90281CommentedDec 17, 2022 at 2:45

1 Answer 1

1

Just remove the original, erroneous entry with the del option.

ip address del 192.168.20.1/24 dev enp0s31f6 

And then add the corrected IP/CIDR:

ip address add 192.168.20.1/25 dev enp0s31f6 
3
  • I needed to do this remotely, so BEFORE deleting the address, I created a temporary address, checked if it worked, when OK, I removed and fixed the previous address, then removed the temporary address. It worked just fine. Thanks for providing the answer with ip instead of ifconfig :)
    – Pedram
    CommentedApr 30, 2023 at 10:31
  • How do you make it permanent? Reboots reset the settings back to default. The machine has network-manager installed, but it doesn't change the network settings after a reboot.
    – Pedram
    CommentedMay 1, 2023 at 8:21
  • OK, so I added a new connection and brought it up with nmcli to make it survive a reboot. But please feel free to let us know if there is another way to make this change permanent with the ip command.
    – Pedram
    CommentedMay 1, 2023 at 9:25

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.