cherissemunja
cherissemunja

Reputation: 27

Is it possible to run command "route -n" specifically for a NIC

I am fairly new to linux I wanted to ask if its possible in linux commands to run a "route -n" command to retrieve information for a specific NIC. E.G route -n ether0. Because currently it shows me for all the NIC's but what if I want just for one?

Upvotes: 1

Views: 60

Answers (2)

Kasun Rathnayaka
Kasun Rathnayaka

Reputation: 503

Do you want see route for ether0, you can use below command

ip -o route show dev ether0

or

ip route show | grep ether0

or

route -n | grep ether0

or

netstat -nr | grep ether0

To see NIC for ether0, you can use ifconfig command and Gateway NIC you can use arp -a commands

ifconfig ether0

for see GW and direct connected NICS you can use below command

arp -a

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295687

Use the iproute2 ip command (rather than the antique route command), and provide a selector with your NIC:

# for ether0
ip -o route list dev ether0

(I've added -o since your tags indicate that you're using this for scripting purposes; ensuring that each result lives on its own line is appropriate in this case).

This is a significant improvement on the simple route | grep ether0 approach, as it shows routing entries which can end up sending traffic through a NIC but don't name that NIC explicitly.

As the default for iproute2 is to avoid leaning on the resolver, no local flag equivalent to -n is necessary; instead, if you did want to use the resolver, you would need to add -r.

Upvotes: 2

Related Questions