
Network setup in Linux through config files, part 1
My first communication with Linux took place about six years ago. Then it was some freshly released Red Hat , which my friend and I were able to install, but at the same time we did not succeed in entering it.
However, the article is not about that. Later almost all Linux distribution families went through my hands and head, and everywhere I noticed my approaches to network auto-tuning. And in this series of articles I will try to highlight the most popular of them. I hope they will be useful to those users who still click on the buttons and tick off the graphical settings managers, but already understand that this is not true :)
Perhaps these articles will also be informative to those who (not from a lot of knowledge) write their scripts network management and puts them in some rc.local
So, in the first part we will talk about the number one family, one of the most extensive in the number of distributions, Red Hat based .
This, for example, includes such distributions now used as:
There you will find several ifup- * scripts and as many ifdown- * scripts that respectively raise or lower a certain the type of interface, as well as ifcfg- * (an asterisk is the name) where the settings for these interfaces are stored.
If the network is not yet configured, then you will find ifcfg-lo describing the loopback interface there. In my Fedora Core 7, which I had on hand, this file looks like this:
Not all parameters listed here are required. Often, to specify an interface, just specify IPADDR and NETMASK. The DEVICE parameter is necessary if you want the script name after “ifcfg-” to be not an interface name, but some other word. Here you can specify parameters such as GATEWAY, BOOTPROTO (static or dhcp), HWADDR (if you want to change the mac-address of the interface) and so on. A complete list of possible parameters depends on the type of interface to be raised and the distribution. And, given the scarcity of official information, it can only be recognized and recognized by shoveling scripts.
These config files can be used to set various ppp interfaces, ip tunnels, vlan's, and so on.
The following example shows the configuration of the ipip tunnel (ifcfg-tun0):
In this example, having the address 172.16.0.2, we create a tunnel with the machine 192.168.0.1, indicating TTL = 255, and assign the address 10.0.0.2 peer 10.0.0.1 to the tunnel.
Not everyone knows that in addition to ifcfg- * files, the corresponding rule- * and route- * files can be placed in the same directory.
They are needed, respectively, to prescribe the routing rules and the routes themselves (
For the above tunnel, these files may look like this.
rule-tun0: route-tun0: Having created these files, we take off the headache that the corresponding rules and routes are created and deleted when the interface is raised and lowered, respectively.
Another interesting feature is the automatic creation of aliases. If you have an eth0 interface with the address 192.168.0.1, and you want to hang also 192.168.0.2, then just create an ifcfg-eth0: 1 file, which should be entered in addition to the above parameters (IPADDR, NETMASK, and so on), one more - REALDEVICE = eth0.
If suddenly there was a need to create a large number of aliases, and you do not want to create separate files for each, then there is a way out: ifcfg-eth0-range will save us, which might look like this: This example will create the interfaces eth0: 3 - eth0: 13 with addresses from 192.168.0.5 to 192.168.0.15. And finally, one can not help but mention the ifup.d and ifdown.d directories that are in the same directory (in / etc / sysconfig / network-scripts).
In these directories you can put your files, which will be executed when raising and lowering the interface. Your scripts will be passed one parameter $ 1 - this is the name of the interface that has been raised or omitted.
Having a network configured according to these principles, you can always raise / lower a separate interface with the ifup name (ifdown name) command, where name is your interface. To reboot the entire network, just type service network reload.
The next part will be devoted not to the family, but to one distribution - Alt Linux. Despite its direct RedHat roots, the developers almost completely re-wrote the entire network management system, which received the name etcnet and deserved (in my opinion) closer attention :)
However, the article is not about that. Later almost all Linux distribution families went through my hands and head, and everywhere I noticed my approaches to network auto-tuning. And in this series of articles I will try to highlight the most popular of them. I hope they will be useful to those users who still click on the buttons and tick off the graphical settings managers, but already understand that this is not true :)
Perhaps these articles will also be informative to those who (not from a lot of knowledge) write their scripts network management and puts them in some rc.local
So, in the first part we will talk about the number one family, one of the most extensive in the number of distributions, Red Hat based .
This, for example, includes such distributions now used as:
- Fedora
- RHEL / CentOS
- Mandriva
- ASPLinux
There you will find several ifup- * scripts and as many ifdown- * scripts that respectively raise or lower a certain the type of interface, as well as ifcfg- * (an asterisk is the name) where the settings for these interfaces are stored.
If the network is not yet configured, then you will find ifcfg-lo describing the loopback interface there. In my Fedora Core 7, which I had on hand, this file looks like this:
DEVICE=lo
IPADDR=127.0.0.1
NETMASK=255.0.0.0
NETWORK=127.0.0.0
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
Not all parameters listed here are required. Often, to specify an interface, just specify IPADDR and NETMASK. The DEVICE parameter is necessary if you want the script name after “ifcfg-” to be not an interface name, but some other word. Here you can specify parameters such as GATEWAY, BOOTPROTO (static or dhcp), HWADDR (if you want to change the mac-address of the interface) and so on. A complete list of possible parameters depends on the type of interface to be raised and the distribution. And, given the scarcity of official information, it can only be recognized and recognized by shoveling scripts.
These config files can be used to set various ppp interfaces, ip tunnels, vlan's, and so on.
The following example shows the configuration of the ipip tunnel (ifcfg-tun0):
DEVICE=tun0
MY_OUTER_IPADDR=172.16.0.2
PEER_OUTER_IPADDR=192.168.0.1
MY_INNER_IPADDR=10.0.0.2
PEER_INNER_IPADDR=10.0.0.1
TYPE=IPIP
TTL=255
In this example, having the address 172.16.0.2, we create a tunnel with the machine 192.168.0.1, indicating TTL = 255, and assign the address 10.0.0.2 peer 10.0.0.1 to the tunnel.
Not everyone knows that in addition to ifcfg- * files, the corresponding rule- * and route- * files can be placed in the same directory.
They are needed, respectively, to prescribe the routing rules and the routes themselves (
ip rule, ip route
), for example, when using source-policy routing . For the above tunnel, these files may look like this.
rule-tun0: route-tun0: Having created these files, we take off the headache that the corresponding rules and routes are created and deleted when the interface is raised and lowered, respectively.
from 10.0.0.2 lookup mytable
default dev tun0 table mytable
192.168.0.0/24 dev tun0
Another interesting feature is the automatic creation of aliases. If you have an eth0 interface with the address 192.168.0.1, and you want to hang also 192.168.0.2, then just create an ifcfg-eth0: 1 file, which should be entered in addition to the above parameters (IPADDR, NETMASK, and so on), one more - REALDEVICE = eth0.
If suddenly there was a need to create a large number of aliases, and you do not want to create separate files for each, then there is a way out: ifcfg-eth0-range will save us, which might look like this: This example will create the interfaces eth0: 3 - eth0: 13 with addresses from 192.168.0.5 to 192.168.0.15. And finally, one can not help but mention the ifup.d and ifdown.d directories that are in the same directory (in / etc / sysconfig / network-scripts).
IPADDR_START=192.168.0.5
IPADDR_END=192.168.0.15
CLONENUM_START=3
In these directories you can put your files, which will be executed when raising and lowering the interface. Your scripts will be passed one parameter $ 1 - this is the name of the interface that has been raised or omitted.
Having a network configured according to these principles, you can always raise / lower a separate interface with the ifup name (ifdown name) command, where name is your interface. To reboot the entire network, just type service network reload.
The next part will be devoted not to the family, but to one distribution - Alt Linux. Despite its direct RedHat roots, the developers almost completely re-wrote the entire network management system, which received the name etcnet and deserved (in my opinion) closer attention :)