3

Specifically, route add -host allows me to specify the host via hostname to add a route to a host, e.g.,

route add -host www.google.com gw 192.168.1.1

However, I cannot find a way to do this using ip route. The closest I can get is

ip route add <ipaddrofhost>/32 via 192.168.1.1 dev eth0

Am I really confined to not using a hostname?

6
  • 1
    Even if it did work, relying on DNS to configure routing or firewall entries for entities outside your network is usually a bad idea in the era of CDNs.
    – Andrew B
    Feb 8, 2016 at 0:58
  • 4
    It doesn't make sense to use a hostname here. First, the routing table only uses IP addresses, so it has to be looked up anyway. Second, the IP address returned when you actually get around to sending traffic may be different. So you have to add in all the IP addresses anyway. Feb 8, 2016 at 1:02
  • 1
    I agree that this is not a good idea in a general case; perhaps that's why the address resolution "feature" wasn't carried forward. For the moment I'm using the old way because it's "good enough". Feb 8, 2016 at 5:19
  • 3
    Funny you picked www.google.com as an example, because it clearly shows why this concept is broken: www.google.com has 4 A records for me, but your command results into only a route for only one of these four.
    – Teun Vink
    Feb 8, 2016 at 6:15
  • Yes, as I typed that contrived example I realized that would be the case in the general sense. Although, I will admit that I've never tried using a host route where the name has multiple A record; it's entirely possible that route(8) could add a host route for each A record. I will likely write something bespoke to get the overall behavior I'm looking for rather than rely on brittle host routes. Feb 8, 2016 at 18:01

1 Answer 1

1

"ip route add" can only take an IP address or a prefix for the destination argument. The documentation (search for "man 8 ip") describes this explicitely. The destination can be an ADDR or a PREFIX. An ADDR is an IP address or the string "any", and a PREFIX is an IP address optionally followed by /length, or the string "default".

So, there is no direct equivalent of "route add -host some.host.fqdn". If your target is single-homed and can be resolved by a DNS query, you can use something like:

ip route add `dig +short www.google.com`/32 via 192.168.1.1 dev eth0

But this won't work for a multihomed host, nor for names resolvable by /etc/hosts, nor for targets which use a single name for multiple hosts.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .