Ubuntu 20.04 网络配置

摘要:
“从17.10开始,ubuntu放弃了在/etc/network/interfaces中配置固定IP地址。即使该配置不会生效,它也将更改为netplan模式。该配置写入/etc/netplan/01-netcfg.yaml或具有类似名称的yaml文件中。”然而,最好在17.10之前检查传统配置方法的DNS配置https://linuxhint.com/ubuntu_20-04_network_config

“ubuntu从17.10开始,已放弃在/etc/network/interfaces里固定IP的配置,即使配置也不会生效,而是改成netplan方式 ,配置写在/etc/netplan/01-netcfg.yaml或者类似名称的yaml文件里”

但DNS配置最好检查一下17.10以前的传统配置方法

Ubuntu 20.04 网络配置第1张

翻译自https://linuxhint.com/ubuntu_20-04_network_configuration/

Whether you are a Linux administrator or regular user, you must know the basics of network configuration in your Linux system. it might be helpful when troubleshooting the issues with internal and external connectivity. The basic knowledge involves knowing the interface name, the current IP configuration, and the hostname. Also, you should know how to change the default configurations to the customized settings.

In this article, we will explain how to do basic network configuration in the Ubuntu system. We will use the command-line Terminal for executing the commands. To open the command line Terminal in Ubuntu, use the Ctrl+Alt+T keyboard shortcut.

In this article, we will be covering how to:

  • View current IP address
  • Set static IP address             设置静态IP
  • Set the dynamic IP address  设置动态IP
  • View current hostname         查看主机名称
  • Change hostname                 修改主机名称

Note: We have run the commands and procedure mentioned in this article on the Ubuntu 20.04 system.

View Current IP Address

To view the current IP address of your machine, you can use either of the following commands:

ip a

Or

ip addr

Ubuntu 20.04 网络配置第2张

Running either of the above commands will display the IP address information. Note down the name of the interface from the output of the above command.

Set Static IP Address

In the following procedure, we will see how to set up the static IP in a Ubuntu system.

Ubuntu 20.04 uses netplan as a default network manager. The configuration file for the netplan is stored in the /etc/netplan directory. You can find this configuration file listed in the /etc/netplan directory the following command:

ls /etc/netplan

The above command will return the name of the configuration file with the .yaml extension, which in my case was 01-network-manager-all.yaml.

Before making any changes to this file, make sure to create a backup copy of it. Use the cp command to do so:

$ sudo cp /etc/netplan/01-network-manager-all.yaml 01-network-manager-all.yaml.bak

Note: You might have a configuration file with the name other than the 01-network-manager-all.yaml. So make sure you use the right configuration file name in the commands.

You can edit the netplan configuration using any text editor. Here we are using the Nano text editor for this purpose.

$ sudo nano /etc/netplan/01-network-manager-all.yaml

Then add the following lines by replacing the interface name, IP address, gateway, and DNS information that fit your networking needs.

network:
version: 2
renderer: NetworkManager
ethernets:
 ens33:
  dhcp4no
  addresses:
  - 192.168.72.140/24
  gateway4: 192.168.72.2
  nameservers:
   addresses[8.8.8.8, 8.8.4.4]

Once done, save and close the file.

Now test the new configuration using the following command:

sudo netplan try

If it validates the configuration, you will receive the configuration accepted message; otherwise, it rolls back to the previous configuration.
Next, run the following command to apply the new configurations.

sudo netplan apply

After this, confirm the IP address of your machine using the following command:

ip a

It should reflect the changes you have made.

Ubuntu 20.04 网络配置第3张

Set the Dynamic IP Address

In the following procedure, we will see how to configure the interface to receive a dynamic IP address from DHCP. Edit the netplan configuration file using any text editor. Here we are using the Nano text editor for this purpose.

$ sudo nano /etc/netplan/01-network-manager-all.yaml

Then add the following lines by replacing the interface name with your system’s network interface.

network:
 version: 2
 renderer: NetworkManager
 ethernets:
  ens33:
   dhcp4yes
   addresses[]

Once done, save and close the file.

Now test the new configuration using the following command:

