此文参考:

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

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

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

一 网络参数设置命令

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

1.1 man手册定义

  1. DESCRIPTION
  2. Ifconfig is used to configure the kernel-resident network interfaces. It is used at boot time to set up
  3. interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is
  4. needed.
  5.  
  6. If no arguments are given, ifconfig displays the status of the currently active interfaces. If a single
  7. interface argument is given, it displays the status of the given interface only; if a single -a argument
  8. is given, it displays the status of all interfaces, even those that are down. Otherwise, it configures
  9. an interface.

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

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

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

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

1.2 命令输出简述

  1. [root@test_1 net]# ifconfig
  2. eth0 Link encap:Ethernet HWaddr :2B:2B::F7:7D
  3. inet addr:192.168.2.241 Bcast:192.168.2.255 Mask:255.255.255.0
  4. inet6 addr: fe80::862b:2bff:fe94:f77d/ Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  6. RX packets: errors: dropped: overruns: frame:
  7. TX packets: errors: dropped: overruns: carrier:
  8. collisions: txqueuelen:
  9. RX bytes: (719.1 MiB) TX bytes: (1.3 GiB)
  10. Interrupt:
  11.  
  12. eth0: Link encap:Ethernet HWaddr :2B:2B::F7:7D
  13. inet addr:192.168.2.242 Bcast:192.168.2.255 Mask:255.255.255.0
  14. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  15. Interrupt:
  16.  
  17. eth0: Link encap:Ethernet HWaddr :2B:2B::F7:7D
  18. inet addr:192.168.2.243 Bcast:192.168.2.255 Mask:255.255.255.0
  19. UP BROADCAST RUNNING MULTICAST MTU: Metric:
  20. Interrupt:
  21.  
  22. lo Link encap:Local Loopback
  23. inet addr:127.0.0.1 Mask:255.0.0.0
  24. inet6 addr: ::/ Scope:Host
  25. UP LOOPBACK RUNNING MTU: Metric:
  26. RX packets: errors: dropped: overruns: frame:
  27. TX packets: errors: dropped: overruns: carrier:
  28. collisions: txqueuelen:
  29. RX bytes: (50.6 KiB) TX bytes: (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地址用的命令

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

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

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

  1. [devtac@test_1 network-scripts]$ more /etc/rc.d/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don't
  6. # want to do the full Sys V style init stuff.
  7.  
  8. touch /var/lock/subsys/local
  9. xl2tpd -c /etc/xl2tpd.conf -D &
  10. ifconfig eth0: 192.168.2.242 netmask 255.255.255.0 up &
  11. ifconfig eth0: 192.168.2.243 netmask 255.255.255.0 up &

2. ifup ifdown

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

  1. The ifup and ifdown commands may be used to configure (or, respec- tively, deconfigure) network inter-
  2. faces based on interface definitions in the files /etc/sysconfig/network and /etc/sysconfig/network-
  3. scripts/ifcfg-<configuration>
  4.  
  5. These scripts take one argument normally: the name of the configuration (e.g. eth0). They are called with
  6. a second argument of "boot" during the boot sequence so that devices that are not meant to be brought up
  7. on boot (ONBOOT=no, see below) can be ignored at that time.

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

  1. [devtac@test_1 network-scripts]$ pwd
  2. /etc/sysconfig/network-scripts
  3. [devtac@test_1 network-scripts]$ ll
  4. 总用量
  5. -rw-r--r--. root root 8 : ifcfg-eth0
  6. -rw-r--r--. root root 1 ifcfg-lo

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

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

  1. [devtac@test_1 network-scripts]$ pwd
  2. /etc/sysconfig/network-scripts
  3. [devtac@test_1 network-scripts]$ ll ifup ifdown
  4. lrwxrwxrwx. root root 8 : ifdown -> ../../../sbin/ifdown
  5. lrwxrwxrwx. root root 8 : ifup -> ../../../sbin/ifup

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

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

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

3.route

3.1 man 手册定义

  1. DESCRIPTION
  2. Route manipulates the kernels IP routing tables. Its primary use is to set up static routes to specific
  3. hosts or networks via an interface after it has been configured with the ifconfig() program.
  4.  
  5. When the add or del options are used, route modifies the routing tables. Without these options, route
  6. displays the current contents of the routing tables.

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

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

3.2 命令参数详解

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

3.3 route 命令输出参数解释

  1. [root@test_1 ~]# route -n
  2. Kernel IP routing table
  3. Destination Gateway Genmask Flags Metric Ref Use Iface
  4. 192.168.2.0 0.0.0.0 255.255.255.0 U eth0
  5. 192.168.200.0 192.168.2.1 255.255.255.0 UG eth0
  6. 192.178.200.0 192.168.2.1 255.255.255.0 UG eth0
  7. 0.0.0.0 192.168.2.1 0.0.0.0 UG eth0
  8. [root@test_1 ~]#
  9. [root@test_1 ~]# route
  10. Kernel IP routing table
  11. Destination Gateway Genmask Flags Metric Ref Use Iface
  12. 192.168.2.0 * 255.255.255.0 U eth0
  13. 192.168.200.0 192.168.2.1 255.255.255.0 UG eth0
  14. 192.178.200.0 192.168.2.1 255.255.255.0 UG eth0
  15. default 192.168.2.1 0.0.0.0 UG 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

常用linux 命令 -网络相关的更多相关文章

  1. # 常用linux 命令和相关问题解决

    最近试着自己部署了服务器,在unbantu的环境下 学习了很多新知识 也遇到了很多问题,现在腾出手了,总结一下 常用Linux命令 目录操作 pwd: 查看当前路径 cd: 移动 cd .. : 返回 ...

  2. 常用linux 命令 -字符串相关

    参考网络文章,个人工作总结 题记:一般对字符串的操作有以下几种:求长度,截取字符串,拼接字符串,找字符串中某个字符的索引 1 expr 命令 1.1 定义 man 手册 Print the value ...

  3. 77个常用Linux命令和工具

    77个常用Linux命令和工具 Linux管理员不能单靠GUI图形界面吃饭.这就是我们编辑这篇最实用Linux命令手册的原因.这个指南是特别为Linux管理员和系统管理员 设计的,汇集了最有用的一些工 ...

  4. Linux命令网络命令之netstat

    Linux命令网络命令之netstat 这一年感觉到技术上成长到了一个瓶颈.可能是感觉自己学的东西足够应付目前的工作了,因此精神上就产生了懈怠,不思进取.到了一个技术氛围不错的公司,有许多专业能力很不 ...

  5. F4NNIU 的常用 Linux 命令(2019-08-24)

    目录 F4NNIU 的常用 Linux 命令 停止防火墙 查看 IP 址 启动 deepin 的桌面 查看当前时区 查看 CPU 和内存信息 用户相关 日志 F4NNIU 的常用 Linux 命令 记 ...

  6. 常用Linux命令小结

    常用Linux命令小结 Linux下有很多常用的很有用的命令,这种命令用的多了就熟了,对于我来说,如果长时间没有用的话,就容易忘记.当然,可以到时候用man命令查看帮助,但是,到时候查找的话未免有些临 ...

  7. 常用Linux命令笔记

    任何脱离业务的架构都是耍流氓 只记录实际常用的Linux命令 常用Linux命令 查找安装路径: whereis nginx 查询nginx进程: ps aux|grep nginx 查看 CentO ...

  8. 【Linux基础】常用Linux命令: cd, cp, ls, mkdir, mv, rm, su, uname

    常用Linux命令:cd, cp, ls, mkdir, mv, rm, su, uname cd命令:切换当前工作目录至 dirName(目录参数) 其中 dirName 可为绝对路径或相对路径.若 ...

  9. 01_常用 Linux 命令的基本使用

    01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命令完成,如 磁盘操作.文件存取.目录操作.进程管理.文件权限 设定等 在职场中,大量的 服务器维护工作 都 ...

随机推荐

  1. gcc/linux内核中likely、unlikely和__attribute__(section(""))属性

    查看linux内核源码,你会发现有很多if (likely(""))...及if (unlikely(""))...语句,这些语句其实是编译器的一种优化方式,具 ...

  2. struts2中Ajax校验

    Ajax(Asynchronous JavaScript and Xml),整合了JavaScript,XML,CSS,DOM,Ajax引擎(XMLHttpRequest). JavaScript语言 ...

  3. mysql workbench如何把已有的数据库导出ER模型

    mysql workbench的特长是创建表结构的,然后在结构图中,圈圈点点,很容易就利用可视化方式把数据库建好,然后再导入到数据库服务器中,这种办法很效率.但是有时我们有一个需求,事先没有建表结构模 ...

  4. Typical EEG waveforms during sleep 睡眠状态下的几种典型EEG波形

    Sources: EEG Atlas

  5. [LeetCode] Largest Number 最大组合数

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. JavaScript基础语法

    首先,JavaScript的基本语法是以名为ECMAScript的伪语言定义的,理解ECMAScript的细节就是理解它在浏览器中实现的关键,目前大多数浏览器都遵循了ECMAScript第3版的,但是 ...

  7. PHP学习总结

    <?php /* PHP简介: PHP是什么:PHP是一种创建动态交互性站点的强有力的服务器端脚步语言 PHP代表Hypertext Preprocessor PHP是一种使用广泛的开源的脚本语 ...

  8. 数据库中char与varchar类型的区别

    在建立数据库表结构的时候,为了给一个String类型的数据定义一个数据库的数据库类型,一般参考的都是char或者varchar,这两种选择有时候让人很纠结,今天想总结一下它们两者的区别,明确一下选择塔 ...

  9. Mybatis传入参数类型为Map

    mybatis更新sql语句: <update id="publishT00_notice" parameterType="Map"> update ...

  10. apache.commons.compress 压缩,解压

    最近在一个前辈的指引下,开始研究apache.commons.都是网上找的,而且不会中文乱码,而且还可以在压缩包里面加一层文件夹 package my.test; import java.io.Buf ...