What does source /etc/network/interfaces.d/* do at the top of the /etc/network/interfaces file?
I've noticed that by default the below line is included in /etc/network/interfaces
:
source /etc/network/interfaces.d/*
What is the purpose of this line and what does it actually do?
Solution 1:
From the Ubuntu interfaces man page:
Lines beginning with "source" are used to include stanzas from other files, so configuration can be split into many files. The word "source" is followed by the path of file to be sourced. Shell wildcards can be used. Currently only supports absolute path names.
Solution 2:
Discussion
The source
command is the textual version of the .
(source) syntax. This allows you to include / import code from one file and merge it into a parent. However, what is not clear from a base install of Ubuntu 16.04 is why the source
line would be first. Meaning, if one wanted to use a static IP for interface enp0s3
below, does that mean I must first comment out / erase the iface
line for enp0s3
?
The file and file.d scheme is pretty common. If you run the Apache HTTP Server, you have probably seen the apache24/conf/httpd.conf and it's side kick the extra/ directory, that contains more *.conf files (typically httpd-ssl.conf, httpd-vhosts.conf, php.conf, httpd-default.conf, and more). In this case, shell scripts are not being included, but the principle is basically the same.
Why do it this way?
1. Separation of concerns.
It is better to mess up only one interface configuration up, then do something that might affect more than one interface. By keeping things separate, it actually makes it easier to write shell scripts that automate stuff.
Instead of sifting through /etc/network/interfaces
with sed
, grep
, and awk
for the correct interface, you can simply target the correct file in /etc/network/interfaces.d/
with basic file globbing.
By keeping things separate, your chances of messing things up are reduced dramatically.
2. Better organization in the long run.
If you think about what interfaces.d/ holds, it holds "instances" of network interface configurations (which can get rather elaborate). In that way, the time saved from having to scroll may make interfaces.d/ the way to go.
Conclusion
Are you planning a professional setup? Use /etc/network/interfaces.d/
to hold the configurations of your network interfaces (one file per interface).
Are you a hobbyist using Linux in your basement (on a computer with one interface) and could care less about making life easier for someone else? Use /etc/network/interfaces
and go on with your life. Comment out the default configuration settings, or remove them altogether.
Remember, though the man page for interfaces says that interface configuration commands are cumulative allowing for multiple protocols and multiple IP addresses (v4 and v6) on each interface. At the end of the day, YOU decide what goes into /etc/network/interfaces
. Like the default loop back configuration? Dump the configuration into a file and put it into /etc/netowork/interfaces.d/
.
Now, do the same for any other default install interface settings.
Leave a note in /etc/network/interfaces
that this says "This file is not to be used to alter interface configurations! Look in /etc/network/interfaces.d for the correct interface file.
Name the files in /etc/netowork/interfaces.d/
with a similar convention. Red Hat systems use an interface file prefix convention of ifcfg-.
Thus, I might go ahead and use that convention in /etc/network/interfaces/d
, absent guidance.
/etc/network/interfaces.d/ifcfg-lo
/etc/network/interfaces.d/ifcfg-enp0s3
Definitely read man interfaces
.
Good luck!