sudo netplan try

If it validates the configuration, you will receive the configuration accepted message, otherwise, it rolls back to the previous configuration.

Next, run the following command to apply the new configurations.

sudo netplan apply

Ubuntu 20.04 网络配置第4张

After this, check the IP address of your machine using the following command:

ip a

View Current Hostname

To view the current hostname, you can use either of the following commands:

$ hostnamectl

or

hostname

Ubuntu 20.04 网络配置第5张

Change Hostname

There are two different ways to change the hostname of the system. To change the hostname of the system, you must be a root user or a standard user with sudo privileges.

Using the Hostnamectl Command

To change the hostname of the system to a new name, use the following command:

sudo hostnamectl set-hostname name

Example:

sudo hostnamectl set-hostname desktop

This command will change the hostname of the system to “desktop”.

After that, reboot the system and you will see the new hostname assigned to your system.

Using Hostname Command

Hostname command can also be used to change the hostname of the system. Use the following command to change the hostname of the system:

sudo hostname name

This command changes the hostname of the system temporarily. To permanently change the hostname, you will need to edit the /etc/hostname and /etc/hosts file.

Use the following command to edit the /etc/hostname file:

sudo nano /etc/hostname

Replace the old hostname with the new name, then save and exit the file.

Next, use the following command to edit the /etc/hosts file:

Replace the old hostname with the new name, then save and exit the file.

sudo nano /etc/hosts

After that, reboot the system and you will see the new hostname assigned to your system.

Once you are done with basic network configurations, use the ping command to verify the connectivity of your system with other systems on the network and the external network.

ping IP-address or domain name

That is all the basics you need to know about network configuration in Ubuntu 20.04 system. Note that if you have multiple network interfaces, you will have to perform IP configurations for each interface. I hope you liked the article!

How to Check Route Table?
ip route show

davidyin@u20:~$ ip route show

default via 192.168.1.1 dev ens18 proto dhcp src 192.168.1.158 metric 100
192.168.1.0/24 dev ens18 proto kernel scope link src 192.168.1.158
192.168.1.1 dev ens18 proto dhcp scope link src 192.168.1.158 metric 100
davidyin@u20:~$
Other commands I am not using.
Add Static Route

# ip route add 10.10.20.0/24 via 192.168.1.100 dev eth0
Remove Static Route
# ip route del 10.10.20.0/24

Add default gateway
# ip route add default via 192.168.1.100

Add an IP address to a specific interface
# ip addr add 192.168.1.5 dev eth1

Remove an IP address
# ip addr del 192.168.1.5/24 dev eth1

I always like to use a Router to assign a static IP to the network device. I just need to know the MAC address then I can do it at DD-WRT router.

Ubuntu20配置值静态ip时需要修改/etc/netplan下面

转自https://blog.csdn.net/chaofanjun/article/details/106663662?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control

1-network-manager-all.yaml这个文件,该文件的原始内容为:
原始内容

在修改该文件时,先试用ifconfig查看下网卡相关信息,然后再对应修改,修改后的文件内容为:
修改后的文件内容
修改时相应的配置为:

ens33:   #配置的网卡名称
      dhcp4: no    #dhcp4关闭
      dhcp6: no    #dhcp6关闭
      addresses: [192.168.147.130/24]   #设置本机IP及掩码
      gateway4: 192.168.147.1   #设置网关
      nameservers:
          addresses: [192.168.147.1, 114.114.114.114]   #设置DNS

有时候在修改该文件时,会碰到无权限修改的情况,这时候使用chmod修改下文件权限即可。
修改完成后,在终端再输入

sudo netplan apply

此时,在终端中使用ifconfig查看ip时可以发现ip地址已经发生了变化。

 转自https://blog.csdn.net/fansnn/article/details/105930009

Ubuntu 20.04修改ip地址

