Merge pull request #948 from KayGuoWhu/master

translated by KayGuoWhu
This commit is contained in:
Xingyu.Wang
2014-05-10 22:01:56 +08:00
2 changed files with 120 additions and 124 deletions

View File

@@ -1,124 +0,0 @@
[KayGuoWhu Translating ]
Start Practising Linux ip command and Avoid the Habit of Using ifconfig
================================================================================
The linux **ip** command is similar to **ifconfig**, but more powerful and is intended to be a replacement for it. With ip you have the advantage of performing several network administration tasks with only one command. ifconfig is one of the deprecated command within net-tools that has not been maintained for many years. The functionalities of many commands is retained with more features under iproute2 suite.
![Net tools vs Iproute2](http://linoxide.com/wp-content/uploads/2014/05/Linux-Nettools-vs-Iproute2.png)
To install ip download the **iproute2 suite utility** [here][1], however most Linux distributions will come with the iproute2 tools pre-installed.
You can also use use git to download the source code:
$ git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/shemminger/iproute2.git
![iproute2 git clone](http://linoxide.com/wp-content/uploads/2014/04/iproute2-git-clone.png)
### Setting and Deleting an Ip Address ###
To set an IP address for your computer, the command ip can be used as follows:
$ sudo ip addr add 192.168.0.193/24 dev wlan0
Note that the IP address has a prefix, for example /24. This is used in classless inter-domain routing (CIDR) to show the subnet mask used. In this case the subnet mask is 255.255.255.0.
After you have set the IP address confirm with show, whether the the changes have taken effect.
$ ip addr show wlan0
![set ip address](http://linoxide.com/wp-content/uploads/2014/04/set-ip-address.png)
You can also use the same procedure to delete an IP address by just replacing add with del.
$ sudo ip addr del 192.168.0.193/24 dev wlan0
![delete ip address](http://linoxide.com/wp-content/uploads/2014/04/delete-ip-address.png)
### Show Routing Table Entry ###
The route object of ip command also helps you to see the route packets will take in your network as set in your routing table. The first entry is the default route which you can change as you prefer.
In this example there are several routes. These are a result of having a several devices connected through different network interface. These include WIFI, Ethernet and a point to point link.
$ ip route show
![ip route show](http://linoxide.com/wp-content/uploads/2014/04/ip-route-show.png)
Suppose now that you have an IP address which you need to know the route packets will take. You can use route option as follows:
$ ip route get 10.42.0.47
![ip route get](http://linoxide.com/wp-content/uploads/2014/04/ip-route-get.png)
### Changing The Default Route ###
To change the default route, the ip command can be used as follows:
$ sudo ip route add default via 192.168.0.196
![default route](http://linoxide.com/wp-content/uploads/2014/04/default-route.png)
### Show Network Statistics ###
The ip command can also be used to show the statistics of the various network interfaces. To do this you can use the ip command with the option -s and then specify the network device.
$ ip -s link
![ip statistics all interfaces](http://linoxide.com/wp-content/uploads/2014/04/ip-statistics-all-interfaces.png)
When you need to get information about a particular network interface, add the option **ls** followed by the name of the network interface. The option **-s** when used more than once gives you more information about that particular interface. This can be very useful especially when trouble shooting errors in network connectivity.
$ ip -s -s link ls p2p1
![ip link statistics](http://linoxide.com/wp-content/uploads/2014/04/ip-link-statistics-.png)
### ARP Entries ###
Address Resolution Protocol (ARP) is used to translate an IP address to its corresponding physical address, commonly known as MAC address. With ip command you can view the MAC address of the devices connected in your LAN by using the option neigh or neighbour.
$ ip neighbour
![ip neighbour](http://linoxide.com/wp-content/uploads/2014/04/ip-neighbour.png)
### Monitor Netlink Messages ###
It is also possible to view netlink messages with ip command. The monitor option allows you to see the state of your network devices. For instance a computer on your LAN could be categorized as REACHABLE or STALE depending on its status. The command can be used as follows:
$ ip monitor all
![ip monitor all](http://linoxide.com/wp-content/uploads/2014/04/ip-monitor-all.png)
### Activate and Deactivate Network Interface ###
To activate an particular interface you can use the ip command with options up and down, almost similar to how ifconfig is used.
In this example you can see the routing table entry when the ppp0 interface is activated and after it is deactivated and activated once again. The interface can be wlan0 or eth0. Change ppp0 to whatever interface is available in your case.
$ sudo ip link set ppp0 down
$ sudo ip link set ppp0 up
![ip link set up and down](http://linoxide.com/wp-content/uploads/2014/04/ip-link-set-up-and-down.png)
### Getting Help ###
In case you get stuck and do not know how a particular option works you can use the help option. The man page does not give a lot of information on how to use the ip options and this is where help comes to the rescue.
For example, to know more about the route option:
$ ip route help
![ip route help](http://linoxide.com/wp-content/uploads/2014/04/ip-route-help.png)
### Conclusion ###
The command ip is a must have tool for network administrators and all Linux users alike. It is time to move from ifconfig, especially when you are writing scripts.
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/use-ip-command-linux/
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://www.linuxgrill.com/anonymous/iproute2/NEW-OSDL/

View File

@@ -0,0 +1,120 @@
开始练习Linux的ip命令避免使用ifconfig的习惯
================================================================================
linux的**ip**命令和**ifconfig**类似但前者功能更强大并旨在取代后者。使用ip命令只需一个命令你就能很轻松地执行一些网络管理任务。ifconfig是网络工具中已被废弃使用的一个命令许多年前就已经没有维护了。许多命令的功能被保留在iproute2套件里以提供更多特性。
![Net tools vs Iproute2](http://linoxide.com/wp-content/uploads/2014/05/Linux-Nettools-vs-Iproute2.png)
要安装ip请下载**iproute2套装工具** [点击这里][1]。不过大多数Linux发行版已经预装了iproute2工具。
你也可以使用git命令来下载源代码
$ git clone https://kernel.googlesource.com/pub/scm/linux/kernel/git/shemminger/iproute2.git
![iproute2 git clone](http://linoxide.com/wp-content/uploads/2014/04/iproute2-git-clone.png)
### 设置和删除Ip地址 ###
要给你的机器设置一个IP地址可以使用下列ip命令
$ sudo ip addr add 192.168.0.193/24 dev wlan0
请注意IP地址要有一个后缀比如/24。这种用法用于在无类域内路由选择CIDR中来显示所用的子网掩码。在这个例子中子网掩码是255.255.255.0。
在你按照上述方式设置好IP地址后需要查看是否已经生效。
$ ip addr show wlan0
![set ip address](http://linoxide.com/wp-content/uploads/2014/04/set-ip-address.png)
你也可以使用相同的方式来删除IP地址只需用del代替add。
$ sudo ip addr del 192.168.0.193/24 dev wlan0
![delete ip address](http://linoxide.com/wp-content/uploads/2014/04/delete-ip-address.png)
### 列出路由表条目 ###
ip路由对象的命令还可以帮助你查看网络中的路由数据包并设置你的路由表。第一个条目是默认的路由条目你可以随意改动它。
在这个例子中有一些路由条目。这个结果显示有几个设备通过不同的网络接口连接起来。它们包括WIFI、以太网和一个点对点连接。
$ ip route show
![ip route show](http://linoxide.com/wp-content/uploads/2014/04/ip-route-show.png)
假设现在你有一个IP地址你需要知道路由包从哪里来。可以使用下面的路由选项
$ ip route get 10.42.0.47
![ip route get](http://linoxide.com/wp-content/uploads/2014/04/ip-route-get.png)
### 更改默认路由 ###
要更改默认路由使用下面ip命令
$ sudo ip route add default via 192.168.0.196
![default route](http://linoxide.com/wp-content/uploads/2014/04/default-route.png)
### 显示网络统计数据 ###
使用ip命令还可以显示不同网络接口的统计数据。
![ip statistics all interfaces](http://linoxide.com/wp-content/uploads/2014/04/ip-statistics-all-interfaces.png)
当你需要获取一个特定网络接口的信息时,在网络接口名字后面添加选项**ls**即可。多次添加选项**-s**会给你这个特定接口更详细的信息。特别是在网络连接中排除故障时,这会非常有用。
$ ip -s -s link ls p2p1
![ip link statistics](http://linoxide.com/wp-content/uploads/2014/04/ip-link-statistics-.png)
### ARP条目 ###
地址解析协议ARP用于将一个IP地址转换成它对应的物理地址也就是通常所说的MAC地址。使用ip命令的neigh或者neighbour选项你可以查看接入你所在的局域网的设备的MAC地址。
$ ip neighbour
![ip neighbour](http://linoxide.com/wp-content/uploads/2014/04/ip-neighbour.png)
### 监控网络连接消息 ###
也可以使用ip命令查看网络连接消息。monitor选项允许你查看网络设备的状态。比如所在局域网的一台电脑根据它的状态可以被分类成REACHABLE或者STALE。使用下面的命令
$ ip monitor all
![ip monitor all](http://linoxide.com/wp-content/uploads/2014/04/ip-monitor-all.png)
### 激活和停止网络接口 ###
你可以使用ip命令的up和down选项来激某个特定的接口就像ifconfig的用法一样。
在这个例子中当ppp0接口被激活和在它被停止和再次激活之后你可以看到相应的路由表条目。这个接口可能是wlan0或者eth0.将ppp0更改为你可用的任意接口即可。
$ sudo ip link set ppp0 down
$ sudo ip link set ppp0 up
![ip link set up and down](http://linoxide.com/wp-content/uploads/2014/04/ip-link-set-up-and-down.png)
### 获取帮助 ###
当你陷入困境不知道某一个特定的选项怎么用的时候你可以使用help选项。man页面并不会提供许多关于如何使用ip选项的信息因此这里就是获取帮助的地方。
比如想知道关于route选项更多的信息
$ ip route help
![ip route help](http://linoxide.com/wp-content/uploads/2014/04/ip-route-help.png)
### 小结 ###
对于网络管理员们和所有的Linux使用者们ip命令是必备工具。是时候抛弃ifconfig命令了特别是当你写脚本时。
--------------------------------------------------------------------------------
via: http://linoxide.com/linux-command/use-ip-command-linux/
译者:[KayGuoWhu](https://github.com/KayGuoWhu) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://www.linuxgrill.com/anonymous/iproute2/NEW-OSDL/