江湖道

庙堂,江湖,学术!

返回顶部

linux网络管理之网络参数设置

(1)ifconfig:查询、设置网卡与IP网络等相关参数。

(2)ifup、ifdown:启动,关闭网卡

(3)route:查看配置路由表

(4)ip:整合式命令,可以直接修改上述描述的参数

1.ifconfig

功能:手动启动、查看与修改网络接口的相关参数。

语法:ifconfig {interface} {options}

interface:网卡接口名称。

options:可以使用的参数:

            up,down:启动关闭网卡

            mtu:设置不同的mtu值(注解:https://www.cnblogs.com/keystone/p/11410659.html

            netmask:子网掩码

    broadcast:广播地址

例1:查看所有的网络接口

[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.180  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::20c:29ff:fe4f:64e7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 44964  bytes 36769279 (35.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26366  bytes 3381897 (3.2 MiB)
        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 1  (Local Loopback)
        RX packets 232  bytes 27098 (26.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 232  bytes 27098 (26.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

列出eth0网卡的信息

[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.180  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::20c:29ff:fe4f:64e7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 45056  bytes 36776524 (35.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26417  bytes 3387497 (3.2 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

例2:修改网口IP

[root@localhost ~]# ifconfig eth0 192.168.42.200

Connection closed by foreign host.

Disconnected from remote host(192.168.42.180:22) at 22:25:57.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ ssh 192.168.42.200


Connecting to 192.168.42.200:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Wed Nov 20 21:44:49 2019 from 192.168.42.1 
[root@localhost ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.200  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::20c:29ff:fe4f:64e7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 45320  bytes 36802465 (35.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26544  bytes 3403897 (3.2 MiB)
        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 1  (Local Loopback)
        RX packets 232  bytes 27098 (26.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 232  bytes 27098 (26.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig eth0 192.168.42.200 netmask 255.255.255.0 gateway 192.168.42.2 broadcast 192.168.42.255 mtu 1000
[root@localhost ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.200  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::20c:29ff:fe4f:64e7  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 45998  bytes 36872352 (35.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 26965  bytes 3457938 (3.2 MiB)
        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 1  (Local Loopback)
        RX packets 232  bytes 27098 (26.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 232  bytes 27098 (26.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@localhost ~]# ifconfig eth0 mtu 800
[root@localhost ~]# 
[root@localhost ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 800
        inet 192.168.42.180  netmask 255.255.255.0  broadcast 192.168.42.255
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 46105  bytes 36881632 (35.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 27012  bytes 3464246 (3.3 MiB)
        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 1  (Local Loopback)
        RX packets 232  bytes 27098 (26.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 232  bytes 27098 (26.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
绑定IP
[root@localhost ~]# ifconfig eth0:0 192.168.42.201 [root@localhost ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 800 inet 192.168.42.180 netmask 255.255.255.0 broadcast 192.168.42.255 ether 00:0c:29:4f:64:e7 txqueuelen 1000 (Ethernet) RX packets 46210 bytes 36890938 (35.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 27071 bytes 3472671 (3.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 800 inet 192.168.42.201 netmask 255.255.255.0 broadcast 192.168.42.255 ether 00:0c:29:4f:64:e7 txqueuelen 1000 (Ethernet) 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 1 (Local Loopback) RX packets 232 bytes 27098 (26.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 232 bytes 27098 (26.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 800 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4f:64:e7 brd ff:ff:ff:ff:ff:ff inet 192.168.42.180/24 brd 192.168.42.255 scope global dynamic eth0 valid_lft 1713sec preferred_lft 1713sec inet 192.168.42.200/24 brd 192.168.42.255 scope global secondary eth0 valid_lft forever preferred_lft forever inet 192.168.42.201/24 brd 192.168.42.255 scope global secondary eth0:0 valid_lft forever preferred_lft forever
关闭网卡
[root@localhost ~]# ifconfig eth0:0 down [root@localhost ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 800 inet 192.168.42.180 netmask 255.255.255.0 broadcast 192.168.42.255 ether 00:0c:29:4f:64:e7 txqueuelen 1000 (Ethernet) RX packets 46345 bytes 36902353 (35.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 27146 bytes 3486767 (3.3 MiB) 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 1 (Local Loopback) RX packets 232 bytes 27098 (26.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 232 bytes 27098 (26.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

以上设置都是临时生效,重启网卡后即失效

例3:启动和关闭网卡

[root@localhost ~]# ifup eth0
[root@localhost ~]# 
[root@localhost ~]# ifdown  eth0

该命名实际上是执行的脚本文件,执行命令后,会到/etc/sysconfig/network-script/下找对应网口文件。若网口文件不存在,则也就使用不了此命令。

2.route

例4:查看路由

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.2    0.0.0.0         UG    100    0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
Destination,Genmask:network与netmask,两个值确定了一个网络。
Gateway:该网络是通过那个网关出去的,如果现实0.0.0.0表示该路由是直接由本机传送,也就是可以通过局域网的MAC直接发送;如果现实IP则表示路由需要经过路由(网关)的帮忙才能发送出去
Flags:U:该路由是启动的。
H:目标是一台主机而非网络
    G:需要通过外部的主机来传递数据包。
    R:使用动态路由时,恢复路由标志
    D:动态路由
Iface:路由传递数据包的接口

例5:路由的增加与删除
在192.168.42.180节点上绑定一个10.0.0.120的地址
[root@localhost ~]# ifconfig eth0:0 10.0.0.120 [root@localhost ~]# [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 800 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4f:64:e7 brd ff:ff:ff:ff:ff:ff inet 192.168.42.180/24 brd 192.168.42.255 scope global dynamic eth0 valid_lft 1716sec preferred_lft 1716sec inet 10.0.0.120/8 brd 10.255.255.255 scope global eth0:0 valid_lft forever preferred_lft forever
在192.168.42.181上添加路由后访问
[root@localhost ~]# route add -net 10.0.0.0/24 dev eth0 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.42.2 0.0.0.0 UG 100 0 0 eth0 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.42.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0 [root@localhost ~]# ping 10.0.0.120 PING 10.0.0.120 (10.0.0.120) 56(84) bytes of data. 64 bytes from 10.0.0.120: icmp_seq=1 ttl=64 time=0.832 ms 64 bytes from 10.0.0.120: icmp_seq=2 ttl=64 time=0.369 ms ^C --- 10.0.0.120 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1008ms rtt min/avg/max/mdev = 0.369/0.600/0.832/0.232 ms

[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.42.2 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.42.2 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.42.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
删除路由
[root@localhost ~]# route del default gw 192.168.42.2
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.42.2 0.0.0.0 UG 100 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.42.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0

3.ip

语法:ip [option] [动作] [命令]

选项与参数:

option:设置的参数,主要有:

  -s:显示出设备的统计数据,例如:接收到数据包的总数等。

操作:

  link:与设备相关的设置,包括MTU,MAC地址等

  addr/address:多个IP的实现

  route:与路由有关的相关设置

3.1 关于接口设备的相关设置

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

例6:显示本机所有的接口信息

[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    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:14:b5:f9 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip 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:14:b5:f9 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:14:b5:f9 brd ff:ff:ff:ff:ff:ff
    RX: bytes  packets  errors  dropped overrun mcast   
    27137997   24688    0       0       0       0       
    TX: bytes  packets  errors  dropped carrier collsns 
    856945     10922    0       0       0       0 

例7:网卡的启动与关闭

[root@localhost ~]# ip link set eth0:0 down

Connection closed by foreign host.

Disconnected from remote host(192.168.42.180:22) at 23:27:50.

Type `help' to learn how to use Xshell prompt.

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

例8:ip命令设置mtu

[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.181  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::20c:29ff:fe14:b5f9  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:14:b5:f9  txqueuelen 1000  (Ethernet)
        RX packets 24942  bytes 27160114 (25.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11046  bytes 871704 (851.2 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 1  (Local Loopback)
        RX packets 30  bytes 1622 (1.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30  bytes 1622 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ip link set eth0 mtu 1000
[root@localhost ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1000
        inet 192.168.42.181  netmask 255.255.255.0  broadcast 192.168.42.255
        ether 00:0c:29:14:b5:f9  txqueuelen 1000  (Ethernet)
        RX packets 25015  bytes 27166560 (25.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 11083  bytes 876951 (856.3 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 1  (Local Loopback)
        RX packets 30  bytes 1622 (1.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30  bytes 1622 (1.5 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

例9:修改网卡名与mac

[root@localhost ~]# ip link set eth0 name aaa
RTNETLINK answers: Device or resource busy
[root@localhost ~]# ip link set eth1 down
[root@localhost ~]# ip link set eth1 name aaa
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 800 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:4f:64:e7 brd ff:ff:ff:ff:ff:ff
3: aaa: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000
    link/ether 00:0c:29:4f:64:f1 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip link set aaa  address aa:aa:aa:aa:aa:aa
[root@localhost ~]# ip link show 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 800 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
    link/ether 00:0c:29:4f:64:e7 brd ff:ff:ff:ff:ff:ff
3: aaa: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000
    link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff

3.2 关于额外IP的相关设定:

主要是在设置与ip有关的各项参数,包括netmask、broadcast等。

例10:查看ip参数

[root@localhost ~]# ip address show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 800 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:4f:64:e7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.42.180/24 brd 192.168.42.255 scope global dynamic eth0
       valid_lft 1731sec preferred_lft 1731sec
3: aaa: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 800 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:4f:64:e7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.42.180/24 brd 192.168.42.255 scope global dynamic eth0
       valid_lft 1726sec preferred_lft 1726sec
3: aaa: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether aa:aa:aa:aa:aa:aa brd ff:ff:ff:ff:ff:ff

例11:添加网络接口

[root@localhost ~]# ip addr add 192.168.42.200/24 dev eth0 label eth0:0
[root@localhost ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 800
        inet 192.168.42.180  netmask 255.255.255.0  broadcast 192.168.42.255
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 48674  bytes 37107043 (35.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28250  bytes 3636665 (3.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 800
        inet 192.168.42.200  netmask 255.255.255.0  broadcast 0.0.0.0
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)

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 1  (Local Loopback)
        RX packets 234  bytes 27309 (26.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 234  bytes 27309 (26.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

例12:删除添加的接口

[root@localhost ~]# ip addr del 192.168.42.200/24 dev eth0 label eth0:0
[root@localhost ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 800
        inet 192.168.42.180  netmask 255.255.255.0  broadcast 192.168.42.255
        ether 00:0c:29:4f:64:e7  txqueuelen 1000  (Ethernet)
        RX packets 48807  bytes 37118961 (35.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28326  bytes 3647272 (3.4 MiB)
        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 1  (Local Loopback)
        RX packets 234  bytes 27309 (26.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 234  bytes 27309 (26.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3.3关于路由的相关设定

例13 :显示当前的路由信息

[root@localhost ~]# ip route show
default via 192.168.42.2 dev eth0 proto static metric 100 
192.168.42.0/24 dev eth0 proto kernel scope link src 192.168.42.180 metric 100 

例14:添加路由,主要是本机直接可联通的网络

在192.168.42.180上添加10.0.0.10网口
[root@localhost ~]# ip addr add 10.0.0.10/24 dev eth0 label eth0:0 [root@localhost ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 800 inet 192.168.42.180 netmask 255.255.255.0 broadcast 192.168.42.255 ether 00:0c:29:4f:64:e7 txqueuelen 1000 (Ethernet) RX packets 49183 bytes 37152239 (35.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 28542 bytes 3673127 (3.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 800 inet 10.0.0.10 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:4f:64:e7 txqueuelen 1000 (Ethernet) 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 1 (Local Loopback) RX packets 234 bytes 27309 (26.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 234 bytes 27309 (26.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

在192.168.42.181上添加静态路由

[root@localhost ~]# ip route add 10.0.0.0/24 dev eth0
[root@localhost ~]# ip route show
default via 192.168.42.2 dev eth0 proto static metric 100 
10.0.0.0/24 dev eth0 scope link 
192.168.42.0/24 dev eth0 proto kernel scope link src 192.168.42.181 metric 100
[root@localhost ~]# ping 10.0.0.10
PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data.
64 bytes from 10.0.0.10: icmp_seq=1 ttl=64 time=0.458 ms
64 bytes from 10.0.0.10: icmp_seq=2 ttl=64 time=0.269 ms
^C
--- 10.0.0.10 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.269/0.363/0.458/0.096 ms

例15 增加可以通往外部的路由

[root@localhost ~]# ip route add 192.168.192.0/24 via 192.168.42.181 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.2    0.0.0.0         UG    100    0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.192.0   192.168.42.181  255.255.255.0   UG    0      0        0 eth0

例16:增加静态路由

[root@localhost ~]# ip route add default via 192.168.42.2 dev eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.2    0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.42.2    0.0.0.0         UG    100    0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.192.0   192.168.42.181  255.255.255.0   UG    0      0        0 eth0

例17:删除路由

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.2    0.0.0.0         UG    0      0        0 eth0
0.0.0.0         192.168.42.2    0.0.0.0         UG    100    0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
192.168.192.0   192.168.42.181  255.255.255.0   UG    0      0        0 eth0
[root@localhost ~]# ip route del 192.168.192.0/24
[root@localhost ~]# ip route del default
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.42.2    0.0.0.0         UG    100    0        0 eth0
192.168.42.0    0.0.0.0         255.255.255.0   U     100    0        0 eth0
posted @ 2019-11-21 00:10  大江东流水  阅读(1298)  评论(0编辑  收藏  举报