linux 中ip命令_Linux中IP命令简介

linux 中ip命令

介绍: (Introduction:)

As a person with a Computer Networking background, IP utility is one of the most common tools I use on a day to day basis. This utility is crucial in troubleshooting routing issues. It also plays a significant role in understanding the packet traversal from source to destination, specifically answering the questions like which interface is chosen for packet traversal, which device is the source directly connected to, the IP addresses present on its interfaces, and so on. This article provides an overview of typical use-cases for IP utility and the options to be passed.

作为具有计算机网络背景的人,IP实用程序是我每天使用的最常用工具之一。 该实用程序对于解决路由问题至关重要。 它在理解从源到目的地的数据包遍历方面也起着重要作用,尤其是回答以下问题:选择为数据包遍历选择哪个接口,与源直接相连的设备,其接口上的IP地址等等。 本文概述了IP实用程序的典型用例以及要传递的选项。

基本选项: (Basic Options:)

ip utility is a part of the iproute2 package and comes pre-installed on all modern Linux distributions. ip utility can be considered as a replacement(also enhancement) for the ifconfig utility which is part of the net-tools package. With advancements in Linux kernel, the net-tools package is being deprecated and a more powerful alternative has evolved.

ip实用程序是iproute2软件包的一部分,并且预装在所有现代Linux发行版中。 ip实用程序可以被视为ifconfig实用程序的替代品(也是增强功能),它是net-tools软件包的一部分。 随着Linux内核的进步,不推荐使用net-tools软件包,并且已经开发出了更强大的替代方法。

According to the man page, the syntax for ip utility is as below

根据手册页,ip实用程序的语法如下

ip [ OPTIONS ] OBJECT { COMMAND | help }

The object types that are supported by the IP utility are:

IP实用程序支持的对象类型是:

  • address — Display or modify IP addresses

    地址-显示或修改IP地址
  • route — Display or modify the routing table of the device

    route —显示或修改设备的路由表
  • link — Display or modify network interfaces

    链接—显示或修改网络接口
  • neigh — Display or modify ARP table

    neigh(邻居)—显示或修改ARP表

用例: (Use-cases:)

IP commands are crucial in understanding the packet traversal in/through the Linux kernel. It gives information about the routes, the directly connected devices, the IP address on the network interface cards (NIC) present on the device, and of course the MAC address information and other hardware information about the NICs.

IP命令对于了解Linux内核中/通过Linux内核的数据包遍历至关重要。 它提供有关路由,直接连接的设备,设备上存在的网络接口卡(NIC)上的IP地址的信息,当然还提供有关NIC的MAC地址信息和其他硬件信息。

获取/修改IP地址: (Get/Modify IP address:)

# Displays IP information
ip addr
ip addr ls <device_name># Add IP address to a device eth0
ip addr add 192.168.2.1/30 dev eth0# Delete IP address on eth0
ip addr del 192.168.2.1/30 dev eth0

获取/修改路线信息: (Get/Modify route information:)

The route is the path traversed by a packet to reach a destination. The routing table contains all the information about routes through the interfaces on a system.

路由是数据包经过的到达目的地的路径。 路由表包含有关通过系统接口的路由的所有信息。

Routes are stored in what is called a routing table. A default routing table exists for any device connected to some network. Routes can also be added to a new table and can be referenced when a specific criterion is matched. This idea lays the foundation for policy-based routing.

路由存储在所谓的路由表中。 连接到某个网络的任何设备均存在默认路由表。 路由也可以添加到新表中,并且在匹配特定条件时可以引用路由。 这个想法为基于策略的路由奠定了基础。

Policy-based routing:

基于策略的路由:

Routing decisions are typically made based on the destination address of a packet. But there can be situations where the packets need to be routed based on special conditions, for instance, source IP, protocol or port, or any other information available on the packet header. This conditional routing is commonly referred to as policy-based routing.

通常基于数据包的目标地址来做出路由决策。 但是在某些情况下,可能需要根据特殊条件路由数据包,例如,源IP,协议或端口,或数据包头上可用的任何其他信息。 此条件路由通常称为基于策略的路由。

A policy-based route can have a condition to look up a specific table when the condition is matched. Such conditions are added as ip rules.

当符合条件时,基于策略的路由可以具有条件以查找特定表。 这些条件作为ip规则添加。

# view the default routing table
$ ip route show# view the contents of table with name
$ ip route show table <table_name># Get the list of ip rules on the device
$ ip rule show

Custom routes can be added manually using the ip route command.

可以使用ip route命令手动添加自定义路由。

# Add a new route
ip route add {NETWORK_ADDRESS/MASK} via {GATEWAY_IP}
ip route add {NETWORK_ADDRESS/MASK} dev <device_name># Add a default route via interface
ip route add default via dev <device_name># replace an existing default route (or any route in general)
ip route replace default via <new_interface>

Route deletion is also handled by the ip route del. If a specific route is down and if it happens to be in the routing table, the route can be deleted with the command below.

路由删除也由ip route del处理。 如果特定路由已关闭并且恰好在路由表中,则可以使用以下命令删除该路由。

