51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

静态配置网卡IP,centos和ubuntu实现

  1. centos8/ubuntu18.4实现网络基本配置
    1.1. 网络配置方式
    静态指定:
    ifconfig, route, netstat
    ip: object {link, addr, route}, ss, tc
    system-config-network-tui,setup
    配置文件
    动态分配:
    DHCP: Dynamic Host Configuration Protocol

1.2. centos网络基本配置
1.2.1. 手动网络配置
1.2.1.1. IP、MASK、GW、DNS相关的配置文件
文件路径

/etc/sysconfig/network-scripts/ifcfg-xxx

文件参数说明

TYPE 接口类型;常见有的Ethernet, Bridge
NAME 此配置文件应用到的设备
DEVICE 设备名
HWADDR 对应的设备的MAC地址
UUID 设备的惟一标识
BOOTPROTO 激活此设备时使用的地址配置协议,常用的dhcp, static, none, bootp
IPADDR 指明IP地址
NETMASK 子网掩码,如:255.255.255.0
PREFIX 网络ID的位数, 如:24
GATEWAY 默认网关
DNS1 第一个DNS服务器地址
DNS2 第二个DNS服务器地址
DOMAIN 主机不完整时,自动搜索的域名后缀
ONBOOT 在系统引导时是否激活此设备
USERCTL 普通用户是否可控制此设备
PEERDNS
如果BOOTPROTO的值为"dhcp",YES将允许dhcp server分配的dns服务
器信息直接覆盖至/etc/resolv.conf文件,NO不允许修改resolv.conf

配置IP:192.168.100.30/24、网关:192.168.100.2、DNS:114.114.114.114

[02:58:50 root@localhost yum.repos.d]#vim /etc/sysconfig/network-scripts/ifcfg-ens32 
[04:15:55 root@localhost yum.repos.d]#cat /etc/sysconfig/network-scripts/ifcfg-ens32
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.30
PREFIX=24
GATEWAY=192.168.100.2
DNS1=114.114.114.114
`[04:16:06 root@localhost yum.repos.d]#nmcli con reload && nmcli con up ens32
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/7)
[04:21:50 root@localhost yum.repos.d]#hostname -I
192.168.100.30 192.168.122.1
`

验证配置是否成功

#验证IP地址
[04:22:12 root@localhost yum.repos.d]#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: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:35:fb:a9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.30/24 brd 192.168.100.255 scope global noprefixroute ens32
       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:ca:51:ba brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global noprefixroute virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 52:54:00:ca:51:ba brd ff:ff:ff:ff:ff:ff

[04:22:50 root@localhost yum.repos.d]#ping 114.114.114.114 PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data. 64 bytes from 114.114.114.114: icmp_seq=1 ttl=128 time=47.7 ms 64 bytes from 114.114.114.114: icmp_seq=2 ttl=128 time=47.2 ms 64 bytes from 114.114.114.114: icmp_seq=3 ttl=128 time=47.7 ms

#验证DNS [20:48:28 root@localhost ~]#cat /etc/resolv.conf

Generated by NetworkManager

nameserver 114.114.114.114


1.2.2. 命令配置网络
网络配置工具:nmcli

#修改配置文件执行生效
nmcli con reload
nmcli con up con-name
#查看帮助
nmcli con add help
#使用nmcli配置网络
nmcli con show
#显示所有活动连接
nmcli con show --active
#显示网络连接配置
nmcli con show "System eth0"
#显示设备状态
nmcli dev status
#显示网络接口属性
nmcli dev show eth0
#创建新连接default,IP自动通过dhcp获取
nmcli con add con-name default type Ethernet ifname eth0
#删除连接
nmcli con del default
#创建新连接static ,指定静态IP,不自动连接
nmcli con add con-name static ifname eth0 autoconnect no type Ethernet
ipv4.addresses 172.25.X.10/24 ipv4.gateway 172.25.X.254

