常用linux 命令 -网络相关

此文参考:

1.网络文章,但最后发现源头是在《鸟哥私房菜》,再次感谢原作者;

2.工作中跟同事讨论,自己尝试。

本人水平有限,如有错误,请大家指正,谢谢。

一 网络参数设置命令

 1.ifconfig :查询、设置网卡与IP网段等相关参数

    1.1 man手册定义

    

DESCRIPTION
       Ifconfig  is used to configure the kernel-resident network interfaces.  It is used at boot time to set up
       interfaces as necessary.  After that, it is usually only needed when debugging or when system  tuning  is
       needed.

       If  no arguments are given, ifconfig displays the status of the currently active interfaces.  If a single
       interface argument is given, it displays the status of the given interface only; if a single -a  argument
       is  given,  it displays the status of all interfaces, even those that are down.  Otherwise, it configures
       an interface.

简译: ifconfig 习惯用于配置kernel-resident 网络接口,一般在启动时设置必要的接口。也用于debug和系统调试。

如果没有参数 会列出当前激活(up status)的网络接口

如果接一个网络接口参数(ifconfig eth0),会输出该接口的配置

如果后面参数是-a, 会输出所有的网络接口(up 和down staus的网络接口)

 

1.2 命令输出简述

[root@test_1 net]# ifconfig
eth0      Link encap:Ethernet  HWaddr 84:2B:2B:94:F7:7D  
          inet addr:192.168.2.241  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::862b:2bff:fe94:f77d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1546524 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1652650 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:754067360 (719.1 MiB)  TX bytes:1474728958 (1.3 GiB)
          Interrupt:18 

eth0:1    Link encap:Ethernet  HWaddr 84:2B:2B:94:F7:7D  
          inet addr:192.168.2.242  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 

eth0:2    Link encap:Ethernet  HWaddr 84:2B:2B:94:F7:7D  
          inet addr:192.168.2.243  Bcast:192.168.2.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:18 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:594 errors:0 dropped:0 overruns:0 frame:0
          TX packets:594 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:51885 (50.6 KiB)  TX bytes:51885 (50.6 KiB)

。eth0:网卡的代号,也有lo这个loopback。

· HWaddr:网卡的硬件地址,习惯称为MAC。

· inet addr:IPv4的IP地址,后续的Bcase、Mask分别代表的是Broadcast与Netmask。

· inet6 addr:是IPv6的版本的IP,我们没有使用,所以略过。

· RX:那一行代表的是网络由启动到目前为止的数据包接收情况,packets代表数据包数、errors代表数据包发生错误的数量、dropped代表数据包由于有问题而遭丢弃的数量等。

· TX:与RX相反,为网络由启动到目前为止的传送情况。

· collisions:代表数据包碰撞的情况,如果发生太多次,表示你的网络状况不太好。

· txqueuelen:代表用来传输数据的缓冲区的储存长度。

· RX Bytes、TX Bytes:总传送、接收的字节总量。

· Interrupt、Memory:网卡硬件的数据,IRQ岔断与内存地址。

通过观察上述的资料,大致上可以了解到你的网络情况,尤其是RX、TX内的error数量,以及是否发生严重的collision情况,都是需要注意的。

1.3应用 

1.3.1 下面是我给一个网卡配置多个ip地址用的命令

ifconfig eth0:1 192.168.2.242 netmask 255.255.255.0 up &
ifconfig eth0:2 192.168.2.243 netmask 255.255.255.0 up &

PS: 这种配置子系统重启后,会失去,若想系统重启后不丢失,解决方法一:

将该命令添加到rc.local 里 如下后两行

[devtac@test_1 network-scripts]$ more /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
xl2tpd -c /etc/xl2tpd.conf -D &
ifconfig eth0:1 192.168.2.242 netmask 255.255.255.0 up &
ifconfig eth0:2 192.168.2.243 netmask 255.255.255.0 up &

 

2. ifup ifdown

2.1 简述:man 手册里说的很清楚,如下

     

       The  ifup  and  ifdown commands may be used to configure (or, respec- tively, deconfigure) network inter-
       faces based on interface definitions in  the  files  /etc/sysconfig/network  and  /etc/sysconfig/network-
       scripts/ifcfg-<configuration>

       These scripts take one argument normally: the name of the configuration (e.g. eth0). They are called with
       a second argument of "boot" during the boot sequence so that devices that are not meant to be brought  up
       on boot (ONBOOT=no, see below) can be ignored at that time.

简译:ifup 和ifdown 命令 用于配置位于/etc/sysconfig/network 和/etc/sysconfig/network-scripts/ifcfg-<configuration> 已经配置过的网卡接口文件 如下两个配置文件

[devtac@test_1 network-scripts]$ pwd
/etc/sysconfig/network-scripts
[devtac@test_1 network-scripts]$ ll
总用量 204
-rw-r--r--. 1 root root   276 8月  26 14:35 ifcfg-eth0
-rw-r--r--. 1 root root   254 1月   9 2013 ifcfg-lo

通常,该命令会接一个参数。

