Score:1

How to reset `ip a` index to start at `1` and end at `number of devices`

nl flag

The command ip a shows list of network ports and also shows an index.

For example, 1 for lo and 2 for enp5s0 here:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

How to reset the index? I have attached a screenshot of the index missing many numbers in between. That is, it shows 15, (missing 16 to 18), 19, (missing 20 to 86), 87 to 94, (missing 95 to 117), 118, 119. This is caused by creation of virtual devices and deletion of them. Now here how to make ip a show the numbers continuously starting from 1 and if there are 10 devices, index should be from 1 to 10?

enter image description here

Score:2
jp flag

These are not really indexes but rather ip command specific sequential numbers that get messed up when interfaces are deleted and they shouldn't be relied upon for identifying interfaces ... A way to reset them is to simply reboot your system so that the kernel's list of devices are recreated.

You can, however, make them display as you like, if that is important to you, with e.g. something like:

ip a | awk '$0 ~ "^[0-9]+:" {i++; sub("^[0-9]+:",i":")}1'

I did run ip through strace to try and find the source of these numbers and it appears they are created internally by ip with no apparent indication to external source.

A further look at the source code confirms the above assumption and reveals that ip pulls device data directly from the kernel and for some reason it appears that deleted devices keep added to the total number of devices but not shown in the output.

This might clearly explain ... Specifically: "Each device is given an sequential number as an identifying number. This is merely a convenience, and should not be used to refer to devices.".

user535733 avatar
cn flag
+1 A thorough, clear, and clean answer.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.