修改IP等属性格式

nmcli connection modify IFACE [+|-]setting.property value
setting.property: ipv4.addresses ipv4.gateway ipv4.dns1 ipv4.method 
manual |
auto

例子

nmcli con mod "static" connection.autoconnect no
nmcli con mod "static" ipv4.dns 192.168.100.100
nmcli con mod "static" +ipv4.dns 114.114.114.114
nmcli con mod "static" +ipv4.dns 8.8.8.8
nmcli con mod "static" ipv4.addresses "192.168.100.30/24 
192.168.100.2"
nmcli con mod "static" +ipv4.addresses 192.168.100.50/24

案例:nmcli配置静态IP_192.168.100.100/24 网关_192.168.100.2 DNS_114.114.114.114/8.8.8.8

#查看当前网络连接
[04:23:21 root@localhost yum.repos.d]#nmcli connection show
NAME    UUID                                  TYPE      DEVICE 
ens32   152beb06-47c5-c5e8-95a9-385590654382  ethernet  ens32  
virbr0  7437dcde-d816-48c6-b835-676e6ea29553  bridge    virbr0 
#查看当前网络配置并删除,重新创建空文件
[22:21:52 root@localhost ~]#cat /etc/sysconfig/network-scripts/ifcfg- 
ens32 
NAME=ens32
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.100.30
PREFIX=24
GATEWAY=192.168.100.2
DNS1=114.114.114.114
[22:38:41 root@localhost ~]#mv /etc/sysconfig/network-scripts/ifcfg-   
ens32 /etc/sysconfig/network-scripts/ens32.bak
[22:43:04 root@localhost ~]#ls /etc/sysconfig/network-scripts/
ens32.bak
[22:48:16 root@localhost ~]#touch /etc/sysconfig/network-
scripts/ifcfg-ens32
[22:48:44 root@localhost ~]#nmcli con mod ens32 ipv4.addresses 
192.168.100.100/24
[22:48:50 root@localhost ~]#nmcli con mod ens32 ipv4.dns 
114.114.114.114
[22:50:01 root@localhost ~]#nmcli con mod ens32 +ipv4.dns 8.8.8.8
[22:50:13 root@localhost ~]#nmcli con mod ens32 ipv4.gateway 
192.168.100.2
[22:50:54 root@localhost ~]#cat /etc/sysconfig/network-scripts/ifcfg-
ens32
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
IPADDR=192.168.100.100
PREFIX=24
GATEWAY=192.168.100.2
DNS1=114.114.114.114
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
NAME=ens32
UUID=152beb06-47c5-c5e8-95a9-385590654382
DEVICE=ens32
ONBOOT=yes
DNS2=8.8.8.8
[23:05:17 root@localhost ~]#nmcli device connect ens32;nmcli device 
reapply ens32
[D:\~]$ ssh root@192.168.100.100

Connecting to 192.168.100.100:22... Connection established. To escape to local shell, press Ctrl+Alt+].

ni hao ya zhang xue long Activate the web console with: systemctl enable --now cockpit.socket

Last login: Sun Aug 14 23:00:44 2022 from 192.168.100.1 Session lifetime based on X11 requested, but X11 initialization failed


1.3. Ubuntu网络配置
1.3.1. 手动网络配置
说明

Ubuntu18版本之后网卡配置文件采用YAML格式,必须以/etc/netplan/XXX.yaml 
文件命名方式存放可以每个网卡对应一个单独的配置文件,也可以将所有网卡都放在一个配置文件里
yaml文件格式:列表和键值对两种,并包含从属关系,严格控制缩进区分

网络配置文件

/etc/netplan/01-netcfg.yaml #Ubuntu18版本之后
/etc/network/interfaces #Ubuntu18版本之前

修改网卡配置文件后需执行命令生效:

netplan apply

关闭/启用以太网网卡

ifconfig ens33 down
ifconfig ens33 up