PS:ifup ifdown 所在位置如下,有兴趣的可以分析下这两个脚本。

[devtac@test_1 network-scripts]$ pwd
/etc/sysconfig/network-scripts
[devtac@test_1 network-scripts]$ ll ifup ifdown
lrwxrwxrwx. 1 root root 20 8月  26 14:40 ifdown -> ../../../sbin/ifdown
lrwxrwxrwx. 1 root root 18 8月  26 14:40 ifup -> ../../../sbin/ifup

2.2脚本阅读。。。需要时间~~~或者另开。

2.3使用  注:下面例子里因为eth0 已经启动了。大家若是ssh 连接到服务器,请审议ifdown ,网卡停了,就无法连上主机了。。。。

[root@test_1 ~]# ifup eth0
活跃连接状态:激活的
活跃连接路径:/org/freedesktop/NetworkManager/ActiveConnection/1

 3.route 

3.1 man 手册定义

DESCRIPTION
       Route manipulates the kernel’s IP routing tables.  Its primary use is to set up static routes to specific
       hosts or networks via an interface after it has been configured with the ifconfig(8) program.

       When  the  add  or del options are used, route modifies the routing tables.  Without these options, route
       displays the current contents of the routing tables.

简译:Route 用于操作 内核IP的路由表。它的主要作用是设置静态路由,当访问某个特定主机或者网段时通过路由配置的主机 访问。

         当后面接add 或者del 命令时 ,修改路由规则;没有add 或者del 时 列出当前路由表。

3.2 命令参数详解

[root@linux ~]# route [-nee]
[root@linux ~]# route add [-net|-host] [网段或主机] netmask [mask] [gw|dev]
[root@linux ~]# route del [-net|-host] [网段或主机] netmask [mask] [gw|dev]
观察的参数:
   -n,不要使用通信协议或主机名称,直接使用 IP 或 Port Number;
   -ee,使用更详细的信息来显示;
增加 (add) 与删除 (del) 路由的相关参数;
   -net,表示后面接的路由为一个网段;
   -host,表示后面接的为连接到单台主机的路由;
   Netmask,与网段有关,可以设置 netmask 决定网段的大小;
   Gw,gateway 的简写,后续接的是 IP 的数值,与 dev 不同;
   Dev,如果只是要指定由哪一块网卡联机出去,则使用这个设置,后面接 eth0 等。


 

 3.3 route 命令输出参数解释

[root@test_1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0
192.168.200.0   192.168.2.1     255.255.255.0   UG    0      0        0 eth0
192.178.200.0   192.168.2.1     255.255.255.0   UG    0      0        0 eth0
0.0.0.0         192.168.2.1     0.0.0.0         UG    0      0        0 eth0
[root@test_1 ~]# 
[root@test_1 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.2.0     *               255.255.255.0   U     1      0        0 eth0
192.168.200.0   192.168.2.1     255.255.255.0   UG    0      0        0 eth0
192.178.200.0   192.168.2.1     255.255.255.0   UG    0      0        0 eth0
default         192.168.2.1     0.0.0.0         UG    0      0        0 eth0

· Destination、Genmask:这两个术语就分别是Network与Netmask了。所以这两个东西就组合成为一个完整的网段了。

· Gateway:该网段是通过哪个Gateway连接出去的?如果显示0.0.0.0表示该路由是直接由本机传送,亦即可以通过局域网的MAC直接传输;如果有显示IP的话,表示该路由需要经过路由器(网关)的帮忙才能够传送出去。

· Flags:总共有多个标记,代表的意义如下。

Ø U(route is up):该路由是启动的。

Ø H(target is a host):目标是一台主机(IP)而非网段。

Ø G(use gateway):需要通过外部的主机来传递数据包。

Ø R(reinstate route for dynamic routing):使用动态路由时,恢复路由信息的标记。

Ø D(dynamically installed by daemon or redirect):已经由服务器或转port功能设置为动态路由。

Ø M(modified from routing daemon or redirect):路由已经被修改了。

Ø!(reject route):这个路由将不会被接受(用来阻止不安全的网段)。

· Iface:这个路由传递数据包的接口。

 3.4 route add del 示例

[root@test_1 ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.2.0 * 255.255.255.0 U 1 0 0 eth0
192.168.200.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0
192.178.200.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0
default 192.168.2.1 0.0.0.0 UG 0 0 0 eth0
[root@test_1 ~]# route del -net 192.168.200.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0
[root@test_1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.2.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
192.178.200.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0
[root@test_1 ~]# route add -net 192.168.200.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth0
[root@test_1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.2.0 0.0.0.0 255.255.255.0 U 1 0 0 eth0
192.168.200.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0
192.178.200.0 192.168.2.1 255.255.255.0 UG 0 0 0 eth0
0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0

 

   问题:目前我用vpn 访问google,但是如何设置route ,当我访问非www.google.com 时用原先的网络,而访问

www.google.com 时用vpn?

环境:主机 WIN7  

转载于:https://www.cnblogs.com/tacg/p/4128338.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值