IP Linux命令行

来自百问网嵌入式Linux wiki


本文提供了开始使用Linux内核工具ip [1]所需的基本信息。

简介

ip命令用来 [1]显示/操纵网络接口的路由,设备,策略路由和隧道。

开始使用

  • 检查网络接口
Board $> ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: can0: <NOARP,ECHO> mtu 16 qdisc noop state DOWN group default qlen 10
    link/can 
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:80:e1:42:43:65 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.237/22 brd 192.168.3.255 scope global dynamic eth0
       valid_lft 172057sec preferred_lft 172057sec
    inet 192.168.0.4/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::280:e1ff:fe42:4365/64 scope link 
       valid_lft forever preferred_lft forever
4: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
  • 为指定网络接口分配IP地址
Board $> ip addr add 192.168.1.53 dev eth0
  • 删除IP地址
Board $> ip addr del 192.168.1.53 dev eth0
  • 禁用接口
Board $> ip link set eth0 down
  • 启用接口
Board $> ip link set eth0 up
  • 检查路由表
Board $> ip route show
default via 192.168.3.254 dev eth0 proto dhcp src 192.168.1.237 metric 1024 
192.168.0.0/22 dev eth0 proto kernel scope link src 192.168.1.237 
192.168.3.254 dev eth0 proto dhcp scope link src 192.168.1.237 metric 1024 
  • 增加静态路由
Board $> ip route add 192.168.2.0/16 via 192.168.1.1 dev eth0
  • 移除静态路由
Board $> ip route del 192.168.2.0/16
  • 设置默认网关
Board $> ip route add default via 192.168.1.1


进阶使用

一些用法示例可供参考[2]