Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Use the Netplan Network Configuration Tool on Linux

#1
How to Use the Netplan Network Configuration Tool on Linux

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/09/how-to-use-the-netplan-network-configuration-tool-on-linux.jpg" width="1388" height="732" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/09/how-to-use-the-netplan-network-configuration-tool-on-linux.jpg" class="ff-og-image-inserted" /></div>
<p>For years Linux admins and users have configured their network interfaces in the same way. For instance, if you’re a Ubuntu user, you could either configure the network connection via the desktop GUI or from within the <em>/etc/network/interfaces</em> file. The configuration was incredibly easy and never failed to work. The configuration within that file looked something like this:</p>
<pre>
auto enp10s0 iface enp10s0 inet static address 192.168.1.162 netmask 255.255.255.0 gateway 192.168.1.100 dns-nameservers 1.0.0.1,1.1.1.1</pre>
<p>Save and close that file. Restart networking with the command:</p>
<pre>
sudo systemctl restart networking</pre>
<p>Or, if you’re not using a non-systemd distribution, you could restart networking the old fashioned way like so:</p>
<pre>
sudo /etc/init.d/networking restart</pre>
<p>Your network will restart and the newly configured interface is good to go.</p>
<p>That’s how it’s been done for years. Until now. With certain distributions (such as Ubuntu Linux 18.04), the configuration and control of networking has changed considerably. Instead of that <em>interfaces</em> file and using the <em>/etc/init.d/networking </em>script, we now turn to <a href="https://netplan.io/">Netplan</a>. Netplan is a command line utility for the configuration of networking on certain Linux distributions. Netplan uses YAML description files to configure network interfaces and, from those descriptions, will generate the necessary configuration options for any given renderer tool.</p>
<p>I want to show you how to use Netplan on Linux, to configure a static IP address and a DHCP address. I’ll be demonstrating on Ubuntu Server 18.04. I will give you one word of warning, the .yaml files you create for Netplan must be consistent in spacing, otherwise they’ll fail to work. You don’t have to use a specific spacing for each line, it just has to remain consistent.</p>
<h3>The new configuration files</h3>
<p>Open a terminal window (or log into your Ubuntu Server via SSH). You will find the new configuration files for Netplan in the /etc/netplan directory. Change into that directory with the command <em>cd /etc/netplan</em>. Once in that directory, you will probably only see a single file:</p>
<pre>
01-netcfg.yaml</pre>
<p>You can create a new file or edit the default. If you opt to edit the default, I suggest making a copy with the command:</p>
<pre>
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak</pre>
<p>With your backup in place, you’re ready to configure.</p>
<h3>Network Device Name</h3>
<p>Before you configure your static IP address, you’ll need to know the name of device to be configured. To do that, you can issue the command <em>ip a</em> and find out which device is to be used (Figure 1). </p>
<p>I’ll be configuring ens5 for a static IP address.</p>
<h3>Configuring a Static IP Address</h3>
<p>Open the original .yaml file for editing with the command:</p>
<pre>
sudo nano /etc/netplan/01-netcfg.yaml</pre>
<p>The layout of the file looks like this:</p>
<p>network:</p>
<p>    Version: 2</p>
<p>    Renderer: networkd</p>
<p>    ethernets: </p>
<p>       DEVICE_NAME:</p>
<p>          Dhcp4: yes/no</p>
<p>          Addresses: [IP/NETMASK]</p>
<p>          Gateway: GATEWAY</p>
<p>          Nameservers:</p>
<p>             Addresses: [NAMESERVER, NAMESERVER]</p>
<p>Where:</p>
<ul>
<li>
<p>DEVICE_NAME is the actual device name to be configured.</p>
</li>
<li>
<p>yes/no is an option to enable or disable dhcp4.</p>
</li>
<li>
<p>IP is the IP address for the device.</p>
</li>
<li>
<p>NETMASK is the netmask for the IP address.</p>
</li>
<li>
<p>GATEWAY is the address for your gateway.</p>
</li>
<li>
<p>NAMESERVER is the comma-separated list of DNS nameservers.</p>
</li>
</ul>
<p>Here’s a sample .yaml file:</p>
<pre>
network: version: 2 renderer: networkd ethernets: ens5: dhcp4: no addresses: [192.168.1.230/24] gateway4: 192.168.1.254 nameservers: addresses: [8.8.4.4,8.8.8.8]</pre>
<p>Edit the above to fit your networking needs. Save and close that file.</p>
<p>Notice the netmask is no longer configured in the form 255.255.255.0. Instead, the netmask is added to the IP address.</p>
<h3>Testing the Configuration</h3>
<p>Before we apply the change, let’s test the configuration. To do that, issue the command:</p>
<pre>
sudo netplan try</pre>
<p>The above command will validate the configuration before applying it. If it succeeds, you will see Configuration accepted. In other words, Netplan will attempt to apply the new settings to a running system. Should the new configuration file fail, Netplan will automatically revert to the previous working configuration. Should the new configuration work, it will be applied.</p>
<h3>Applying the New Configuration</h3>
<p>If you are certain of your configuration file, you can skip the try option and go directly to applying the new options. The command for this is:</p>
<pre>
sudo netplan apply</pre>
<p>At this point, you can issue the command ip a to see that your new address configurations are in place.</p>
<h3>Configuring DHCP</h3>
<p>Although you probably won’t be configuring your server for DHCP, it’s always good to know how to do this. For example, you might not know what static IP addresses are currently available on your network. You could configure the device for DHCP, get an IP address, and then reconfigure that address as static.</p>
<p>To use DHCP with Netplan, the configuration file would look something like this:</p>
<pre>
network: version: 2 renderer: networkd ethernets: ens5: Addresses: [] dhcp4: true optional: true</pre>
<p>Save and close that file. Test the file with:</p>
<pre>
sudo netplan try</pre>
<p>Netplan should succeed and apply the DHCP configuration. You could then issue the<em> </em><em>ip a</em> command, get the dynamically assigned address, and then reconfigure a static address. Or, you could leave it set to use DHCP (but seeing as how this is a server, you probably won’t want to do that).</p>
<p>Should you have more than one interface, you could name the second .yaml configuration file 02-netcfg.yaml. Netplan will apply the configuration files in numerical order, so 01 will be applied before 02. Create as many configuration files as needed for your server. </p>
<h3>That’s All There Is</h3>
<p>Believe it or not, that’s all there is to using Netplan. Although it is a significant change to how we’re accustomed to configuring network addresses, it’s not all that hard to get used to. But this style of configuration is here to stay… so you <em>will</em> need to get used to it.</p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016