当前位置:网站首页>Ubuntu Topic 5: Setting a Static IP Address
Ubuntu Topic 5: Setting a Static IP Address
2022-07-31 14:56:00 【python-code dr】
ubuntu20.04 Dynamic is used by defaultIP设置,But sometimes we need to set static for itIP.This article will guide you to fully understandubuntu20.04的IP设置方法.
networking 和 network-manager
Figure it out before you startnetworking服务和network-manager服务的区别.
networking是控制系统网络设置的,如果你修改了相关的网络配置需要重启此服务.
而network-manager是管理networking服务的一个图形化管理工具的后台服务程序.
为了继续下面的操作,需要大家安装一下net-tools,以及ifupdown,并重启一下系统.
sudo apt install -y ifupdown net-tools
init 6

联通性测试.
一切正常.

至此大家对network-managerIs there any impression?
继续.
通过修改/etc/network/interfaces 配置静态IP
Next, let's modify it``/etc/network/interfaces来配置ubuntu20.04`的静态IP地址.
Let's take a look at the previous oneIP地址.
vim /etc/network/interfaces
在文件interfaces中添加如下内容.
auto lo
iface lo inet loopback
auto ens33
iface ens33 inet static
address 192.168.2.117
netmask 255.255.255.0
gateway 192.168.2.1
此处的address修改成了’192.168.2.117’
I'll reboot firstnetwork-manager
Check the system againIP地址,You will find that nothing has changed.
Let's reboot againnetworking服务吧.
systemctl restart networking

Modified is shownIP了.
如果通过systemctl restart networkingThe modified version is also not displayed properlyIP的话,可以重启一下系统. I don't know if it is hereubuntu的bug
再用ip a查看一下
联通性测试
ping baidu.com

网络不通,The above information prompt is because there is no设置DNS.
修改/etc/systemd/resolved.conf而不是修改/etc/resolved.conf,如果修改/etc/resolved.conf,重启系统DNS会失效.
su
echo "DNS=192.168.2.1 8.8.8.8" >> /etc/systemd/resolved.conf
systemctl restart systemd-resolved
多个DNS之间用空格分开.
Restart the system and test again.
Modified so far/etc/network/interfaces file to configure staticallyIP的工作完成.
But the static modified by the above methodIPYou'll find one on the network icon in the upper right corner of the desktop"?".
对于有强迫症的人来说,will feel very uncomfortable.
解决方案一:Compromise solution–眼不见为净
systemctl stop network-manager
systemctl disable networ-manager
解决方案二:通过图形化界面`network-manager`设置静态IP
继续上面的步骤:先开启network-manager(系统默认是开启的,The invisible solution in the previous step turned it off.)
sudo systemctl enable network-manager
sudo systemctl start network-manager
Empty our frontinterfaces文件中的配置内容.
cat /dev/null > /etc/network/interfaces
重启系统.
init 6
The network icon in the upper right corner of the desktop reappeared,点击它.







通过ip a命令检验.
解决方案三(推荐):使用`netplan` 命令设置静态IP
ubuntu20.04推荐使用netplan命令设置静态IP地址.
netplan使用yamlFormat configuration file to configure network addresses.
vim /etc/netplan/01-network-manager-all.yaml
在01-network-manager-all.yaml文件中rendererAdd the following after that line.
ethernets:
ens33:
addresses: [192.168.2.116/24]
gateway4: 192.168.2.1
nameservers:
addresses: [192.168.2.1,8.8.8.8]
The finished file looks like this.
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
ethernets:
ens33:
addresses: [192.168.2.116/24]
gateway4: 192.168.2.1
nameservers:
addresses: [192.168.2.1,8.8.8.8]
注意yamlFiles are indented to control the relationship between the upper and lower levels,Your indentation can be one space or oneN个空格,But be consistent throughout the configuration file,And be careful to add a space after the colon.
让配置文件生效.
netplan apply
联通性测试.
OK
You can modify the configuration fileIP地址,通过netplan apply命令,The address can take effect immediately.
Set up several commands commonly used in the network
# 关闭网卡 ifdown <网卡名称>
sudo ifdown ens33
ifdown: unknown interface ens33 报错的解决方法,使用命令
sudo ip link set ens33 down
# 或者使用命令
sudo ifconfig ens33 down
#开启网卡 ifup <网卡名称>
sudo ifup ens33
ubuntu20.04中ifup: unknown interface ens33报错的解决方法,使用命令
sudo ip link set ens33 up
# 或者使用命令
sudo ifconfig ens33 up
An error occurred: 'NetplanApply' object has no attribute 'state' 报错的解决方法.使用命令
netplan try --state /etc/netplan
边栏推荐
- 谷歌CTS测试(cta测试)
- C language basic practice (nine-nine multiplication table) and printing different asterisk patterns
- 深入浅出边缘云 | 4. 生命周期管理
- OAuth2:四种授权方式
- [Pytorch] F.softmax() method description
- 架构实战营模块8消息队列表结构设计
- 高等数学——常用不定积分公式
- OpenCV测量物体的尺寸技能 get~
- Trigonometric identity transformation formula
- The magic of SQL MERGE statement (detailed instructions)
猜你喜欢
随机推荐
I summed up the bad MySQL interview questions
Sentinel服务熔断和降级
梅克尔工作室-第一次
thread_local 变量的析构顺序
TRACE32——常用操作
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(legend、修改可视化图像的图例在整图中的位置)
763.划分字母区间——之打开新世界
最小费用最大流问题详解
我把问烂了的MySQL面试题总结了一下
redhat/openssl生成自签ca证书并使用
Introduction to BigDecimal, common methods
NPM淘宝镜像(最新版本)于2021-11-21 16:53:52发布新版本npm镜像[通俗易懂]
R语言ggplot2可视化:使用ggpubr包的ggmaplot函数可视化MA图(MA-plot)、font.legend参数和font.main参数设置标题和图例字体加粗
安装Xshell并使用其进行Ymodem协议的串口传输
使用 Chainlink Keepers 实现智能合约函数的自动化执行
Asynchronous processing business using CompletableFuture
基于极限学习机(ELM)进行多变量用电量预测(Matlab代码实现)
Redis与分布式:哨兵模式
Message queue data storage MySQL table design
微服务架构选型









