【问题标题】:Why does the linux ip command not accept a hostname when adding an IPv4 address为什么添加 IPv4 地址时 linux ip 命令不接受主机名
【发布时间】:2018-06-10 12:11:02
【问题描述】:

我在 Ubuntu 17.04 上使用 ip 命令。 ip route add 命令不接受主机名。它将接受等效的 IPv4 地址。我做错了什么?

$ sudo ip route add f3.com via 192.168.0.1
[sudo] password for jeffs: 
Error: inet prefix is expected rather than "f3.com".
$ host f3.com
f3.com has address 174.132.98.146
$ 
$ sudo ip route add 174.132.98.146 via 192.168.0.1
$ sudo ip route list
default via 192.168.0.1 dev eno1 proto static metric 100 
10.0.3.0/24 dev lxcbr0 proto kernel scope link src 10.0.3.1 linkdown 
169.254.0.0/16 dev eno1 scope link metric 1000 
174.132.98.146 via 192.168.0.1 dev eno1 
192.168.0.0/24 dev eno1 proto kernel scope link src 192.168.0.16 metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
$ sudo ip route get f3.com
Error: inet prefix is expected rather than "f3.com".
$ sudo ip route get 174.132.98.146
174.132.98.146 via 192.168.0.1 dev eno1 src 192.168.0.16 
    cache 
$ sudo ip -s route get 174.132.98.146
174.132.98.146 via 192.168.0.1 dev eno1 src 192.168.0.16 
    cache  users 1
$ 

我查看了Guide to IP Layer Network Administration with Linux, Appendix D. IP Route Management10 Useful “IP” Commands to Configure Network Interfaces 和其他地方,但我没有看到任何可以做我想做的事情的例子。

现在,我可以拼凑起来了(UNIX 是个好东西):

$ sudo ip route add `host f3.com | fgrep address | cut -d ' ' -f 4` via 192.168.0.23
$ sudo ip route list
default via 192.168.0.1 dev eno1 proto static metric 100 
10.0.3.0/24 dev lxcbr0 proto kernel scope link src 10.0.3.1 linkdown 
169.254.0.0/16 dev eno1 scope link metric 1000 
174.132.98.146 via 192.168.0.23 dev eno1 
192.168.0.0/24 dev eno1 proto kernel scope link src 192.168.0.16 metric 100 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
$ 

但这是一个杂牌。有没有“更好”的方法?

【问题讨论】:

标签: linux networking routing ip


【解决方案1】:

没有错。路由基于 IP 地址,不能使用 DNS 地址。想象一个主机地址解析为多个 IP 地址,应该以不同方式路由

查看 OSI 层时,DNS 名称位于顶部,而 IP 地址位于网络层下方。你无法扭转局面。

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2014-12-19
    • 2014-12-02
    • 2020-12-10
    • 2014-04-12
    • 2011-03-12
    • 2012-05-20
    • 2014-08-19
    相关资源
    最近更新 更多