Stupid ipv6 tricks – get current global ipv6 prefix – linux

I need to know what global prefix my server is on in order to be able to add firewall rules allowing access from this prefix for specific ports. I don’t control the gateway, but it only gives me the one prefix I’m currently using in the router advertisement.

I can use “ip -6 route show dev eth0” to get the routes, including the prefix:

$ ip -6 route show dev eth0
2001:db8:778:6600::/64 proto ra metric 1 expires 299sec pref medium
fe80::/64 proto kernel metric 256 pref medium
default via fe80::dead:beff:dead:beef proto ra metric 1 expires 179sec pref medium

Since I’ve gotten it from the router advertisement and it’s not the default route and we want the lowest metric (at least in my case), we can grep for the specific line and get only the prefix itself to be used in bash scripts:

prefix=$(ip -6 route show dev eth0 | grep "proto ra metric 1" | grep -v ^default | awk '{print $1}')


Tags:

Leave a Reply