linux系统网络设置!

Posted 龙少。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux系统网络设置!相关的知识,希望对你有一定的参考价值。

一.查看网络配置

1.使用ifconfig命令查看网络接口的信息

(1)ifconfig命令查看所有活动的网络接口信息

[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.128  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::3339:805:9ff3:5382  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:f7:67:91  txqueuelen 1000  (Ethernet)
        RX packets 10834  bytes 14982280 (14.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5283  bytes 343978 (335.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 1000  (Local Loopback)
        RX packets 356  bytes 30848 (30.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 356  bytes 30848 (30.1 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:b5:13:98  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

查看的信息详细说明
ens33:第一块以太网卡的名称,en表示以太网卡,s表示热插拔
inet:表示网路接口的IP地址
netmask:表示网络接口的子网掩码
broadcast:表示网络接口的所在网络的广播地址
inet6:表示IPv6
ether:表示网络接口的物理地址(MAC地址)
RX,TX:表示网路接口的接收发送的数据流量等信息

lo:表示loopback,回环网络接口
virbr0:表示虚拟网卡

(2)查看单个网路接口信息

ipconfig 网卡名 显示单个网卡信息

[root@localhost ~]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.128  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::3339:805:9ff3:5382  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:f7:67:91  txqueuelen 1000  (Ethernet)
        RX packets 10840  bytes 14982952 (14.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5289  bytes 344680 (336.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(3)查看所有网路接口信息

ifconfig -a 全部显示 包括活动和不活动的接口

[root@localhost ~]# ifconfig -a
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.206.128  netmask 255.255.255.0  broadcast 192.168.206.255
        inet6 fe80::3339:805:9ff3:5382  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:f7:67:91  txqueuelen 1000  (Ethernet)
        RX packets 10841  bytes 14983012 (14.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 5289  bytes 344680 (336.6 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 1000  (Local Loopback)
        RX packets 356  bytes 30848 (30.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 356  bytes 30848 (30.1 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:b5:13:98  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

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:b5:13:98  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

网路管理工具
NetworkManager
network

查看状态

[root@localhost ~]# systemctl status NetworkManager
● NetworkManager.service - Network Manager
   Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2021-05-24 10:16:20 CST; 4h 9min ago
     Docs: man:NetworkManager(8)
 Main PID: 6235 (NetworkManager)
    Tasks: 4
   CGroup: /system.slice/NetworkManager.service
           ├─6235 /usr/sbin/NetworkManager --no-daemon
           └─8991 /sbin/dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /v...

524 14:18:56 localhost.localdomain dhclient[8991]: DHCPREQUEST on ens33 t...
524 14:18:56 localhost.localdomain dhclient[8991]: DHCPACK from 192.168.2...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain NetworkManager[6235]: <info>  [1621837...
524 14:18:56 localhost.localdomain dhclient[8991]: bound to 192.168.206.1...
Hint: Some lines were ellipsized, use -l to show in full.

[root@localhost ~]# systemctl status network
● network.service - LSB: Bring up/down networking
   Loaded: loaded (/etc/rc.d/init.d/network; bad; vendor preset: disabled)
   Active: active (exited) since 一 2021-05-24 10:16:50 CST; 4h 10min ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0

524 10:16:50 localhost.localdomain systemd[1]: Starting LSB: Bring up/dow...
524 10:16:50 localhost.localdomain network[6549]: 正在打开环回接口: [  确…]
524 10:16:50 localhost.localdomain network[6549]: 正在打开接口 ens33: []
524 10:16:50 localhost.localdomain systemd[1]: Started LSB: Bring up/down...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# 

2.使用route命令查看路由表条目

route

route -n
可以将路由记录中的地址显示为数字形式,可以跳过解析主机名的过程,在路由条目较多的情况下能够加快执行速度。

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.206.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.206.2   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.206.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33


说明
目标网段是Default时,表示此行是默认网关记录
getway下一跳为*时,表示目标网段是与本机直接相连的

3.使用ip/ethtool命令查看网络接口

ip link
查看网络接口数据链路层信息

[root@localhost ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:f7:67:91 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:b5:13:98 brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:b5:13:98 brd ff:ff:ff:ff:ff:ff

ip address
查看网络接口网络层信息

[root@localhost ~]# ip address 
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:f7:67:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.206.128/24 brd 192.168.206.255 scope global noprefixroute dynamic ens33
       valid_lft 1033sec preferred_lft 1033sec
    inet6 fe80::3339:805:9ff3:5382/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:b5:13:98 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:b5:13:98 brd ff:ff:ff:ff:ff:ff

ethtool 网卡
查看指定网络接口的速率模式等信息

[root@localhost ~]# ethtool ens33
Settings for ens33:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	Speed: 1000Mb/s
	Duplex: Full
	Port: Twisted Pair
	PHYAD: 0
	Transceiver: internal
	Auto-negotiation: on
	MDI-X: off (auto)
	Supports Wake-on: d
	Wake-on: d
	Current message level: 0x00000007 (7)
			       drv probe link
	Link detected: yes

4.使用netstat命令查看网络连接情况

netstat命令

选项说明
netstat
-a 查看所有活动的网络连接信息
-u 查看udp协议的相关信息
-t 查看tcp协议的相关信息
-l 查看监听状态的网络连接及端口信息
-n 以数字形式显示相关的主机地址多扣信息
-p 显示与网路哦连接相关联的进程号进程名信息
-r 显示路由表信息

可以选项进行组合使用

netstat -a | head

[root@localhost ~]# netstat -a | head
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:sunrpc          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:x11             0.0.0.0:*               LISTEN     
tcp        0      0 localhost.locald: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     
tcp        0      0 localhost:smtp          0.0.0.0:*               LISTEN     
tcp6       0      0 [:以上是关于linux系统网络设置!的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

Android 逆向Linux 文件权限 ( Linux 权限简介 | 系统权限 | 用户权限 | 匿名用户权限 | 读 | 写 | 执行 | 更改组 | 更改用户 | 粘滞 )(代码片段

如何在linux系统中设置静态ip地址

构建调试Linux内核网络代码的环境MenuOS系统

linux网络设置

Linux系统编程—网络编程—socket代码实现