I am trying to set the IP address to an interface on my Ubuntu 18.04
machine using ip addr
command however the ip addr
command sets the subnet mask as 255.255.255.255
for a Class C IP address where as the ifconfig
command sets the subnet mask as 255.255.255.0
for the same Class C IP address.
Sharing below the commands and their respective outputs.
ip addr
command and its output.
nvidia@tegra-ubuntu:~$ sudo ip addr flush dev hv0
nvidia@tegra-ubuntu:~$ sudo ip add 192.168.0.2 dev hv0
nvidia@tegra-ubuntu:~$ ifconfig
hv0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.2 netmask 255.255.255.255 broadcast 0.0.0.0
ether 0a:86:4c:f8:4f:01 txqueuelen 1000 (Ethernet)
RX packets 867 bytes 77026 (77.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 756 bytes 71072 (71.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 5
ifconfig
command and its output.
nvidia@tegra-ubuntu:~$ sudo ip addr flush dev hv0
nvidia@tegra-ubuntu:~$ sudo ifconfig hv0 192.168.0.2 up
nvidia@tegra-ubuntu:~$ ifconfig
hv0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.2 netmask 255.255.255.0 broadcast 192.168.0.255
ether 0a:86:4c:f8:4f:01 txqueuelen 1000 (Ethernet)
RX packets 867 bytes 77026 (77.0 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 756 bytes 71072 (71.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 5
I would like to understand whether this is expected behavior or is this an issue with the ip addr
command.
I also understand the mechanism to overcome this issue is to explicitly however I do not want to use them as if now and would like to understand the reason behind this change of behavior.
Assigning an IPv4 address requires a subnet mask. This is what the kernel will use to determine if the destination IP is on the same network subnet or not, and thus, how and where to send packets.
The reason why the subnet mask is not automatically set by the network class is that you might be using some "non-standard" subnets, like 10.12.8.0/21
. If, for whatever reason you're not using DHCP, then it is your responsibility to know the network configuration, which include the network mask.
Last but not least, ifconfig
is now deprecated for a while in favor of ip
(iproute2
), and you experimented, ip
might not reflect (exact) same behaviors as ifconfig
. From your tests, you can see ifconfig
and ip
have different default values for the subnet mask if it is not specified.