Press "Enter" to skip to content

转载: Ubuntu20.04简单修改IP

在网上搜到的ubuntu改IP都是通过手写interfaces文件,这需要记住语法和格式,很不方便,在Redhat系上,可以使用很方便的NetworkManager的nmtui的Text UI来设置IP,我们只需要填写我们希望的网络信息即可,那么在ubuntu上其实也是有简便的方法;

ubuntu 20使用的netplan来管理,其配置文件使用的YAML,虽然语法格式也简单,但是还是可以有不用记的方式来完成IP的修改;

默认情况下的配置文件在/etc/netplan/00-installer-config.yaml,默认使用DHCP的方式,全部内容为

# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
      dhcp4: true
  version: 2

Linux上,大部分软件都包含了自己的文档,常规的包含README,Example之类的,目录在/usr/share/doc/netplan下,有个examples目录,当中包含了很多网络的配置文件模板,如

$ ls /usr/share/doc/netplan/examples/
bonding_router.yaml   direct_connect_gateway_ipv6.yaml  route_metric.yaml                           windows_dhcp_server.yaml
bonding.yaml          direct_connect_gateway.yaml       source_routing.yaml                         wireless.yaml
bridge_vlan.yaml      ipv6_tunnel.yaml                  static_multiaddress.yaml                    wpa_enterprise.yaml
bridge.yaml           loopback_interface.yaml           static_singlenic_multiip_multigateway.yaml
dhcp_wired8021x.yaml  modem.yaml                        static.yaml
dhcp.yaml             network_manager.yaml              vlan.yaml

找到static.yaml就是我们想要的静态IP配置文件模板,现成的格式和内容;拷贝到/etc/netplan下即可;

# 先备份源文件
sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak
# 拷贝静态IP配置文件
sudo cp /usr/share/doc/netplan/examples/static.yaml /etc/netplan/

# 简单修改一下;它的默认是这样的
network:
  version: 2
  renderer: networkd
  ethernets:
    enp3s0:
      addresses:
        - 10.10.10.2/24
      nameservers:
        search: [mydomain, otherdomain]
        addresses: [10.10.10.1, 1.1.1.1]
      routes: 
        - to: default
          via: 10.10.10.1

修改完成后,应用即可

sudo netplan apply

本文转载自八宝叠云峰.

Leave a Reply

Your email address will not be published. Required fields are marked *