网络参数设定使用的指令

手动/自动设定与启动/关闭 IP 参数: ifconfig, ifup, ifdown

ifconfig :查询、设定网络卡与 IP 网域等相关参数;
ifup, ifdown:这两个档案是 script,透过更简单的方式来启动网络接口;
route :查询、设定路由表 (route table)
ip :复合式的指令, 可以直接修改上述提到的功能;

ifup 与 ifdown 仅能就/etc/sysconfig/network-scripts 内的 ifcfg-ethX (X 为数字) 进行启动或关闭的动作,并不能直接修改网络参数,除非手动调整 ifcfg-ethX 档案才行。至于 ifconfig 则可以直接手动给予某个接口 IP 或调整其网络参数.

[root@www ~]# ifconfig {interface} {up|down} <== 观察与启动接口
[root@www ~]# ifconfig interface {options} <== 设定与修改接口
选项与参数:
interface:网络卡接口代号,包括 eth0, eth1, ppp0 等等
options  :可以接的参数,包括如下:
up, down :启动 (up) 或关闭 (down) 该网络接口(不涉及任何参数)
mtu  :可以设定不同的 MTU 数值,例如 mtu 1500 (单位为 byte)
netmask  :就是子屏蔽网络;

broadcast:就是广播地址啊!

查看所有网络接口

[root@localhost 桌面]# ifconfig
eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
ether :0c::7f:dd: txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (16.3 KiB)
RX errors dropped overruns frame
TX packets bytes (16.3 KiB)
TX errors dropped overruns carrier collisions virbr0: flags=<UP,BROADCAST,MULTICAST> mtu
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether :::df::2a txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions

查看特定网卡信息

[root@localhost network-scripts]# ifconfig virbr0
virbr0: flags=<UP,BROADCAST,MULTICAST> mtu
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether :::df::2a txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions

由于是在虚拟机上安装的linux,因此存在一个虚拟网络接口virbri0。

virbr0:就是网络卡的代号,也有 lo 这个 loopback ;
ether(HWaddr):就是网络卡的(硬件)地址,俗称的 MAC ;
inet addr:IPv4 的 IP 地址
MTU:最大传输单元
RX:那一行代表的是网络由启动到目前为止的封包接收情况, packets 代表封包数、 errors 代表封包发生错误的数量、  dropped 代表封包由于有问题而遭丢弃的数量等等
TX:与 RX 相反,为网络由启动到目前为止的传送情况;
collisions:代表封包碰撞的情况,如果发生太多次, 表示你的网络状况不太好;
txqueuelen:代表用来传输数据的缓冲区的储存长度;
RX bytes, TX bytes:总接收、发送字节总量

修改网络卡信息

设定不同参数的网络接口,同时设定 MTU 的数值!

