Linux网络配置

ifconfig命令介绍

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
[root@chenxi ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:68 
          inet addr:192.168.10.9  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fead:a368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11137 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6042 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:13446763 (12.8 MiB)  TX bytes:524726 (512.4 KiB)
 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:72 
          inet6 addr: fe80::20c:29ff:fead:a372/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:73 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:6300 (6.1 KiB)  TX bytes:468 (468.0 b)
 
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
 
[root@chenxi ~]# ifconfig eth1 down   临时禁用网卡
[root@chenxi ~]# ifconfig 查看启动
eth0      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:68 
          inet addr:192.168.10.9  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fead:a368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11211 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6088 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:13453039 (12.8 MiB)  TX bytes:535146 (522.6 KiB)
 
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
[root@chenxi ~]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:68 
          inet addr:192.168.10.9  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fead:a368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11244 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6104 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:13455989 (12.8 MiB)  TX bytes:537540 (524.9 KiB)
 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:72 
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:82 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7128 (6.9 KiB)  TX bytes:468 (468.0 b)
 
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
[root@chenxi ~]# ifconfig eth1 up   启用网卡
[root@chenxi ~]# ifconfig eth1 192.168.10.12/24    临时设置IP
[root@chenxi ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:68 
          inet addr:192.168.10.9  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fead:a368/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:673 errors:0 dropped:0 overruns:0 frame:0
          TX packets:478 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:62885 (61.4 KiB)  TX bytes:61507 (60.0 KiB)
 
eth1      Link encap:Ethernet  HWaddr 00:0C:29:AD:A3:72 
          inet addr:192.168.10.12  Bcast:192.168.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fead:a372/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:58 errors:0 dropped:0 overruns:0 frame:0
          TX packets:37 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:4956 (4.8 KiB)  TX bytes:6030 (5.8 KiB)
 
lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

 路由配置,注意防火墙iptables

A B主机互通

A主机IP 192.168.9.15
A路由 192.168.9.12
A路由 192.168.10.9
B路由 192.168.10.30
B路由 192.168.8.128
B主IP 192.168.8.5

 

 

 

 

 

 

 

 

A主机添加默认网关,即默认路由

1
2
3
4
5
6
[root@mail ~]# route add default gw 192.168.9.12   添加默认网关
[root@mail ~]# route -n   查看路由表
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.9.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.9.12    0.0.0.0         UG    0      0        0 eth0

B 主机添加默认网关,即默认路由

1
2
3
4
5
6
[root@mail ~]#  route add default gw 192.168.8.128
[root@mail ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.8.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.8.128   0.0.0.0         UG    0      0        0 eth0

A路由添加即

1
2
3
4
5
6
7
8
9
10
[root@chenxi ~]# route add -net 192.168.8.0/24 gw 192.168.10.30
[root@chenxi ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.9.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.8.0     192.168.10.30   255.255.255.0   UG    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.10.2    0.0.0.0         UG    0      0        0 eth0
[root@chenxi ~]#  echo "1" > /proc/sys/net/ipv4/ip_forward

  

B路由添加

1
2
3
4
5
6
7
8
9
[root@mast ~]# route add -net 192.168.9.0/24 gw 192.168.10.9
[root@mast ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.10.2    0.0.0.0         UG    102    0        0 ens33
192.168.8.0     0.0.0.0         255.255.255.0   U     101    0        0 ens37
192.168.9.0     192.168.10.9    255.255.255.0   UG    0      0        0 ens33
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens33
[root@mast ~]# echo "1" > /proc/sys/net/ipv4/ip_forward

A主机pingB主机

1
2
3
4
5
6
7
8
9
10
[root@mail ~]# ping  -c 4 192.168.8.5
PING 192.168.8.5 (192.168.8.5) 56(84) bytes of data.
64 bytes from 192.168.8.5: icmp_seq=1 ttl=62 time=3.61 ms
64 bytes from 192.168.8.5: icmp_seq=2 ttl=62 time=1.35 ms
64 bytes from 192.168.8.5: icmp_seq=3 ttl=62 time=1.46 ms
64 bytes from 192.168.8.5: icmp_seq=4 ttl=62 time=1.33 ms
 
--- 192.168.8.5 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3008ms
rtt min/avg/max/mdev = 1.330/1.941/3.619/0.970 ms

B主机pingA主机

1
2
3
4
5
6
7
8
9
10
[root@mail ~]#  ping  -c 4  192.168.9.15
PING 192.168.9.15 (192.168.9.15) 56(84) bytes of data.
64 bytes from 192.168.9.15: icmp_seq=1 ttl=62 time=1.63 ms
64 bytes from 192.168.9.15: icmp_seq=2 ttl=62 time=1.38 ms
64 bytes from 192.168.9.15: icmp_seq=3 ttl=62 time=1.87 ms
64 bytes from 192.168.9.15: icmp_seq=4 ttl=62 time=1.56 ms
 
--- 192.168.9.15 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3009ms
rtt min/avg/max/mdev = 1.381/1.614/1.876/0.184 ms

删路由和默认路由

1
2
[root@mail ~]# route del -net 0.0.0.0/0 gw 192.168.9.12
[root@mail ~]# route del default gw 192.168.9.12

主机A将路由信息添加至配置文件永久生效

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
[root@chenxi ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ad:a3:68 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.9/24 brd 192.168.10.255 scope global eth0
    inet6 fe80::20c:29ff:fead:a368/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:ad:a3:72 brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.12/24 brd 192.168.9.255 scope global eth1
    inet6 fe80::20c:29ff:fead:a372/64 scope link
       valid_lft forever preferred_lft forever
 
[root@chenxi ~]# vim /etc/sysconfig/network-scripts/route-eth1 数据包到192.168.8.0网络从eth1 出去后缀名写eth1 名
 
192.168.8.0/24 via 192.168.10.30
[root@chenxi ~]# service network restart
正在关闭接口 eth0:                                        [确定]
正在关闭接口 eth1:                                        [确定]
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0: Determining if ip address 192.168.10.9 is already in use for device eth0...
                                                           [确定]
弹出界面 eth1: Determining if ip address 192.168.9.12 is already in use for device eth1...
                                                           [确定]
[root@chenxi ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.9.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.8.0     192.168.10.30   255.255.255.0   UG    0      0        0 eth0
0.0.0.0         192.168.10.2    0.0.0.0         UG    0      0        0 eth0
[root@chenxi ~]# echo "1" > /proc/sys/net/ipv4/ip_forward

   B路由的添加默认路由

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
[root@mast ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:40:c2:01 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.30/24 brd 192.168.10.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::64e7:c159:5d4d:5104/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
3: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:40:c2:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.128/24 brd 192.168.8.255 scope global noprefixroute ens37
       valid_lft forever preferred_lft forever
    inet6 fe80::fb06:bc9:d34f:f1a4/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@mast ~]# vim /etc/sysconfig/network-scripts/route-ens33  数据包到192.168.9.0/24  网络从ens33 网卡出去
 
192.168.9.0/24 via 192.168.10.9  
[root@mast ~]# systemctl restart network
[root@mast ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.10.2    0.0.0.0         UG    100    0        0 ens33
192.168.8.0     0.0.0.0         255.255.255.0   U     101    0        0 ens37
192.168.10.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@mast ~]#  echo "1" > /proc/sys/net/ipv4/ip_forward

  B主机添加路由

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
[root@mail ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:8b:16:7f brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.5/24 brd 192.168.8.255 scope global eth0
    inet6 fe80::20c:29ff:fe8b:167f/64 scope link
       valid_lft forever preferred_lft forever
[root@mail ~]# vi /etc/sysconfig/network-scripts/route-eth0
0.0.0.0/24 via 192.168.8.128
[root@mail ~]# service network restart
正在关闭接口 eth0:                                        [确定]
关闭环回接口:                                             [确定]
弹出环回接口:                                             [确定]
弹出界面 eth0: Determining if ip address 192.168.8.5 is already in use for device eth0...
                                                           [确定]
[root@mail ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.128   255.255.255.0   UG    0      0        0 eth0
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.8.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.10.2    0.0.0.0         UG    0      0        0 eth0

   netstat命令介绍与ss

 查看链接状态-n表示链接-t表示tcp

1
2
3
4
[root@mail ~]# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0     52 192.168.8.5:22              192.168.8.1:12902           ESTABLISHED

 查看udp端口-u 

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
[root@mail ~]# netstat -n
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0      0 192.168.8.5:22              192.168.8.1:12902           ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ]         DGRAM                    9832   /var/run/portreserve/socket
unix  7      [ ]         DGRAM                    9869   /dev/log
[root@mail ~]# netstat -ln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN     
tcp        0      0 :::22                       :::*                        LISTEN     
udp        0      0 0.0.0.0:993                 0.0.0.0:*                              
udp        0      0 0.0.0.0:995                 0.0.0.0:*                              
udp        0      0 0.0.0.0:110                 0.0.0.0:*                              
udp        0      0 0.0.0.0:143                 0.0.0.0:* 
[root@mail ~]# netstat -lnut
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State     
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN     
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN     
tcp        0      0 :::22                       :::*                        LISTEN     
udp        0      0 0.0.0.0:993                 0.0.0.0:*                              
udp        0      0 0.0.0.0:995                 0.0.0.0:*                              
udp        0      0 0.0.0.0:110                 0.0.0.0:*                              
udp        0      0 0.0.0.0:143                 0.0.0.0:*                         

  -p 查看哪个服务打开的端口

1
2
3
4
5
6
7
8
9
10
[root@mail ~]# netstat -lnutp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1291/sshd          
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN      1374/master        
tcp        0      0 :::22                       :::*                        LISTEN      1291/sshd          
udp        0      0 0.0.0.0:993                 0.0.0.0:*                               1200/portreserve   
udp        0      0 0.0.0.0:995                 0.0.0.0:*                               1200/portreserve   
udp        0      0 0.0.0.0:110                 0.0.0.0:*                               1200/portreserve   
udp        0      0 0.0.0.0:143                 0.0.0.0:*                               1200/portreserve   

  -nr 查看路由表

1
2
3
4
5
6
7
8
[root@mail ~]# netstat -nr
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.8.128   255.255.255.0   UG        0 0          0 eth0
192.168.10.0    0.0.0.0         255.255.255.0   U         0 0          0 eth0
192.168.8.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0
0.0.0.0         192.168.10.2    0.0.0.0         UG        0 0          0 eth0

  

 

 

  

 

  

 

posted @   烟雨楼台,行云流水  阅读(323)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示