Linux基础之网络配置

举报
江湖有缘 发表于 2023/04/04 12:22:35 2023/04/04
【摘要】 Linux基础之网络配置

@TOC

一、查看网络配置

1.查看网络接口状态

[root@node1 ~]# ifconfig ens224
ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.11  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::1344:2c57:66a9:99a6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:bc  txqueuelen 1000  (Ethernet)
        RX packets 139628  bytes 15557286 (14.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 156135  bytes 20632958 (19.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


2.查看网关路由信息

[root@node1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.200.2   0.0.0.0         UG    100    0        0 ens160
0.0.0.0         192.168.8.1     0.0.0.0         UG    101    0        0 ens224
0.0.0.0         192.168.9.1     0.0.0.0         UG    102    0        0 ens256
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.8.0     0.0.0.0         255.255.255.0   U     101    0        0 ens224
192.168.9.0     0.0.0.0         255.255.255.0   U     102    0        0 ens256
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.200.0   0.0.0.0         255.255.255.0   U     100    0        0 ens160

3.查看dns信息

[root@node1 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
nameserver 192.168.200.150
nameserver 192.168.8.1
nameserver 192.168.9.1
[root@node1 ~]# 

二、ifconfig网络管理工具使用

1.查看已激活网卡信息

[root@node1 ~]# ifconfig 
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:3bff:fe27:6e  prefixlen 64  scopeid 0x20<link>
        ether 02:42:3b:27:00:6e  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 28  bytes 3955 (3.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.200.135  netmask 255.255.255.0  broadcast 192.168.200.255
        inet6 fe80::79d4:c66c:4dfd:8050  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:b2  txqueuelen 1000  (Ethernet)
        RX packets 63413  bytes 4855353 (4.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 51247  bytes 3972067 (3.7 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens224: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.8.11  netmask 255.255.255.0  broadcast 192.168.8.255
        inet6 fe80::1344:2c57:66a9:99a6  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:bc  txqueuelen 1000  (Ethernet)
        RX packets 141667  bytes 15779234 (15.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 158353  bytes 20924018 (19.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens256: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.9.11  netmask 255.255.255.0  broadcast 192.168.9.255
        inet6 fe80::98f4:d12d:141:ed83  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:4e:fd:c6  txqueuelen 1000  (Ethernet)
        RX packets 20102  bytes 4249700 (4.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 14190  bytes 1560464 (1.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 1000  (Local Loopback)
        RX packets 3834  bytes 280418 (273.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3834  bytes 280418 (273.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethe519a37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::98e4:e9ff:feda:7799  prefixlen 64  scopeid 0x20<link>
        ether 9a:e4:e9:da:77:99  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 6321 (6.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethef87674: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::3c42:a8ff:fef3:e316  prefixlen 64  scopeid 0x20<link>
        ether 3e:42:a8:f3:e3:16  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 6321 (6.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:00:3f:0a  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

2.查看所有网卡(包括未激活)网卡信息

[root@node1 ~]# ifconfig -a

3.设置网卡激活及禁用

[root@node1 ~]# ifconfig ens224 up
[root@node1 ~]# ifconfig ens224 down

4.配置网卡IP地址

[root@node1 ~]# ifconfig ens224 172.25.25.21/24

三、nmcli网络管理工具使用

1.新加网卡配置文件

[root@node1 ~]# nmcli connection add ifname ens224 type ethernet con-name ens224-con224 ipv4.addresses  192.168.8.11/24 ipv4.gateway 192.168.8.1 ipv4.dns 192.168.8.1 ipv4.method manual
[root@node1 ~]# nmcli connection reload 
[root@node1 ~]# nmcli connection up ens224-con224 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@node1 ~]# 

2.修改网卡配置文件

[root@node1 ~]# nmcli connection modify ens224-con224 ipv4.addresses 192.168.8.11/24 ipv4.gateway 192.168.8.1 ipv4.dns 192.168.8.1 ipv4.method manual 
[root@node1 ~]# nmcli connection reload 
[root@node1 ~]# nmcli connection up ens224-con224 

3.新加网卡IP地址

nmcli connection modify ens224-con224 +ipv4.addresses 192.168.8.111/24
[root@node1 ~]# nmcli con up ens224-con224 
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/10)
[root@node1 network-scripts]# ip addr show ens224
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.11/24 brd 192.168.8.255 scope global noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet 192.168.8.111/24 brd 192.168.8.255 scope global secondary noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet6 fe80::1344:2c57:66a9:99a6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
[root@node1 network-scripts]#

4.查看网卡配置文件状态

[root@node1 network-scripts]# nmcli connection show 
NAME           UUID                                  TYPE      DEVICE  
docker0        ff8dae9c-631f-4e9d-961b-d5e09c85dbaa  bridge    docker0 
ens160         142fe7fb-a214-497f-a638-accac6a8407d  ethernet  ens160  
ens224-con224  1244a360-7311-472f-b6bd-25186c342c67  ethernet  ens224  
ens224-con256  a87770e8-3b7a-4cb4-9fbe-45bc08388cb8  ethernet  ens256  
virbr0         d7a40851-e7dc-477d-b479-e26eb4895d2b  bridge    virbr0  
[root@node1 network-scripts]# 

5.查看网卡设备状态

[root@node1 network-scripts]# nmcli device  status 
DEVICE       TYPE      STATE      CONNECTION    
ens160       ethernet  connected  ens160        
ens224       ethernet  connected  ens224-con224 
docker0      bridge    connected  docker0       
ens256       ethernet  connected  ens224-con256 
virbr0       bridge    connected  virbr0        
vethe519a37  ethernet  unmanaged  --            
vethef87674  ethernet  unmanaged  --            
lo           loopback  unmanaged  --            
virbr0-nic   tun       unmanaged  --            
[root@node1 network-scripts]# 

6.连接或断开网卡设备

[root@node1 network-scripts]# nmcli device disconnect/connect ens224

四、IP网络管理工具使用

1.查看网卡设备信息

[root@node1 network-scripts]# ip link  show 
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:4e:fd:b2 brd ff:ff:ff:ff:ff:ff
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:4e:fd:bc brd ff:ff:ff:ff:ff:ff
4: ens256: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 00:0c:29:4e:fd:c6 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default 
    link/ether 02:42:3b:27:00:6e brd ff:ff:ff:ff:ff:ff
7: vethef87674@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default 
    link/ether 3e:42:a8:f3:e3:16 brd ff:ff:ff:ff:ff:ff link-netnsid 1
9: vethe519a37@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP mode DEFAULT group default 
    link/ether 9a:e4:e9:da:77:99 brd ff:ff:ff:ff:ff:ff link-netnsid 0
10: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff
11: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff

2.激活或禁用网卡

[root@node1 network-scripts]# ip link set ens224 up/down

3.查看网络地址

[root@node1 network-scripts]# ip addr
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:b2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.200.135/24 brd 192.168.200.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::79d4:c66c:4dfd:8050/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: ens224: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.11/24 brd 192.168.8.255 scope global noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet 192.168.8.111/24 brd 192.168.8.255 scope global secondary noprefixroute ens224
       valid_lft forever preferred_lft forever
    inet6 fe80::1344:2c57:66a9:99a6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: ens256: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:4e:fd:c6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.11/24 brd 192.168.9.255 scope global noprefixroute ens256
       valid_lft forever preferred_lft forever
    inet6 fe80::98f4:d12d:141:ed83/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:3b:27:00:6e brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:3bff:fe27:6e/64 scope link 
       valid_lft forever preferred_lft forever
7: vethef87674@if6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 3e:42:a8:f3:e3:16 brd ff:ff:ff:ff:ff:ff link-netnsid 1
    inet6 fe80::3c42:a8ff:fef3:e316/64 scope link 
       valid_lft forever preferred_lft forever
9: vethe519a37@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether 9a:e4:e9:da:77:99 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::98e4:e9ff:feda:7799/64 scope link 
       valid_lft forever preferred_lft forever
10: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:00:3f:0a 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
11: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:00:3f:0a brd ff:ff:ff:ff:ff:ff
[root@node1 network-scripts]# 

4.网卡新增IP和删除IP地址

[root@node1 network-scripts]# 
[root@node1 network-scripts]# ip addr add 192.168.8.222/24 dev ens224
[root@node1 network-scripts]# ip addr del 192.168.8.222/24 dev ens224

5.管理路由表

①查看路由表

[root@node1 ~]# ip route show
default via 192.168.200.2 dev ens160 proto static metric 100 
default via 192.168.9.1 dev ens256 proto static metric 102 
default via 192.168.8.1 dev ens224 proto static metric 103 
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.11 metric 103 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.111 metric 103 
192.168.9.0/24 dev ens256 proto kernel scope link src 192.168.9.11 metric 102 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
192.168.200.0/24 dev ens160 proto kernel scope link src 192.168.200.135 metric 100

②.新增静态路由

[root@node1 ~]# ip route add 192.168.120.0/24 via 192.168.8.1 
[root@node1 ~]# ip route 
default via 192.168.200.2 dev ens160 proto static metric 100 
default via 192.168.9.1 dev ens256 proto static metric 102 
default via 192.168.8.1 dev ens224 proto static metric 103 
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.11 metric 103 
192.168.8.0/24 dev ens224 proto kernel scope link src 192.168.8.111 metric 103 
192.168.9.0/24 dev ens256 proto kernel scope link src 192.168.9.11 metric 102 
192.168.120.0/24 via 192.168.8.1 dev ens224 
192.168.122.0/24 dev virbr0 proto kernel scope link src 192.168.122.1 linkdown 
192.168.200.0/24 dev ens160 proto kernel scope link src 192.168.200.135 metric 100 
[root@node1 ~]# 

③.删除路由

[root@node1 ~]# ip route del 192.168.120.0/24 via 192.168.8.1

五、网络其他命令

1.查看网卡配置文件

[root@node1 ~]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# ls
ifcfg-ens160  ifcfg-ens224-con224  ifcfg-ens224-con256
[root@node1 network-scripts]# cat ifcfg-ens224-con224 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.8.11
PREFIX=24
GATEWAY=192.168.8.1
DNS1=192.168.8.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens224-con224
UUID=1244a360-7311-472f-b6bd-25186c342c67
DEVICE=ens224
ONBOOT=yes
IPADDR1=192.168.8.111
PREFIX1=24
[root@node1 network-scripts]# 

2.查看及设置主机名

[root@node1 network-scripts]# hostname
node1

[root@node1 network-scripts]# hostnamectl set-hostname test
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。