[root@localhost network-scripts]# ifconfig eth0 192.168.100.100 netmask 255.255.255.128 mtu
[root@localhost network-scripts]# ifconfig
eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.100.100 netmask 255.255.255.128 broadcast 192.168.100.127
inet6 fe80::20c:29ff:fe7f:dd91 prefixlen scopeid 0x20<link>
ether :0c::7f:dd: txqueuelen (Ethernet)
RX packets bytes (3.3 KiB)
RX errors dropped overruns frame
TX packets bytes (3.9 KiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
inet 127.0.0.1 netmask 255.0.0.0
inet6 :: prefixlen scopeid 0x10<host>
loop txqueuelen (Local Loopback)
RX packets bytes (48.9 KiB)
RX errors dropped overruns frame
TX packets bytes (48.9 KiB)
TX errors dropped overruns carrier collisions virbr0: flags=<UP,BROADCAST,MULTICAST> mtu
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether :::df::2a txqueuelen (Ethernet)
RX packets bytes (0.0 B)
RX errors dropped overruns frame
TX packets bytes (0.0 B)
TX errors dropped overruns carrier collisions

在该实体网卡上,再仿真一个网络接口,即是在一张网络卡上面设定多个 IP

[root@localhost network-scripts]# ifconfig eth0: 192.168.50.50
[root@localhost network-scripts]# ifconfig
eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.100.100 netmask 255.255.255.128 broadcast 192.168.100.127
inet6 fe80::20c:29ff:fe7f:dd91 prefixlen scopeid 0x20<link>
ether 00:0c:29:7f:dd:91 txqueuelen (Ethernet)
RX packets bytes (4.2 KiB)
RX errors dropped overruns frame
TX packets bytes (4.9 KiB)
TX errors dropped overruns carrier collisions eth0:: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.50.50 netmask 255.255.255.0 broadcast 192.168.50.255
ether 00:0c:29:7f:dd:91 txqueuelen (Ethernet) ……
……

以上在eth0网络卡上新建的网络接口的硬件信息(MAC)ether与原有的信息相同,因为他们是建立在同一张网络卡上的。

关闭该网络连接

[root@localhost network-scripts]# ifconfig eth0: down
[root@localhost network-scripts]# ifconfig
eth0: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.100.100 netmask 255.255.255.128 broadcast 192.168.100.127
inet6 fe80::20c:29ff:fe7f:dd91 prefixlen scopeid 0x20<link>
ether :0c::7f:dd: txqueuelen (Ethernet)
RX packets bytes (4.5 KiB)
RX errors dropped overruns frame
TX packets bytes (5.1 KiB)
TX errors dropped overruns carrier collisions lo: flags=<UP,LOOPBACK,RUNNING> mtu
…… virbr0: flags=<UP,BROADCAST,MULTICAST> mtu
……

ifup,ifdown

[root@www ~]# ifdown {interface}

[root@www ~]# ifup eth0

ifup 与 ifdown 真是太简单了!这两支程序其实是 script 而已,他会直接到/etc/sysconfig/network-scripts 目录下搜寻对应的配置文件,例如 ifup eno16777736时,他会找出 ifcfg-eno16777736这个档案的内容,然后来加以设定。

注意:当以ifconfig修改eth0并启动它后,再使用ifdown eth0将无法关闭它,因为ifdown将会比较eth0和ifcfg-eth0的参数,如果不同则放弃该动作。因此必须使用ifconfig eth0 down才能关闭

路由修改: route

[root@www ~]# route [-nee]
[root@www ~]# route add [-net|-host] [网域或主机] netmask [mask] [gw|dev]
[root@www ~]# route del [-net|-host] [网域或主机] netmask [mask] [gw|dev]
观察的参数:
  -n  :不要使用通讯协议或主机名,直接使用 IP 或 port number;
  -ee :使用更详细的信息来显示增加 (add) 与删除 (del) 路由的相关参数:
  -net  :表示后面接的路由为一个网域;
  -host  :表示后面接的为连接到单部主机的路由;
  netmask :与网域有关,可以设定 netmask 决定网域的大小;
  gw  :gateway 的简写,后续接的是 IP 的数值喔,与 dev 不同; net.qiang@hotmail.com
  dev  :如果只是要指定由那一块网络卡联机出去,则使用这个设定,后面接 eth0 等

单纯的观察路由状态

[root@localhost 桌面]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.247.2 0.0.0.0 UG eth0
192.168.122.0 0.0.0.0 255.255.255.0 U virbr0
192.168.247.0 0.0.0.0 255.255.255.0 U eth0
[root@localhost 桌面]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.247.2 0.0.0.0 UG eth0
192.168.122.0 0.0.0.0 255.255.255.0 U virbr0
192.168.247.0 0.0.0.0 255.255.255.0 U eth0
[root@localhost 桌面]#

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):需要透过外部的主机 (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:这个路由传递封包的接口。

增加和删除路由

[root@localhost 桌面]# route del -net 192.168.247.0 netmask 255.255.255.0 dev eth0
[root@localhost 桌面]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.247.2 0.0.0.0 UG eth0
192.168.122.0 0.0.0.0 255.255.255.0 U virbr0
[root@localhost 桌面]# route add -net 192.168.247.0 netmask 255.255.255.0 dev eth0
[root@localhost 桌面]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 192.168.247.2 0.0.0.0 UG eth0
192.168.122.0 0.0.0.0 255.255.255.0 U virbr0
192.168.247.0 0.0.0.0 255.255.255.0 U eth0

注意:路由的设定必须与你的网络互通。比如:route add  -net 192.168.200.0 netmask 255.255.255.0  gw 192.168.200.254因为我的主机内仅有 192.168.247.130这个 IP ,所以不能直接与192.168.200.254这个网段直接使用 MAC 互通!

网络参数综合指令: ip

[root@www ~]# ip [option] [动作] [指令]
选项与参数:
  option :设定的参数,主要有:
    -s :显示出该装置的统计数据(statistics),例如总接受封包数等;
    动作:亦即是可以针对哪些网络参数进行动作,包括有:
    link :关于装置 (device) 的相关设定,包括 MTU, MAC 地址等等
    addr/address :关于额外的 IP 协议,例如多 IP 的达成等等;
    route :与路由有关的相关设定

关于装置接口 (device) 的相关设定: ip link

ip link 可以设定与装置 (device) 有关的相关参数,包括 MTU 以及该网络接口的 MAC 等等,当然也可以启动 (up) 或关闭 (down) 某个网络接口啦!

[root@www ~]# ip [-s] link show <== 单纯的查阅该装置相关的信息
[root@www ~]# ip link set [device] [动作与参数]
选项与参数:
  show:仅显示出这个装置的相关内容,如果加上 -s 会显示更多统计数据;
  set :可以开始设定项目, device 指的是 eth0, eth1 等等界面代号;
  动作与参数:包括有底下的这些动作:
  up|down  :启动 (up) 或关闭 (down) 某个接口,其他参数使用默认的以太网络;
  address  :如果这个装置可以更改 MAC 的话,用这个参数修改!
  name  :给予这个装置一个特殊的名字;
  mtu  :就是最大传输单元啊!

显示所有接口信息

[root@localhost 桌面]# ip link show
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback ::::: brd :::::
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP mode DEFAULT qlen
link/ether :0c::7f:dd: brd ff:ff:ff:ff:ff:ff
: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu qdisc noqueue state DOWN mode DEFAULT
link/ether :::df::2a brd ff:ff:ff:ff:ff:ff
: virbr0-nic: <BROADCAST,MULTICAST> mtu qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen
link/ether :::df::2a brd ff:ff:ff:ff:ff:ff [root@localhost 桌面]# ip -s link show eth0
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP mode DEFAULT qlen
link/ether :0c::7f:dd: brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast TX: bytes packets errors dropped carrier collsns

启动、关闭与设定装置的相关信息

[root@www ~]# ip link set eth0 up

[root@www ~]# ip link set eth0 down

[root@www ~]# ip link set eth0 mtu 

修改网络卡代号、MAC 等参数

修改前先关闭接口

[root@localhost 桌面]# ip link set eth0 down
[root@localhost 桌面]# ip link set eth0 name wuchao
[root@localhost 桌面]# ip link show
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback ::::: brd :::::
: wuchao: <BROADCAST,MULTICAST> mtu qdisc pfifo_fast state DOWN mode DEFAULT qlen
link/ether :0c::7f:dd: brd ff:ff:ff:ff:ff:ff
: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu qdisc noqueue state DOWN mode DEFAULT
link/ether :::df::2a brd ff:ff:ff:ff:ff:ff
: virbr0-nic: <BROADCAST,MULTICAST> mtu qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen
link/ether :::df::2a brd ff:ff:ff:ff:ff:ff

关于额外的 IP 相关设定: ip address

最新文章

  1. WebApi - 路由
  2. SQL优化技术分析-4:其他
  3. [转]什么是SPI通信
  4. iscsi 怎么搞
  5. Easyui中使用jquery或js动态添加元素时出现的样式失效的解决方法
  6. 高性能滚动 scroll 及页面渲染优化
  7. GO语言练习:channel select 超时机制
  8. Codeforces 721D [贪心]
  9. django使用笔记
  10. 少年Vince之遐想
  11. 一个强大的LogParser的UI工具--logparserlizard简介(开源IIS日志分析工具)
  12. 学OpenGL的一些好的网站
  13. php在cli和cgi方式下获取服务器ip的实例
  14. 从0到1学习node(七)之express搭建简易论坛
  15. js小动画算法
  16. 自己做的notepad++ FTP同步插件
  17. ANSI控制码的说明
  18. NodeJS基础(二)
  19. WINDOWS系统中常用网络命令及命令实例详解
  20. Eclipse开发环境配置

热门文章

  1. PHP Ajax跨域问题解决办法
  2. JavaScript入门学习(2)--进度条
  3. 将Windows 7安装到移动固态硬盘(U盘)
  4. PTA基础编程题目集6-3简单求和 (函数题)
  5. SQL 去重 显示第一条数据 显示一条数据
  6. 20155211 实验三 敏捷开发与XP实践
  7. 20155215 2016-2017-2 《Java程序设计》第10周学习总结
  8. String类使用
  9. NUnit基本使用方法
  10. 微信小程序模板消息群发解决思路