今天在继续上次的办公协作平台实验的时候发现虚拟机因为之前是DHCP的方式获取的ip地址,而这次虚拟机开机后DHCP获取的地址变更了,因为网站上已经配置了固定ip地址访问,导致不能访问网页,所以需要修改ip地址为上次获取的ip地址才能访问,查了一下ubuntu怎么修改ip地址才发现“ubuntu从17.10开始,已放弃在/etc/network/interfaces里固定IP的配置,即使配置也不会生效,而是改成netplan方式 ,配置写在/etc/netplan/01-netcfg.yaml或者类似名称的yaml文件里”
话不多说,直接说netplan怎么修改ip地址

 

在这里插入图片描述
使用ifconfig命令查看网卡

ifconfig

修改yaml配置文件
在这里插入图片描述

sudo vi /etc/netplan/00-installer-config.yaml

network:
  ethernets:
    ens33:     #配置的网卡的名称
      addresses: [192.168.31.215/24]    #配置的静态ip地址和掩码
      dhcp4: no    #关闭DHCP,如果需要打开DHCP则写yes
      optional: true
      gateway4: 192.168.31.1    #网关地址
      nameservers:
         addresses: [192.168.31.1,114.114.114.114]    #DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
  version: 2
  renderer: networkd    #指定后端采用systemd-networkd或者Network Manager,可不填写则默认使用systemd-workd

根据自己的需要配置好之后保存文件

使配置的ip地址生效

sudo netplan apply

好了,使用ifconfig命令查看配置的新ip地址是否生效了吧

注意事项:
1、ip地址和DNS服务器地址需要用[]括起来,但是网关地址不需要
2、注意每个冒号后边都要先加一个空格
3、注意每一层前边的缩进,至少比上一层多两个空格

免责声明:文章转载自《Ubuntu 20.04 网络配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇纯干货!live2d动画制作简述以及踩坑in 索引失效的问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

MySQL的安装、配置文件和初始化

MySQL基于源代码的安装本文以MySQL5.1.73+Centos6.5作为安装演示,其他版本安装方法大同小异。首先下载MySQL5.1.73的源代码包。比如MySQL5.1.73,安装在目录/usr/local/myql-5.1.73,然后做一个软连接/usr/local/mysql指向真正的版本路径。进行到此,MySQL库以及可执行程序已经编译出来,可...

buildroot使用介绍【转】

整个Buildroot由Makefile脚本和Kconfig配置文件组成。就像编译Linux内核一样,您可以编译一个完整的Linux系统软件,该软件可以通过buildroot配置和menuconfig修改直接写入机器。使用buildroot构建基于qemu的虚拟开发平台。请参阅通过buildroot+qemu构建ARM Linux虚拟开发环境。工具链--˃配...

uni-app 安卓和IOS更新方案

热更新资源,即重新安装应用程序,并更新js等前端代码。Android平台更新方案,详见上一篇文章https://www.cnblogs.com/tiandi/p/15331522.html二、2015年,IOS平台苹果发布了一项规定,禁止用户在应用程序中被提示进行版本更新。无法直接更新通用iOSAppstore的安装包。应用程序启动后,检查是否有新版本,该版...

GitLab 数据库

要访问GitLab数据库步骤中使用的DockerGitlab,首先输入容器dockerexec-itgitlab/bin/bash ``查找数据库配置文件``bash/var/opt/GitLab/gitlabrails/etc/database yml内容如下,记录数据库配置信息production:adapter:postgresqlencoding:u...

Elasticsearch之插件介绍及安装

3.KopfPlugin(主要)Kopf是ElasticSearch的管理工具。它还为ES集群操作提供了API。4.级别插件级别工具可以帮助用户监控ElasticSearch的运行状态。但是,此插件需要许可证。安装许可证后,您可以安装marvel的代理。代理将收集弹性搜索的运行状态。ES插件HeadPlugin的在线安装需要转到https://github....

C#使用FFMPEG推流,并且获取流保存在本地,随时取媒体进行播放!

最近,基于C#的拖缆的发展并不理想。最后,经过不懈的努力,我取得了一些成绩。这是一张便条;本文重点介绍如何将ffmpeg用于简单的流式传输。如果没有官方文档WithFilter.WithField,简单的代码行似乎很难。拉动();以上是流和获取流的核心代码,保存在本地=TargetType。Live){thrownewApplicationException...