第21章 网络配置

2.ifconfig命令用于获取网卡配置与网络状态等信息

[root@dsrw ~]# ifconfig
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.11  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::8782:14ae:73c2:4971  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:65:c4:9c  txqueuelen 1000  (Ethernet)
        RX packets 758169  bytes 954511534 (910.2 MiB)
        RX errors 0  dropped 87735  overruns 0  frame 0
        TX packets 74323  bytes 5841261 (5.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3.netstat命令用于显示网络状态

1)显示系统网络状态中的所有连接信息

[root@dsrw ~]# netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 192.168.122.1:domain    0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN     
tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN   

2)显示网卡当前状态信息

[root@dsrw ~]# netstat -i 
Kernel Interface table
Iface             MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
bond0            1500       47      0     42 0            25      0      2      0 BMmU
ens160           1500     7469      0 100626 0           232      0      0      0 BMRU
ens192           1500     2171      0      0 0          1507      0      0      0 BMRU
lo              65536     1851      0      0 0          1851      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU

3)显示网络路由表状态信息

[root@dsrw ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         192.168.10.1    0.0.0.0         UG        0 0          0 ens160
default         192.168.10.1    0.0.0.0         UG        0 0          0 bond0
192.168.10.0    0.0.0.0         255.255.255.0   U         0 0          0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U         0 0          0 bond0
192.168.10.1    0.0.0.0         255.255.255.255 UH        0 0          0 ens160
192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

4)找到服务所对应的连接信息

[root@dsrw ~]# netstat -ap | grep ssh
tcp        0      0 0.0.0.0:ssh             0.0.0.0:*               LISTEN      1044/sshd           
tcp6       0      0 [::]:ssh                [::]:*                  LISTEN      1044/sshd           
unix  2      [ ACC ]     STREAM     LISTENING     46148    2056/gnome-keyring-  /run/user/0/keyring/ssh
unix  2      [ ACC ]     STREAM     LISTENING     139846   60423/gpg-agent      /var/lib/fwupd/gnupg/S.gpg-agent.ssh
unix  2      [ ]         STREAM     CONNECTED     29303    1044/sshd            
unix  3      [ ]         STREAM     CONNECTED     28790    1044/sshd     

5)ssh命令用于安全的远程连接服务器

[root@dsrw ~]# ssh 192.168.10.2
The authenticity of host '192.168.10.2 (192.168.10.2)' can't be established.
ECDSA key fingerprint is SHA256:hFlfjG/6A/hF+hqNMuW0p0gNEIedOpPVHo/bC7GkIxw.
Are you sure you want to continue connecting (yes/no)? 

6)ping命令用于测试主机间网络连通性

[root@dsrw ~]# ping -c 4 192.168.10.2
PING 192.168.10.2 (192.168.10.2) 56(84) bytes of data.
64 bytes from 192.168.10.2: icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from 192.168.10.2: icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from 192.168.10.2: icmp_seq=3 ttl=64 time=0.045 ms
64 bytes from 192.168.10.2: icmp_seq=4 ttl=64 time=0.059 ms

--- 192.168.10.2 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 59ms
rtt min/avg/max/mdev = 0.042/0.047/0.059/0.009 ms

4.route命令用来显示并设置Linux内核中的网络路由表,route命令设置的路由主要是静态路由。

1)显示路由信息

[root@dsrw ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.10.1    0.0.0.0         UG    102    0        0 ens160
default         192.168.10.1    0.0.0.0         UG    300    0        0 bond0
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     300    0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

2)添加一个网络

[root@dsrw ~]# route add -net 10.0.0.0/24 dev ens160
[root@dsrw ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.10.1    0.0.0.0         UG    102    0        0 ens160
default         192.168.10.1    0.0.0.0         UG    300    0        0 bond0
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     300    0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

3)指定网关添加网络

[root@dsrw ~]# route add -net 10.0.0.0/24 dev ens160 gw 10.0.0.254
[root@dsrw ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.10.1    0.0.0.0         UG    102    0        0 ens160
default         192.168.10.1    0.0.0.0         UG    300    0        0 bond0
10.0.0.0        10.0.0.254      255.255.255.0   UG    0      0        0 ens160
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     300    0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

4)添加一个主机IP

[root@dsrw ~]# route add -host 10.1.1.1 dev ens160
[root@dsrw ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.10.1    0.0.0.0         UG    102    0        0 ens160
0.0.0.0         192.168.10.1    0.0.0.0         UG    300    0        0 bond0
10.0.0.0        10.0.0.254      255.255.255.0   UG    0      0        0 ens160
10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 ens160
10.1.1.1        0.0.0.0         255.255.255.255 UH    0      0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     300    0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

5)删除网络

[root@dsrw ~]# route del -net 10.0.0.0/24
[root@dsrw ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.10.1    0.0.0.0         UG    102    0        0 ens160
default         192.168.10.1    0.0.0.0         UG    300    0        0 bond0
10.1.1.1        0.0.0.0         255.255.255.255 UH    0      0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     300    0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

6)删除主机

[root@dsrw ~]# route del -host 10.1.1.1
[root@dsrw ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.10.1    0.0.0.0         UG    102    0        0 ens160
default         192.168.10.1    0.0.0.0         UG    300    0        0 bond0
192.168.10.0    0.0.0.0         255.255.255.0   U     101    0        0 ens192
192.168.10.0    0.0.0.0         255.255.255.0   U     102    0        0 ens160
192.168.10.0    0.0.0.0         255.255.255.0   U     300    0        0 bond0
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

1 2

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称

    请登录后查看评论内容