Advertisement
metalx1000

Set Default Network Interface

Jul 18th, 2020
3,351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.17 KB | None | 0 0
  1. https://serverfault.com/questions/123553/how-to-set-the-preferred-network-interface-in-linux
  2. #To make packets with destinations 192.168.10.* use eth0, and all other packets use eth1:
  3. 1) View your current routing table
  4.  
  5. ip route list
  6.  
  7. One entry will be something like "default via 192.168.1.1" where 192.168.1.1 is your router (a.k.a. gateway) ip address. Remember the gateways for eth0 and eth1, as we'll need them later.
  8.  
  9. 2) Delete the default route(s). (Warning: this will kick you offline.)
  10.  
  11. ip route del default
  12.  
  13. 3) Add a new default route (this will bring you back online). Replace 192.168.1.1, below, with your gateway ip address from above.
  14.  
  15. ip route add default via 192.168.1.1 dev eth1
  16.  
  17. 4) Add a specific route that will be served by eth0. More-specific routes automatically take precedence over less-specific ones.
  18.  
  19. ip route add 192.168.10.0/24 via 192.168.1.1 dev eth0
  20.  
  21. Finally, you can ask Linux which interface will be used to send a packet to a specific ip address:
  22. ip route get 8.8.8.8
  23.  
  24. If the configuration worked, packets to 8.8.8.8 (Google's server) will use eth1. Packets to any ip on your local network:
  25.  
  26. ip route get 192.168.10.7
  27.  
  28. will use eth0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement