网络配置命令

设置网卡IP地址

ifconfig命令

我们可以使用ifconfig命令来设置网卡的ip地址,使用方法: ifconfig 接口 IP地址 [netmask 子网掩码]

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@CHJ-20190520VPS:/# ifconfig
eth3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.34.113 netmask 255.255.255.240 broadcast 172.31.34.127
inet6 fe80::4c47:3429:b72c:5130 prefixlen 64 scopeid 0x0<global>
ether 7e:15:d8:27:73:38 (Ethernet)
...

# 修改ip
root@CHJ-20190520VPS:/# ifconfig eth3 172.31.34.118
root@CHJ-20190520VPS:/# ifconfig
eth3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.34.113 netmask 255.255.255.240 broadcast 172.31.34.127
inet6 fe80::4c47:3429:b72c:5130 prefixlen 64 scopeid 0x0<global>
ether 7e:15:d8:27:73:38 (Ethernet)
...

# 修改ip并指定子网掩码
root@CHJ-20190520VPS:/# ifconfig eth3 172.31.34.118 netmask 255.255.255.0
root@CHJ-20190520VPS:/# ifconfig
eth3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.31.34.118 netmask 255.255.255.0 broadcast 172.31.34.127
inet6 fe80::4c47:3429:b72c:5130 prefixlen 64 scopeid 0x0<global>
ether 7e:15:d8:27:73:38 (Ethernet)
...

启动和关闭网卡

启动和关闭网卡动作和我们Windows中的是一样的,这两个操作其实是在一些特殊情况来使用网卡操作的命令,一般是网卡的配置被改乱了,我们希望恢复到默认的配置,可能需要这两个命令,一般情况下我们是不需要使用这两个命令的

启动命令

以下都分别对用两种工具包的命令,使用哪一种都可以

ifconfig 接口 up

ifup 接口

关闭命令

ipconfig 接口 down

ifdown 接口

网关配置命令

添加网关网段

使用route add命令,使用方法:

  • route add [-net | -host] 目的网络或主机 gw 网关ip

  • route add [-net | -host] dev 网卡接口

  • route add -net 指定网段 netmask 子网掩码 eth0

  • route add -net 指定网段 netmask 子网掩码 gw 网关ip

-net是指定网段,-host是指定ip

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 添加主机路由
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route add -host 192.168.1.2 dev eth0

[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.2 * 255.255.255.255 UH 0 0 0 eth0
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 添加网络路由 10.20.30.40 ~ 255.255.255.248
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route add -net 10.20.30.40 netmask 255.255.255.248 eth0
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.2 * 255.255.255.255 UH 0 0 0 eth0
10.20.30.40 * 255.255.255.248 U 0 0 0 eth0
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 添加默认路由 default ~ 192.168.1.2
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route add default gw 192.168.1.2
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.2 * 255.255.255.255 UH 0 0 0 eth0
10.20.30.40 * 255.255.255.248 U 0 0 0 eth0
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 192.168.1.2 0.0.0.0 UG 0 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

删除网关网段

使用route del命令,使用方法:

  • route del [-net | -host] 目的网络或主机 gw 网关ip

  • route del [-net | -host] dev 网卡接口

  • route del -net 指定网段 netmask 子网掩码 eth0

  • route del -net 指定网段 netmask 子网掩码 gw 网关ip

使用示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.2 * 255.255.255.255 UH 0 0 0 eth0
10.20.30.40 * 255.255.255.248 U 0 0 0 eth0
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 192.168.1.2 0.0.0.0 UG 0 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 删除主机路由
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route del -host 192.168.1.2 dev eth0
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.20.30.40 * 255.255.255.248 U 0 0 0 eth0
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 192.168.1.2 0.0.0.0 UG 0 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 删除默认路由
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route del -net default gw 192.168.1.2
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.20.30.40 * 255.255.255.248 U 0 0 0 eth0
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 删除网络路由
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route del -net 10.20.30.40 netmask 255.255.255.248 eth0
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
172.31.240.0 * 255.255.240.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
default 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

修改网关地址

如果我们要修改默认网关,需要先把网关删除,然后再进行添加

使用示例:

将 169.254.0.0 的默认网关修改为 192.168.1.2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.2 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
172.31.240.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 169.254.0.0 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

# 先删除
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route del -net default gw 169.254.0.0

# 然后添加
[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route add default gw 192.168.1.2

[root@iZm5ehzqow4ijp2ya2g2drZ etc]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.1.2 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
172.31.240.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.2 0.0.0.0 UG 0 0 0 eth0
0.0.0.0 172.31.255.253 0.0.0.0 UG 0 0 0 eth0

最后更新: 2019年08月20日 20:06

原始链接: https://jjw-story.github.io/2019/08/12/网络配置/

× 请我吃糖~
打赏二维码