Ubuntu配置网络

        对于习惯了CentOS的道友来说,Ubuntu可能不太熟悉,两者的配置网络方式不太一样,早期Ubuntu的网卡配置文件在/etc/network/interfaces,现在Ubuntu使用的是netplan,接下来就给大家分享一下Ubuntu怎么配置静态IP和动态IP。

动态IP

ethernets下面的ens33是我网卡的名字,根据实际情况换成自己网卡的名字。

root@ubuntu:~# 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:
        ens33:
            dhcp4: true
    version: 2

配置生效

root@ubuntu:~# netplan apply

静态IP

root@ubuntu:~# 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:
        ens33:
            dhcp4: false
            addresses: [192.168.207.135/24]
            gateway4: 192.168.207.2
            nameservers:
              addresses: [114.114.114.114]
    version: 2

配置生效

这个显示是说我们配置的gateway4被弃用了(其实配置上了),可以采用新写法

root@ubuntu:~# netplan apply

** (generate:1858): WARNING **: 04:53:04.650: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:1857): WARNING **: 04:53:04.927: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

** (process:1857): WARNING **: 04:53:05.055: `gateway4` has been deprecated, use default routes instead.
See the 'Default routes' section of the documentation for more details.

新写法

这就不报错了,添加了一个renderer配置,该配置设置为networkd,表示使用network,networkd是systemd-networkd的简写,它是systemd的一部分,用于配置和管理网络接口。

大多数Linux都有networkd,不加默认也是这样,也可以设置为NetworkManager,使用NetworkManager接管网络。参考你该不会还不知道nmcli吧_nmcli从啥时候开始启动的-CSDN博客

root@ubuntu:~# 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:
    renderer: networkd  
    ethernets:
        ens33:
            dhcp4: false
            addresses: [192.168.207.135/24]
            routes:
              - to: default
                via: 192.168.207.2
            nameservers:
              addresses: [114.114.114.114]
    version: 2

root@ubuntu:~# netplan apply

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值