ip route del {NETWORK_ADDRESS/MASK} dev <device_name>

获取/修改网络接口: (Get/Modify Network Interfaces:)

The ip link command handles the parameters related to the network interface. The most commonly used actions when working with IP link utility are show, set, add, del. The IP link command can make changes to both physical and virtual interfaces as well

ip link命令处理与网络接口有关的参数。 使用IP链接实用程序时,最常用的操作是显示,设置,添加和删除。 IP链接命令也可以同时更改物理和虚拟接口

# list all links on the device
$ ip link show# turn a device up
$ ip link set up dev eth0# add a new device
$ ip link add dev tun1

IP link is particularly useful in changing the hardware properties of the network interface. This is one of the options in the IP utility that deals with the parameter changes required for layer 2 data transmission. The following are the list of options supported by the ip link (source: ip link help).

IP链接在更改网络接口的硬件属性时特别有用。 这是IP实用程序中用于处理第2层数据传输所需的参数更改的选项之一。 以下是ip链接支持的选项列表(来源:ip链接帮助)。

ip link set { DEVICE | dev DEVICE | group DEVGROUP }
[ { up | down } ]
[ type TYPE ARGS ]
[ arp { on | off } ]
[ dynamic { on | off } ]
[ multicast { on | off } ]
[ allmulticast { on | off } ]
[ promisc { on | off } ]
[ trailers { on | off } ]
[ carrier { on | off } ]
[ txqueuelen PACKETS ]
[ name NEWNAME ]
[ address LLADDR ]
[ broadcast LLADDR ]
[ mtu MTU ]
[ netns { PID | NAME } ]
[ link-netnsid ID ]
[ alias NAME ]
[ vf NUM [ mac LLADDR ]
[ vlan VLANID [ qos VLAN-QOS ] [ proto VLAN-PROTO ] ]
[ rate TXRATE ]
[ max_tx_rate TXRATE ]
[ min_tx_rate TXRATE ]
[ spoofchk { on | off} ]
[ query_rss { on | off} ]
[ state { auto | enable | disable} ] ]
[ trust { on | off} ] ]
[ node_guid { eui64 } ]
[ port_guid { eui64 } ]
[ xdp { off |
object FILE [ section NAME ] [ verbose ] |
pinned FILE } ]
[ master DEVICE ][ vrf NAME ]
[ nomaster ]
[ addrgenmode { eui64 | none | stable_secret | random } ]
[ protodown { on | off } ]

获取/修改邻居信息(ARP表): (Get/Modify neighbor information (ARP Table):)

ip neigh command is useful to know about the physical interfaces of the devices that are directly connected to a specific device. ip neigh is useful to display the Address Resolution Protocol (ARP ) table. This table is a key component of routing and is the source of truth to map a specific IP address to a physical interface (MAC Address) for delivering the packets destined to the IP address.

ip neigh命令有助于了解直接连接到特定设备的设备的物理接口。 ip neigh对于显示地址解析协议(ARP)表很有用。 该表是路由的关键组成部分,并且是将特定IP地址映射到物理接口(MAC地址)以传递目的地为IP地址的数据包的真相的来源。

ip neigh supports the following options (source: ip neigh help)

ip neigh支持以下选项(来源:ip neigh帮助)

ip neigh help
Usage: ip neigh { add | del | change | replace }
{ ADDR [ lladdr LLADDR ] [ nud STATE ] | proxy ADDR } [ dev DEV ]
ip neigh { show | flush } [ proxy ] [ to PREFIX ] [ dev DEV ] [ nud STATE ]
[ vrf NAME ]

The commonly used commands ip neigh commands are as follows

常用命令ip neigh命令如下

# get contents of the ARP table
$ ip neigh show# add a new entry to ARP table
$ ip neigh add {IP_ADDRESS} lladdr {MAC/LLADDRESS} dev {DEVICE_NAME} nud {STATE}# Change ARP state
$

One interesting topic that requires mentioning is the state of the neighbor in the ARP table of a device.nud in the command above stands for Neighbor Unreachability Detection. A neighbor can be in one of the 9 states — none, probe, reachable, stale, delay, failed, permanent, noarp, incomplete. I will leave the details of the ARP table and the neighbor states for a future article.

需要提及的一个有趣的话题是设备的ARP表中的邻居状态。上述命令中的nud表示邻居不可达性检测。 邻居可以处于9个状态之一中-无,探测,可到达,陈旧,延迟,失败,永久,noarp,不完整。 在以后的文章中,我将保留ARP表的详细信息和邻居状态。

最后的话: (Final Words:)

That’s my take on some useful ip commands for beginners. This isn’t a comprehensive list but a list of the most common use-cases and the commands to use in such situations. I hope you enjoyed reading the article as much as I enjoyed writing it.

这就是我对初学者有用的一些ip命令的观点。 这不是完整的列表,而是最常见的用例和在这种情况下使用的命令的列表。 我希望您喜欢阅读这篇文章,也喜欢阅读它。

翻译自: https://medium.com/100-days-of-linux/introduction-to-ip-commands-in-linux-15a2be029e11

linux 中ip命令

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值