Ubuntu22.04 Server版和Desktop

netplan工具

Netplan 是一个网络配置抽象化工具,它允许您在一个统一的YAML文件中定义网络接口配置,然后由Netplan将这些配置转换为底层网络驱动程序可以理解的格式。Netplan的设计目标是简化网络配置,并提供更好的集成和支持。

在Ubuntu 18.04及更高版本中,传统的/etc/network/interfaces文件和/etc/init.d/networking脚本已经不再是配置网络的首选方式。取而代之的是netplan,这是一个更现代化、更灵活的网络配置工具。

sudo netplan generate           # 生成与后端管理工具对应的配置;
sudo netplan apply              # 应用配置,必要时重启管理工具;
sudo netplan --debug apply      # 调试,返回错误信息;
sudo netplan get                # 获取当前 netplan 配置;
sudo netplan set                # 修改当前 netplan 的配置。

配置网卡区别

Ubuntu22.04 Desktop原始 /etc/netplan/01-network-manager-all.yaml

cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  • 参考配置
# Let NetworkManager manage all devices on this system
network:
    ethernets:
        ens32:                    ## network card name
            dhcp4: false
            addresses:
              - 192.168.3.88/24   ## set static IP
            routes:
              - to: default
                via: 192.168.3.1  ## gateway
            nameservers:
              addresses: [8.8.8.8,8.8.4.4,192.168.3.1]
    version: 2
  • 重启网络服务
systemctl status NetworkManager

systemctl restart NetworkManager

Ubuntu22.04 Server /etc/netplan/50-cloud-init.yaml

  • 默认配置
root@rjhcp-yyfwq:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.1 LTS
Release:        24.04
Codename:       noble

root@rjhcp-yyfwq:~# cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets: {}
    version: 2
  • 修改后配置
root@rjhcp-yyfwq:~# cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: false
      addresses:
        - 192.66.1.4/24
      routes:
        - to: default
          via: 192.66.1.254
root@rjhcp-yyfwq:~# 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 noprefixroute
       valid_lft forever preferred_lft forever
2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether fa:16:3e:29:9a:c2 brd ff:ff:ff:ff:ff:ff
    altname enp0s3
    inet 192.66.1.4/24 brd 192.66.1.255 scope global ens3
       valid_lft forever preferred_lft forever
    inet6 fe80::f816:3eff:fe29:9ac2/64 scope link
       valid_lft forever preferred_lft forever
  • 重启网络服务
netplan apply                          #验证配置是否正确
systemctl status systemd-networkd      #重启网络服务

临时配置IP

##添加临时IP
ip addr add 10.0.2.15/24 dev enp0s3
##添加网关
ip route add default via 10.0.2.1
##down/up端口
ip link set eno3 down
ip link set eno3 up

参考****
https://blog.csdn.net/kfepiza/article/details/127330588
https://www.cnblogs.com/laina/articles/17674155.html

posted @ 2024-11-21 20:52  *一炁化三清*  阅读(4)  评论(0编辑  收藏  举报