Now lets see how to configure network settings on an Ubuntu machine.
The IP command can help us discover almost everything
we need to know about our network configuration.
We can start out by entering IP link.
This shows us our networking devices also called network interfaces.
In Linux terminology,
some are real devices like Ethernet cards, wireless cards, et cetera.
But other devices shown here can be virtual.
Now, the lo
here at the top is the loopback interface which is virtual.
The loopback interface is used by the system when it needs to connect to itself.
For example,
when a program wants to connect to a
database application running on the same system,
it goes to 127.0 0.0 0.1 through this loop deck device
ENP 0 S3 is an interface representing a real device. In this case, an
Ethernet card.
Now to display the IP addresses configured for these interfaces, we can run IP
address
or even shorter command
IP AD Dr
or
IP A
and this output is a bit messy and hard to read
to make the important bits stand out, we can pass the dash the color option,
it looks much better, right?
And the output is much easier to go through,
make sure to add the dash the parameter in the middle, adding it at the end won't work.
So in this output, we can see the things that we're interested in,
namely the IPv4 and IPv6 addresses configured for each network interface.
But notice the output for our third interface. We can see it's marked as being down.
That means it's not currently active.
We can also see it doesn't have any IP address configured
to make the interface active or bring it up.
We can type in something like this.
We can see there's a nice flow for this command.
First, we specify we want to work with a linked device,
then we want to set a property.
Next, we specify the name of the device or DEV
that we want to modify settings for and finally
up states our intention to activate the device.
Now, if we check the IP address settings again,
we'll see that it's now up and even autoconfigured with an IPv6 for itself.
If we want to manually add an IPV four address for it,
we can do that.
Once again, the logic of the command follows a similar straightforward pattern.
We specify that we want to work with an address and add an IP,
we assign the IP address to our DEV slash device
and we specify a slash 24 side range for it.
Finally, we type the name of the device, we want to set this property for.
Now, if we look at the IP settings again,
we can see the IP address has been configured
to add an IPV six address. Things are roughly the same.
We'll use the shortened format for this address.
Just the IP format differs.
Ok. And if we explore the IP settings again,
we see the address has been configured.
Note that a network interface can have multiple IPs
to remove an IP address, we can type in this.
But now let's bring this network device back down.
The trouble with setting IP S with the
IP command is that these changes are temporary.
As soon as we reboot, the server, our changes are lost.
So the IP command is mostly used to test new IP settings temporarily
and of course to explore the current network settings.
So how do we make our changes permanent?
Well, it depends on the operating system.
Each one has its own set of tools.
And this can sometimes change from one major version to the next
default networking tools have been changed a lot in the past.
What
Ubuntu currently uses is called Netplan.
This is a set of utilities that reads some configuration files,
then sends instructions to other networking tools,
telling them to configure network settings accordingly.
On the
Ubuntu server edition Netplan sends instructions to
systemd's network daemon called systemd dash networkd.
So let's see how we can instruct Netplan to assign some IP S for our devices
to see the current Netplan configuration files we can type in
pseudo
Netplan. Get
we noticed that this specifies some settings for
the Ethernet device called ENP 0 S3.
By setting DHCP four to true,
it makes this device ask for network settings from a DHCP four server.
The four in here refers to IPv4 settings.
A DHCP server is often used to auto configure IP S for the devices on a network.
Now, chances are you use one at home?
For example, when you connect your phone to the Wi Fi router,
you're also using a DHCP server.
The phone asks the DHCP daemon running on the router to tell it what IP settings.
It should configure for itself,
but we want to manually configure these settings for now.
So how can we do that?
Well, Netplan configuration files are stored in the Etsy Netplan directory.
We can take a look at it now
and you'll see in this case, we have a single file in here
to explore the contents of this file. We can run
cat
Etsy Netplan.
Ah We see permission denied.
Now note that it contains the same stuff we
saw displayed in our Netplan get command earlier.
If we want to change the settings for the device mentioned here,
we just have to edit the file and make our desired modifications.
But let's actually create a new file dedicated to a different
Ethernet device on this system.
First, we'll copy this text
and use it as a template in our new file.
Now, let's remember the name of the device that we want to configure,
which in our case is ENP zero S eight. And let's create a new Netplan
YAML file.
Now let's paste in the content that we copied earlier.
And the first thing we need to modify here is the interface name
and for mine, it's ENP zero S eight.
Next, we should change the
DHCP four line. As we don't want this device to auto configure with the help of
a
DHCP server.
We'll change the property here to false
and we'll also add an extra line and set DH TP six auto configuration to false as well.
Now, we should be careful to preserve formatting here,
namely the preceding space characters we add before every line
we can see that the settings are aligned with a certain number of spaces to
signal what the parent is for each object and what the child objects are.
For example,
Ethernet is apparent for the objects below.
Then ENP zero S eight is apparent for the DH TP settings.
This way when Netplan processes the file,
it will know what property applies to what object.
And this type of formatting is specific to YAML files
to specify the IP addresses that we want to assign to this device.
We simply type in this.
And on the next line, we type 10.0 0.0 0.9
slash 24.
Now note when we press enter,
how the Vim editor intelligently adds two leading
spaces for us to help with formatting.
In this case,
we need this alignment to add some properties that belong to this address' object.
But in other cases, it can format things wrong.
So do watch out for that when editing YAML files to avoid any mistakes.
Now here the minus sign signals that this is an element belonging to a list.
Once again, this is specifically how YAML files should be defined.
But why can't we just list elements without a dash sign
because each IP here would have additional properties.
And if we add two or three properties below it,
Netplan wouldn't know how to differentiate between lines,
what are the elements and what are the properties for each element?
Since they would all be aligned the same by adding a dash sign.
Here, we signal where the elements are.
And if we want to add two or three properties to an element, we simply type them below,
but don't add the dash sign before them.
This way,
Netplan understands where the elements are specified
and what properties apply to each one
on the line below. We'll also add an IPv6 address.
Let's use something that will stand out. So we can check out later. If this worked,
we'll add an AB CD in our address to recognize it easily.
It's going to be right here.
Now, we're ready to save our file
and we can notice this countdown.
So if we don't press enter to accept changes,
network changes will be reverted to whatever they were before.
In case, we lost network connection to a server because of wrong settings,
we can try to log back in in
two minutes when settings will automatically be reverted.
In our case though, let's press enter to accept the changes
to specify a smaller timeout window like 30 seconds,
we can pass the dash dash timeout option
and to cancel the operation, we can press control C
CH mod
600
and then the file name
and then it will make that warning go away.
Now, if we check the settings for our devices,
it looks like everything worked perfectly.
Our device is now using the new IPv4 and IPv6 addresses.
Notice the AB CD part that we used in our IPv6 address
and the interface is also in an upstate.
So Netplan helped bring it online as well.
Now, if we reboot our server, the settings would be reapplied automatically.
So these settings are permanent until they're modified again.
So if we run that plan, get once more,
we'll see the entire configuration is displayed
both for the old YAML file we already had
and also the settings we defined in our new file.
So everything is available at a glance.
Now let's go back to editing our file to
showcase some additional network settings that might be useful.
Let's add a few lines and make sure to align this
text two spaces to the right of the ENP line above
to signal that these are settings for that parent element.
This sets two name servers, one with the IPv4 address of 8844 and one with 8888.
These are domain name servers belonging to Google and
they're used to solve domain names to IP addresses.
For example, when we type in a name like code cloud.com in our browser,
the computer can't know on its own what IP address is assigned to this domain.
So it asks a name server for this data.
With the lines. Here, we configured two name servers also called DNS servers for our
EP zero S eight interface.
We'll also add a few extra lines and I'll explain them in a few moments.
Note how this time the minus sign makes sense.
We differentiate between elements and properties.
So when we type in via here without a preceding minus sign,
Netplan will know that this via property applies to the element above.
So what we did here was add two network routes.
The first one tells our machine that when it wants to reach any IP.
In the 192168.0 0.0 network,
it can contact a server at 10.0 0.0 0.100.
The server acts as a middleman passing data from one network to the other.
The other route specifies that if the computer can't figure
out what route to use from everything it has available,
it can fall back on this default route.
For example, with current settings,
it wouldn't know what route to use to send data to an IP like 20.20 0.20 0.20.
No route is configured for this destination.
So we'll fall back on the default route and tell
the server at 10.0 0.0 0.1 to pass this data along
the server is often called a gateway.
Since there's a door to an external world from this network to any other network.
For example, when we use the internet at home, our router is usually the gateway
because it helps us send data from our home
network out to any other network in the world.
Now, let's save our settings
and we'll apply our settings
and it looks like I have a YAML problem.
So let's dig in and take a look at it.
And as we can see here, I forgot to put a colon after routes
and now it should work properly.
All right,
it's accepted.
But were our settings applied?
We can check our network routes with this command
IP route.
We can see the routes were indeed applied.
We noticed the default route via the 10.0 0.0
0.1 gateway for the ENP zero S eight device
and the route here via the 10.0 0.0 0.100 gateway.
What about our DNS resolvers?
Well, we can take a look at configured resolvers with this command
resolve
CTL status.
Here we go.
So the resolvers were successfully configured as well.
But note that the 8.8 0.8 0.8 resolver only applies to the ENP zero S eight device.
Sometimes we will want resolvers to be applied
globally to all network devices we might have
for this purpose. We can configure resolvers in a different way.
We can edit the Etsy Systemd
resolved.com file.
All right,
we'll open this up.
We'll go down here to DNS
and uncomment that line by removing half time.
And we'll put in 11.1 0.1
space 9.99 0.9.
And we'll add these two DNS servers
and after we save the file, we should restart systemd's
resolver daemon.
Now, if we run the resolve
CTL command again,
we'll see that our resolvers have been applied globally
as an added tip,
we can also use this command for cleaner but less detailed output about DNS settings
type in resolve CTL DNS.
Now,
these DNS resolvers work for domains such
as code cloud.com and other similar entries.
But what about simple host names for machines in our internal network?
For example, we might have a database server around,
but we can't remember its IP address every time we want to use it.
However, it would be easier to remember a code name for it like db server.
Now to implement this idea,
we'll edit this file
Etsy hosts.
Now, as you can see, we have some addresses in here
and we're going to add a line
127.0 0.123 0.123
space DV server.
This tells our machine to associate this hostname DB server with this IP.
And now we'll try to ping DB server
and we can see our system now knows the IP address of this specific hostname.
We can also add more complex host names in that file entries like this
1.2 0.3 0.4 example.com
effectively tricking our system to believe that example.com
has the 1.2 0.3 0.4 IP address.
This is a common way to test out websites on
a local machine before we actually post them online.
And our machine thinks that example.com is 1.2 0.3 0.4.
Now to wrap up this lesson, here's a useful tip.
You might have noticed that Netplan
YAML files are easy to read, but a bit harder to write.
It can be tricky to figure out what exact words we need
to use to configure various network settings.
But we can get some help from this manual by typing in man space Netplan.
Now, here we'll find quite a few examples about various settings we can configure.
Here's one interesting page.
If we press front slash to begin a search,
then type in address label
we get to this part
which shows us a similar format that we used in our demo to configure an IP address
or we type front slash, then default routes,
then press N
to look for the next occurrence. Here,
we get to this part
also similar to what we used for our purpose,
except it has a few extra settings in the middle, like metric settings and so on.
So these examples are very helpful to figure out how to define that YAML file.
But if all of this seems a bit too complicated, here's another trick we can use.
In fact,
it might come in handy with many other
tools that require us to edit configuration files.
We'll find a special directory on almost any Linux
installation called slash user slash share slash doc.
This includes additional documentation for various software packages.
And in the case of Netplan,
we can get access to quite a few example configuration files,
we just have to list the contents of the directory.
So we'll look at user share doc, netplan examples
and pick the example that we want to take a look at.
Now in our case, DH DP, YAML
and static
YAML are pretty interesting.
This shows us how to configure a simple
Ethernet interface that gets its IP settings through
DHCP.
And the static YAML file
shows us how to configure an
Ethernet interface with static network settings.
Hopefully this will make it easier to create Netplan
YAML configuration files.
OK. This is how to configure IPv4 and IPv6 networking and hostname resolution.
This wraps up our current lesson. Now, let's jump ahead to the next one.