Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ubuntu 18.04 设置静态ip方法 #1

Open
zazitufu opened this issue Jun 12, 2020 · 3 comments
Open

Ubuntu 18.04 设置静态ip方法 #1

zazitufu opened this issue Jun 12, 2020 · 3 comments

Comments

@zazitufu
Copy link
Owner

zazitufu commented Jun 12, 2020

1,先查看配置的网卡ID和当前的IP
ifconfig
image
注意上面截图的画圈位置。

2、查看子网掩码写法
ip a
image

3、编辑配置文件
nano /etc/netplan/01-netcfg.yaml
下面内容填上,其中【addresses】字段参考上面第2步截图内容

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: no
      addresses: [192.168.137.10/24]
      gateway4: 192.168.137.1
      nameservers:
         addresses: [192.168.137.1,192.168.123.1,223.5.5.5]

保存并退出

4、应用变更内容
sudo netplan apply

@zazitufu
Copy link
Owner Author

如果要恢复动态IP,将此文件删掉,再运行一次
sudo netplan apply

@zazitufu
Copy link
Owner Author

不要修改这个文件
cat /etc/netplan/50-cloud-init.yaml

#如果在 Netplan 上使用 DHCP,配置文件看起来就像这样:
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# 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:
        eno1:
            dhcp4: true
    version: 2

参考文档:
https://askubuntu.com/questions/1029531/how-to-setup-a-static-ip-on-ubuntu-server-18-04

@zazitufu
Copy link
Owner Author

zazitufu commented Jun 12, 2020

这里有几点需要注意:
1、nano /etc/netplan/01-network-manager-all.yaml
将renderer: NetworkManager注释,否则netplan命令无法生效;
2、ip配置信息要按如上格式,使用yaml语法格式,每个配置项使用空格缩进表示层级;
3、对应配置项后跟着冒号,之后要接个空格,否则netplan命令也会报错。
————————————————
参考链接:https://blog.csdn.net/u010039418/article/details/80934346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant