您的位置:首页 > 理论基础 > 计算机网络

第5章 Linux 常用网络指令

2016-10-25 10:51 330 查看
网络参数设定使用的指令
手动/自动设定与启动/关闭 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=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
ether 00:0c:29:7f:dd:91  txqueuelen 1000  (Ethernet)
RX packets 0  bytes 0 (0.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 0  bytes 0 (0.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 0  (Local Loopback)
RX packets 192  bytes 16704 (16.3 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 192  bytes 16704 (16.3 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
ether 52:54:00:df:92:2a  txqueuelen 0  (Ethernet)
RX packets 0  bytes 0 (0.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 0  bytes 0 (0.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


查看特定网卡信息

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


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

virbr0:就是网络卡的代号,也有 lo 这个 loopback ;
ether(HWaddr):就是网络卡的(硬件)地址,俗称的 MAC ;
inet addr:IPv4 的 IP 地址
MTU:最大传输单元
RX:那一行代表的是网络由启动到目前为止的封包接收情况, packets 代表封包数、 errors 代表封包发
4000
生错误的数量、  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 8000
[root@localhost network-scripts]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 8000
inet 192.168.100.100  netmask 255.255.255.128  broadcast 192.168.100.127
inet6 fe80::20c:29ff:fe7f:dd91  prefixlen 64  scopeid 0x20<link>
ether 00:0c:29:7f:dd:91  txqueuelen 1000  (Ethernet)
RX packets 19  bytes 3469 (3.3 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 27  bytes 4038 (3.9 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
inet 127.0.0.1  netmask 255.0.0.0
inet6 ::1  prefixlen 128  scopeid 0x10<host>
loop  txqueuelen 0  (Local Loopback)
RX packets 576  bytes 50112 (48.9 KiB)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 576  bytes 50112 (48.9 KiB)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
ether 52:54:00:df:92:2a  txqueuelen 0  (Ethernet)
RX packets 0  bytes 0 (0.0 B)
RX errors 0  dropped 0  overruns 0  frame 0
TX packets 0  bytes 0 (0.0 B)
TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


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

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

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 8000
inet 192.168.50.50  netmask 255.255.255.0  broadcast 192.168.50.255
ether 00:0c:29:7f:dd:91  txqueuelen 1000  (Ethernet)

……
……


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

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

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
……

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
……


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    100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.247.0   0.0.0.0         255.255.255.0   U     100    0        0 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    100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.247.0   0.0.0.0         255.255.255.0   U     100    0        0 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    100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 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    100    0        0 eth0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.247.0   0.0.0.0         255.255.255.0   U     0      0        0 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
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:7f:dd:91 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT
link/ether 52:54:00:df:92:2a brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT qlen 500
link/ether 52:54:00:df:92:2a brd ff:ff:ff:ff:ff:ff

[root@localhost 桌面]# ip -s link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
link/ether 00:0c:29:7f:dd:91 brd ff:ff:ff:ff:ff:ff
RX: bytes  packets  errors  dropped overrun mcast
28101      216      0       0       0       0
TX: bytes  packets  errors  dropped carrier collsns
21633      223      0       0       0       0


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

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

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

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


修改网络卡代号、MAC 等参数
修改前先关闭接口

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


关于额外的 IP 相关设定: ip address
 
 
 
 
 
 
 
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: