Ubuntu 20.04 网络配置
<p><strong><span style="font-size: 16px">“ubuntu从17.10开始,已放弃在/etc/network/interfaces里固定IP的配置,即使配置也不会生效,而是改成netplan方式 ,配置写在/etc/netplan/01-netcfg.yaml或者类似名称的yaml文件里”</span></strong></p><p><strong><span style="font-size: 16px">但DNS配置最好检查一下<strong>17.10以前的传统配置方法</strong></span></strong></p>
<p><strong><span style="font-size: 16px"><strong><img src="https://img2020.cnblogs.com/blog/1893638/202012/1893638-20201220183839178-1039837944.png"></strong></span></strong></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p><strong>翻译自https://linuxhint.com/ubuntu_20-04_network_configuration/</strong></p>
<p>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.</p>
<div class="adthrive-video-player in-post" data-video-id="GUfHQuGO" data-player-type="collapse">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.</div>
<p>In this article, we will be covering how to:</p>
<ul>
<li>View current IP address</li>
<li>Set static IP address 设置静态IP</li>
<li>Set the dynamic IP address 设置动态IP</li>
<li>View current hostname 查看主机名称</li>
<li>Change hostname 修改主机名称</li>
</ul>
<p>Note: We have run the commands and procedure mentioned in this article on the Ubuntu 20.04 system.</p>
<h3>View Current IP Address</h3>
<p>To view the current IP address of your machine, you can use either of the following commands:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">ip a</span></span></div>
</div>
<p>Or</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">ip addr</span></span></div>
</div>
<p><img src="https://linuxhint.com/wp-content/uploads/2020/06/1-40.png"></p>
<p>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.</p>
<h3>Set Static IP Address</h3>
<p>In the following procedure, we will see how to set up the static IP in a Ubuntu system.</p>
<p>Ubuntu 20.04 uses netplan as a default network manager. The configuration file for the netplan is stored in the <em>/etc/netplan</em> directory. You can find this configuration file listed in the /etc/netplan directory the following command:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">ls <span class="sy0">/etc<span class="sy0">/netplan</span></span></span></span></div>
</div>
<p>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.</p>
<p>Before making any changes to this file, make sure to create a backup copy of it. Use the cp command to do so:</p>
<div class="codecolorer-container yaml default">
<div class="yaml codecolorer">$ sudo cp /etc/netplan/01-network-manager-<span class="kw1">all.yaml 01-network-manager-<span class="kw1">all.yaml.bak</span></span></div>
</div>
<p>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.</p>
<p>You can edit the netplan configuration using any text editor. Here we are using the Nano text editor for this purpose.</p>
<div class="codecolorer-container yaml default">
<div class="yaml codecolorer">$ sudo nano /etc/netplan/01-network-manager-<span class="kw1">all.yaml</span></div>
</div>
<p>Then add the following lines by replacing the interface name, IP address, gateway, and DNS information that fit your networking needs.</p>
<div class="codecolorer-container yaml default">
<div class="yaml codecolorer"><span class="co4">network:<span class="co3"><br>version<span class="sy2">: 2<span class="co3"><br>renderer<span class="sy2">: NetworkManager<span class="co4"><br>ethernets:<span class="co4"><br> ens33:<span class="co3"><br> dhcp4<span class="sy2">: <span class="kw1">no<span class="co4"><br> addresses<span class="sy2">:<br> - 192.168.72.140/24<span class="co3"><br> gateway4<span class="sy2">: 192.168.72.2<span class="co4"><br> nameservers:<span class="co3"><br> addresses<span class="sy2">: <span class="br0"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
</div>
<p>Once done, save and close the file.</p>
<p>Now test the new configuration using the following command:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo netplan try</span></span></div>
</div>
<p>If it validates the configuration, you will receive the configuration accepted message; otherwise, it rolls back to the previous configuration.<br>Next, run the following command to apply the new configurations.</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo netplan apply</span></span></div>
</div>
<p>After this, confirm the IP address of your machine using the following command:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">ip a</span></span></div>
</div>
<p>It should reflect the changes you have made.</p>
<p><img sizes="(max-width: 810px) 100vw, 810px" srcset="https://linuxhint.com/wp-content/uploads/2020/06/2-50-1024x321.png 1024w, https://linuxhint.com/wp-content/uploads/2020/06/2-50-300x94.png 300w, https://linuxhint.com/wp-content/uploads/2020/06/2-50-768x241.png 768w, https://linuxhint.com/wp-content/uploads/2020/06/2-50-810x254.png 810w, https://linuxhint.com/wp-content/uploads/2020/06/2-50.png 1048w" alt="" width="810" height="254" class="aligncenter size-large wp-image-61872 lazyloaded lazyload" data-ll-status="loaded" data-src="https://linuxhint.com/wp-content/uploads/2020/06/2-50-1024x321.png"></p>
<h3>Set the Dynamic IP Address</h3>
<p>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.</p>
<div class="codecolorer-container yaml default">
<div class="yaml codecolorer">$ sudo nano /etc/netplan/01-network-manager-<span class="kw1">all.yaml</span></div>
</div>
<p>Then add the following lines by replacing the interface name with your system’s network interface.</p>
<div class="codecolorer-container yaml default">
<div class="yaml codecolorer"><span class="co4">network:<span class="co3"><br> version<span class="sy2">: 2<span class="co3"><br> renderer<span class="sy2">: NetworkManager<span class="co4"><br> ethernets:<span class="co4"><br> ens33:<span class="co3"><br> dhcp4<span class="sy2">: <span class="kw1">yes<span class="co3"><br> addresses<span class="sy2">: <span class="br0">[<span class="br0">]</span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
</div>
<p>Once done, save and close the file.</p>
<p>Now test the new configuration using the following command:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo netplan try</span></span></div>
</div>
<p>If it validates the configuration, you will receive the configuration accepted message, otherwise, it rolls back to the previous configuration.</p>
<p>Next, run the following command to apply the new configurations.</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo netplan apply</span></span></div>
</div>
<p><img sizes="(max-width: 900px) 100vw, 900px" srcset="https://linuxhint.com/wp-content/uploads/2020/06/3-46.png 900w, https://linuxhint.com/wp-content/uploads/2020/06/3-46-300x124.png 300w, https://linuxhint.com/wp-content/uploads/2020/06/3-46-768x317.png 768w, https://linuxhint.com/wp-content/uploads/2020/06/3-46-810x334.png 810w" alt="" width="900" height="371" class="aligncenter size-full wp-image-61873 lazyloaded lazyload" data-ll-status="loaded" data-src="https://linuxhint.com/wp-content/uploads/2020/06/3-46.png"></p>
<p>After this, check the IP address of your machine using the following command:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">ip a</span></span></div>
</div>
<h3>View Current Hostname</h3>
<p>To view the current hostname, you can use either of the following commands:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ hostnamectl</span></div>
</div>
<p>or</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">hostname</span></span></div>
</div>
<p><img sizes="(max-width: 305px) 100vw, 305px" srcset="https://linuxhint.com/wp-content/uploads/2020/06/4-45.png 305w, https://linuxhint.com/wp-content/uploads/2020/06/4-45-300x49.png 300w" alt="" width="305" height="50" class="aligncenter size-full wp-image-61874 lazyloaded lazyload" data-ll-status="loaded" data-src="https://linuxhint.com/wp-content/uploads/2020/06/4-45.png"></p>
<h2>Change Hostname</h2>
<p>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.</p>
<h3>Using the Hostnamectl Command</h3>
<p>To change the hostname of the system to a new name, use the following command:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo hostnamectl set-hostname name</span></span></div>
</div>
<p>Example:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo hostnamectl set-hostname desktop</span></span></div>
</div>
<p>This command will change the hostname of the system to “desktop”.</p>
<p>After that, reboot the system and you will see the new hostname assigned to your system.</p>
<h3>Using Hostname Command</h3>
<p>Hostname command can also be used to change the hostname of the system. Use the following command to change the hostname of the system:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo <span class="kw2">hostname name</span></span></span></div>
</div>
<p>This command changes the hostname of the system temporarily. To permanently change the hostname, you will need to edit the <em>/etc/hostname</em> and <em>/etc/hosts</em> file.</p>
<p>Use the following command to edit the <em>/etc/hostname</em> file:</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo <span class="kw2">nano <span class="sy0">/etc<span class="sy0">/<span class="kw2">hostname</span></span></span></span></span></span></div>
</div>
<p>Replace the old hostname with the new name, then save and exit the file.</p>
<p>Next, use the following command to edit the <em>/etc/hosts</em> file:</p>
<p>Replace the old hostname with the new name, then save and exit the file.</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">sudo <span class="kw2">nano <span class="sy0">/etc<span class="sy0">/hosts</span></span></span></span></span></div>
</div>
<p>After that, reboot the system and you will see the new hostname assigned to your system.</p>
<p>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.</p>
<div class="codecolorer-container bash default">
<div class="bash codecolorer"><span class="co4">$ <span class="kw2">ping IP-address or domain name</span></span></div>
</div>
<p>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!</p>
<p> </p>
<p> </p>
<p><span style="font-size: 18px"><strong>How to Check Route Table?</strong></span><br>ip route show</p>
<p>davidyin@u20:~$ ip route show</p>
<p>default via 192.168.1.1 dev ens18 proto dhcp src 192.168.1.158 metric 100<br>192.168.1.0/24 dev ens18 proto kernel scope link src 192.168.1.158<br>192.168.1.1 dev ens18 proto dhcp scope link src 192.168.1.158 metric 100<br>davidyin@u20:~$<br>Other commands I am not using.<br>Add Static Route</p>
<p># ip route add 10.10.20.0/24 via 192.168.1.100 dev eth0<br>Remove Static Route<br># ip route del 10.10.20.0/24</p>
<p>Add default gateway<br># ip route add default via 192.168.1.100</p>
<p>Add an IP address to a specific interface<br># ip addr add 192.168.1.5 dev eth1</p>
<p>Remove an IP address<br># ip addr del 192.168.1.5/24 dev eth1</p>
<p>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.</p>
<p> </p>
<p><span style="font-size: 18pt"><strong>Ubuntu20配置值静态ip时需要修改/etc/netplan下面</strong></span></p>
<p><strong>转自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</strong></p>
<p>1-network-manager-all.yaml这个文件,该文件的原始内容为:<br><img alt="原始内容" data-src="https://img-blog.csdnimg.cn/20200610132429999.png"></p>
<p>在修改该文件时,先试用ifconfig查看下网卡相关信息,然后再对应修改,修改后的文件内容为:<br><img src="https://img-blog.csdnimg.cn/20200610132600571.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoYW9mYW5qdW4=,size_16,color_FFFFFF,t_70"><br>修改时相应的配置为:</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering">ens33: <span class="token comment">#配置的网卡名称
dhcp4: no <span class="token comment">#dhcp4关闭
dhcp6: no <span class="token comment">#dhcp6关闭
addresses: <span class="token punctuation"> <span class="token comment">#设置本机IP及掩码
gateway4: 192.168.147.1 <span class="token comment">#设置网关
nameservers:
addresses: <span class="token punctuation"> <span class="token comment">#设置DNS</span></span></span></span></span></span></span></span></span></span></code></pre>
<p> </p>
<p>有时候在修改该文件时,会碰到无权限修改的情况,这时候使用chmod修改下文件权限即可。<br>修改完成后,在终端再输入</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering"><span class="token function">sudo netplan apply</span></code></pre>
<p>此时,在终端中使用ifconfig查看ip时可以发现ip地址已经发生了变化。</p>
<p> </p>
<p> 转自https://blog.csdn.net/fansnn/article/details/105930009</p>
<p> </p>
<h1 id="articleContentId" class="title-article">Ubuntu 20.04修改ip地址</h1>
<p>今天在继续上次的办公协作平台实验的时候发现虚拟机因为之前是DHCP的方式获取的ip地址,而这次虚拟机开机后DHCP获取的地址变更了,因为网站上已经配置了固定ip地址访问,导致不能访问网页,所以需要修改ip地址为上次获取的ip地址才能访问,查了一下ubuntu怎么修改ip地址才发现“ubuntu从17.10开始,已放弃在/etc/network/interfaces里固定IP的配置,即使配置也不会生效,而是改成netplan方式 ,配置写在/etc/netplan/01-netcfg.yaml或者类似名称的yaml文件里”<br>话不多说,直接说netplan怎么修改ip地址</p>
<h3> </h3>
<p><img src="https://img-blog.csdnimg.cn/2020050511431861.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZhbnNubg==,size_16,color_FFFFFF,t_70"><br>使用ifconfig命令查看网卡</p>
<pre class="prettyprint"><code class="prism language-bash has-numbering"><span class="token function">ifconfig
</span></code></pre>
<p> </p>
<p>修改yaml配置文件<br><img src="https://img-blog.csdnimg.cn/20200505115013624.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ZhbnNubg==,size_16,color_FFFFFF,t_70"></p>
<pre class="prettyprint"><code class="prism language-bash has-numbering"><span class="token function">sudo <span class="token function">vi /etc/netplan/00-installer-config.yaml
network:
ethernets:
ens33: <span class="token comment">#配置的网卡的名称
addresses: <span class="token punctuation"> <span class="token comment">#配置的静态ip地址和掩码
dhcp4: no <span class="token comment">#关闭DHCP,如果需要打开DHCP则写yes
optional: <span class="token boolean">true
gateway4: 192.168.31.1 <span class="token comment">#网关地址
nameservers:
addresses: <span class="token punctuation"> <span class="token comment">#DNS服务器地址,多个DNS服务器地址需要用英文逗号分隔开
version: 2
renderer: networkd <span class="token comment">#指定后端采用systemd-networkd或者Network Manager,可不填写则默认使用systemd-workd
</span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
<p>根据自己的需要配置好之后保存文件</p>
<h3>使配置的ip地址生效</h3>
<pre class="prettyprint"><code class="prism language-bash has-numbering"><span class="token function">sudo netplan apply</span></code></pre>
<p>好了,使用ifconfig命令查看配置的新ip地址是否生效了吧</p>
<p>注意事项:<br>1、ip地址和DNS服务器地址需要用[]括起来,但是网关地址不需要<br>2、注意每个冒号后边都要先加一个空格<br>3、注意每一层前边的缩进,至少比上一层多两个空格</p><br><br>
来源:https://www.cnblogs.com/ArcherWuAIot/p/14164414.html
頁:
[1]