案例:配置静态IP

#注意格式缩进
#Ubuntu18版本之后格式:
network:
    version: 2
    renderer: networkd
    ethernets:
        eth0:
            addresses: [192.168.8.10/24,10.0.0.10/8] #或者用下面两行,两种格式不能混用
            - 192.168.8.10/24
            - 10.0.0.10/8
            gateway4: 10.0.0.2
            nameservers:
                search: [magedu.com, magedu.org]
                addresses: [180.76.76.76, 8.8.8.8, 1.1.1.1]

#Ubuntu18版本之前格式:

The primary network interface

auto eth0 iface eth0 inet static address 192.168.3.90 netmask 255.255.255.0 gateway 192.168.3.1 #network 192.168.3.0 #broadcast 192.168.3.255 #dns-nameservers 114.114.114.114,8.8.8.8


配置实操

root@zxl:~# hostname -I
192.168.100.35 
root@zxl:~# vim /etc/netplan/my-init.yaml
root@zxl:~# cat /etc/netplan/my-init.yaml
network:
 version: 2
 renderer: networkd
 ethernets:
  ens32:
   addresses:
    - 192.168.100.36/24
   gateway4: 192.168.100.2
   nameservers:
    addresses: [114.114.114.114, 8.8.8.8]
root@zxl:~# netplan apply      
                                 &lt;/code&gt;&lt;/pre&gt;

验证配置结果是否生效

[D:\~]$ ssh root@192.168.100.36

Connecting to 192.168.100.36:22... Connection established. To escape to local shell, press Ctrl+Alt+].

Welcome to Ubuntu 18.04 LTS (GNU/Linux 4.15.0-20-generic x86_64)

  • Documentation: https://help.ubuntu.com

  • Management: https://landscape.canonical.com

  • Support: https://ubuntu.com/advantage

System information as of Sun Aug 14 17:45:14 UTC 2022

System load: 0.0 Processes: 181 Usage of /: 1.7% of 99.95GB Users logged in: 1 Memory usage: 13% IP address for ens32: 192.168.100.36 Swap usage: 0%

0 packages can be updated. 0 updates are security updates.

Failed to connect to https://changelogs.ubuntu.com/meta-release-lts. Check your Internet connection or proxy settings

Last login: Sun Aug 14 17:01:27 2022 from 192.168.100.1 /usr/bin/xauth: file /root/.Xauthority does not exist root@zxl:~# hostname -I 192.168.100.36 root@zxl:~# ping 114.114.114.114 PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data. 64 bytes from 114.114.114.114: icmp_seq=1 ttl=128 time=48.2 ms 64 bytes from 114.114.114.114: icmp_seq=2 ttl=128 time=47.7 ms ^C --- 114.114.114.114 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1004ms rtt min/avg/max/mdev = 47.703/47.975/48.248/0.349 ms

#查看DNS: /etc/resolv.conf root@zxl:~# systemd-resolve --status Global DNSSEC NTA: 10.in-addr.arpa 16.172.in-addr.arpa 168.192.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.172.in-addr.arpa 22.172.in-addr.arpa 23.172.in-addr.arpa 24.172.in-addr.arpa 25.172.in-addr.arpa 26.172.in-addr.arpa 27.172.in-addr.arpa 28.172.in-addr.arpa 29.172.in-addr.arpa 30.172.in-addr.arpa 31.172.in-addr.arpa corp d.f.ip6.arpa home internal intranet lan local private test

Link 2 (ens32) Current Scopes: DNS LLMNR setting: yes MulticastDNS setting: no DNSSEC setting: no DNSSEC supported: no DNS Servers: 114.114.114.114 8.8.8.8 root@zxl:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.100.2 0.0.0.0 UG 0 0 0 ens32 192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens32

<br />

赞(4)
未经允许不得转载:工具盒子 » 静态配置网卡IP,centos和ubuntu实现