Setting up a server with multiple IP addresses is a common task, often handled by automated tools like Cloud-init or NetworkManager. However, these tools do not always perform perfectly in every situation, particularly on AlmaLinux. In my experience, I have found that manually configuring IP addresses can be the best solution when automation falls short.
For instance, while configuring virtual machines for a client using AlmaLinux, the automation provided by WHMCS and Cloud-init did not work as expected. This led me to switch to a manual setup, which ultimately gave me the control and precision needed to resolve the issue. In this guide, I will walk you through the same manual process, which can be especially helpful if you encounter similar challenges or prefer a hands-on approach.
Although tools like Cloud-init and NetworkManager can assist with setting up multiple IPs, this guide focuses on the manual method. We will cover three main steps:
- Creating custom routing tables to manage multiple IP addresses.
- Linking IP addresses to these routing tables to ensure proper network paths.
- Configuring and testing the network routes to verify that everything is working correctly.
Note: Before you proceed, ensure you have root access or sufficient privileges on your AlmaLinux server. This guide also assumes familiarity with basic networking concepts and command-line operations.
Preparing Your IP Information
Before we begin the process to manually configure multiple IPs in AlmaLinux, it is important to have the necessary IP information on hand. Typically, when your hosting provider assigns you a block of IP addresses, they will also provide details such as the subnet mask, gateway, and broadcast address. Some hosting providers, like OVH, may also require you to bind each IP address to a specific MAC address for the IPs to function correctly.
Example IP Information:
Let’s assume your hosting provider has allocated the following IP block:
- IP Range: 192.0.2.16 to 192.0.2.31
- Subnet Mask: 255.255.255.240 (/28)
- Gateway: 192.0.2.1
- Broadcast Address: 192.0.2.31
- Usable IP Addresses: 192.0.2.17 to 192.0.2.30
- MAC Addresses: Provided by your hosting provider for each IP
Explanation of the Subnet (/28):
A /28
subnet mask (255.255.255.240) means that the first 28 bits of the IP address are used for network identification, leaving 4 bits for host addresses. This provides 16 IP addresses in total, with 2 reserved for network and broadcast addresses, leaving 14 usable IP addresses.
Here’s how the IP block breaks down:
- Network Address: 192.0.2.16 (reserved)
- First Usable IP: 192.0.2.17
- Last Usable IP: 192.0.2.30
- Broadcast Address: 192.0.2.31 (reserved)
You will configure your server using the IPs within this usable range (192.0.2.17 to 192.0.2.30), along with the specified gateway and subnet mask. Be sure to adjust these values according to the specific details provided by your hosting provider.
Binding IPs to MAC Addresses (For Providers like OVH)
If your hosting provider requires MAC address binding, as is common with OVH, you must ensure that each public IP address is associated with the correct MAC address. This step is crucial for the IPs to function properly on your server.
- Obtain MAC Addresses: Your hosting provider should provide a MAC address for each IP address. Ensure you have these details before proceeding.
- Assign MAC Addresses: Depending on your network setup, you may need to bind each IP address to its corresponding MAC address in your network interface configuration. This is typically done in the hosting provider’s control panel or directly in your server’s network configuration files.
If OVH or your hosting provider offers specific tools or steps for binding MAC addresses to IPs, follow those guidelines as they may vary.
Step 1: Creating Custom Routing Tables on AlmaLinux
Routing tables are a key part of how your AlmaLinux server handles network traffic. When your server has multiple IP addresses, setting up custom routing tables ensures that each IP address sends and receives data through the correct network interface.
What Are Routing Tables?
A routing table is like a guide that your AlmaLinux server uses to decide where to send data based on the destination IP address. By creating custom routing tables, you can direct the data from each IP address to go through a specific network interface. This is especially useful when your server is using multiple IP addresses and needs to manage traffic efficiently.
How to Add Custom Routing Tables on AlmaLinux
To configure your AlmaLinux server to handle multiple IP addresses effectively, you will need to define custom routing tables. Here’s how to do it:
- Access the Routing Tables Configuration File: Start by opening the routing tables configuration file on your AlmaLinux server, where you will define the new routing tables. Use a text editor like
nano
:
nano /etc/iproute2/rt_tables
- Add Your Custom Routing Tables: In the file, add a line for each network interface you want to manage. Each routing table needs a unique number (which acts as an identifier) and the name of the network interface:
echo "100 eth0" >> /etc/iproute2/rt_tables
echo "200 eth1" >> /etc/iproute2/rt_tables
The numbers 100
and 200
identify the routing tables, and eth0
and eth1
are the names of the network interfaces on your AlmaLinux server.
- Save and Close the File: After adding the entries, save your changes and close the file. In
nano
, pressCtrl + O
to save andCtrl + X
to exit.
Checking Your Configuration on AlmaLinux
To make sure your custom routing tables were added correctly on AlmaLinux, check the contents of the file:
cat /etc/iproute2/rt_tables
You should see the lines you added, like this:
100 eth0
200 eth1
This confirms that your AlmaLinux server now has custom routing tables set up for these network interfaces.
Tips to Keep in Mind
- Interface Names: Double-check that the names (
eth0
,eth1
) match the actual network interfaces on your AlmaLinux server. You can list all network interfaces using theip link show
command. - Unique Numbers: The numbers (
100
,200
) should be unique to avoid any conflicts with other entries in the routing tables file.
By following these steps, you ensure your AlmaLinux server can handle multiple IP addresses effectively, with each one using the correct path through the network.
Step 2: Linking IP Addresses to Custom Routing Tables on AlmaLinux
Now that you have created custom routing tables on your AlmaLinux server, the next step is to link your IP addresses to these tables. This process ensures that each IP address within your allocated range follows the correct routing path based on the network interface it is assigned to.
Why Link IP Addresses to Routing Tables?
Linking IP addresses to specific routing tables is important for managing how data is routed through your network interfaces. This setup is particularly useful if your AlmaLinux server has multiple network interfaces, allowing you to control the flow of traffic and prevent routing conflicts.
How to Link IP Addresses to Routing Tables
To link each IP address from your allocated range to its corresponding routing table, you will use the ip rule add
command. This command specifies which routing table an IP address should use.
- Linking an IP Address to a Routing Table: Using the example IP range provided earlier, you can link your IP addresses to the appropriate routing tables. For instance:
ip rule add from 192.0.2.17 table eth0
ip rule add from 192.0.2.18 table eth1
Here, the IP address 192.0.2.17
is linked to the eth0
routing table, and 192.0.2.18
is linked to the eth1
routing table. Ensure that these IPs fall within your usable range (e.g., 192.0.2.17 to 192.0.2.30
).
- MAC Address Binding (If Required by Your Provider): If your hosting provider, like OVH, requires you to bind IP addresses to specific MAC addresses, ensure that each IP address is correctly associated with its MAC address. This might need to be done in your hosting provider’s control panel or configured directly in your AlmaLinux network settings.Example:
If 192.0.2.17
is bound to MAC address 00:1A:2B:3C:4D:5E
, confirm this binding before linking the IP to a routing table.
- Repeat for Additional IP Addresses: If you have more IP addresses in your range that need to be linked to routing tables, repeat the above command for each IP. Be sure to use the correct routing table and interface for each address.
Verifying the IP to Routing Table Links
After linking the IP addresses to their respective routing tables, verify the configuration by listing the active routing rules:
ip rule show
This command will display the current routing rules on your AlmaLinux server, allowing you to confirm that each IP address is correctly linked to its routing table and, if applicable, the correct MAC address.
Tips to Keep in Mind
- Correct IP Addresses and MAC Binding: Ensure that the IP addresses you are linking to the routing tables match those in your allocated range and that they are bound to the correct MAC addresses if required.
- Routing Table Names: The names of the routing tables (
eth0
,eth1
) should match the ones you configured earlier. Consistency here is key to preventing misconfigurations.
By successfully linking your IP addresses to the custom routing tables and ensuring any required MAC bindings, your AlmaLinux server is now configured to route traffic correctly based on the specific IP address and network interface.
Step 3: Configuring Default Routes on AlmaLinux
After you have linked your IP addresses to the appropriate routing tables, the next step is to configure the default routes for these tables. A default route specifies the path that network traffic should take when no other more specific routes match the destination address. This ensures that your server can properly handle outbound traffic.
Why Set Up Default Routes?
A default route acts as a fallback path for any network traffic that doesn’t match a more specific route in your routing table. By configuring default routes for each of your custom routing tables, you ensure that traffic from each IP address on your AlmaLinux server is routed correctly, even when there isn’t a direct match in the routing table.
How to Configure Default Routes
To set up default routes for your custom routing tables, you will use the ip route add
command. This command adds a default route to each of your routing tables, directing traffic through the appropriate gateway.
Start by adding the default routes to your routing tables. For example, if your gateway is 192.0.2.1
and you have two network interfaces, eth0
and eth1
, you would execute the following commands:
ip route add default via 192.0.2.1 dev eth0 table eth0
ip route add default via 192.0.2.1 dev eth1 table eth1
These commands specify that any traffic from the eth0
interface, when no other route matches, will be sent through the gateway 192.0.2.1
. The same applies to the eth1
interface.
If you have additional routing tables associated with other network interfaces, you would repeat the command, adjusting the interface and table names accordingly. For example, if you have a third interface eth2
, the command would look like this:
ip route add default via 192.0.2.1 dev eth2 table eth2
Verifying Default Routes on AlmaLinux
To ensure that the default routes have been added correctly, you can check the routes in each routing table by using the following commands:
ip route show table eth0
ip route show table eth1
Running these commands will display the routes within the specified routing tables, allowing you to confirm that the default routes are correctly configured.
Tips to Keep in Mind
- Correct Gateway: Ensure that the gateway IP (
192.0.2.1
in this example) is correct and provided by your hosting provider. This is the IP address through which your server will send outbound traffic. - Interface Matching: The network interface names (
eth0
,eth1
) should match the ones you have configured on your server. Double-check to prevent any routing issues.
By configuring default routes, your AlmaLinux server will be able to handle network traffic efficiently, ensuring that all outbound traffic has a clear path, even when it doesn’t match a specific route in your tables.
Step 4: Testing the Configuration on AlmaLinux
After setting up your routing tables and default routes, it’s important to test the configuration to make sure everything is functioning as intended. Testing will help you confirm that each IP address is correctly routed through the assigned network interface and that the traffic flows as expected.
Why Testing Is Important
Testing your configuration ensures that all the hard work you’ve put into setting up your AlmaLinux server pays off. By verifying that the routing tables and default routes are working, you can catch any potential issues early and make adjustments before they cause problems in a live environment.
How to Test the Configuration
- Ping External Servers from Each IP Address: The most straightforward way to test your configuration is by pinging an external server from each of the IP addresses you have configured. This will verify that the traffic from each IP address is correctly routed through the network interface it is associated with.For example, to test the IP address
192.0.2.17
configured oneth0
, use the following command:
ping -c 4 -I 192.0.2.17 8.8.8.8
This command sends 4 ICMP echo requests (pings) to Google’s public DNS server (8.8.8.8
) using the IP address 192.0.2.17
. Replace 192.0.2.17
with any other IP address you wish to test.
Similarly, to test another IP, say 192.0.2.18
on eth1
, use:
ping -c 4 -I 192.0.2.18 8.8.8.8
- Check for Successful Responses: After running the ping command, review the output to ensure that you receive responses from the external server. A successful response will look something like this:
64 bytes from 8.8.8.8: icmp_seq=1 ttl=120 time=14.7 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=120 time=14.9 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=120 time=15.1 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=120 time=14.8 ms
- If the pings are successful, this indicates that the IP address is correctly routed through the network interface and can communicate with external servers.
- Troubleshoot Any Issues: If you do not receive responses or see an error message like
Destination Host Unreachable
, it could indicate a problem with your configuration. Here are a few things to check:- Verify that the IP address is correctly linked to the appropriate routing table.
- Ensure that the default route is set up correctly for the routing table.
- Double-check the network interface configuration and ensure that the IP address is properly assigned.
Final Verification and Considerations
- Multiple IP Testing: It’s important to test each IP address you’ve configured to ensure that all routes and interfaces are functioning as expected.
- Check Logs: If you encounter any issues, checking the system logs (
/var/log/messages
orjournalctl -xe
) can provide more insight into what might be going wrong.
By thoroughly testing your configuration, you can be confident that your AlmaLinux server is set up correctly to handle multiple IP addresses, with traffic being routed through the right interfaces.
Step 5: Automating the Routing Configuration with a Systemd Service
After successfully configuring and testing your routing setup on AlmaLinux, it is important to automate these configurations so that they persist across reboots. By creating a Systemd service, you can ensure that your routing rules are automatically applied every time your server starts, eliminating the need for manual intervention and reducing the risk of downtime or misconfiguration.
Creating the Routing Script
The first step in automating your configuration is to create a script that contains all the routing commands you have configured. This script will be executed by the Systemd service at startup. Begin by creating the script in a directory like /usr/local/bin/
and naming it something descriptive, such as routing-setup.sh
. To create and edit this script, open it in a text editor using the following command:
nano /usr/local/bin/routing-setup.sh
Inside the script, add the routing commands you have been using. For example, your script might look like this:
#!/bin/bash
sleep 10
ip rule add from 192.0.2.17 table eth0
ip rule add from 192.0.2.18 table eth1
ip route add default via 192.0.2.1 dev eth0 table eth0
ip route add default via 192.0.2.1 dev eth1 table eth1
Once you have added these commands, save the file and close the editor.
Making the Script Executable
For the Systemd service to run your script, it needs to be executable. You can make the script executable by running the following command:
chmod +x /usr/local/bin/routing-setup.sh
Creating the Systemd Service File
Next, you need to create a Systemd service file that will call your script during the system boot process. Place this service file in /etc/systemd/system/
and name it routing-setup.service
. To create and edit the service file, use the following command:
nano /etc/systemd/system/routing-setup.service
In the service file, add the following configuration:
[Unit]
Description=Routing Setup Service
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/routing-setup.sh
[Install]
WantedBy=multi-user.target
This configuration tells Systemd to run the routing-setup.sh
script after the network has been initialized.
Enabling and Starting the Service
Once the service file is created, you need to enable the service so that it runs automatically every time the server boots. You can enable the service with this command:
systemctl enable routing-setup.service
If you want to apply the routing setup immediately without rebooting the server, you can start the service manually using:
systemctl start routing-setup.service
Verifying the Systemd Service
To ensure that the Systemd service is functioning correctly, check its status with the following command:
systemctl status routing-setup.service
This command will display whether the service started successfully and will highlight any issues that occurred during execution.
Before relying on the Systemd service, it is a good practice to test the script manually to ensure all commands execute without errors. If you encounter any problems, checking the system logs using journalctl -xe
can provide more details on potential issues.
By automating your routing configuration with a Systemd service, you ensure that your AlmaLinux server is always correctly configured, even after reboots. This automation adds a layer of reliability to your server’s network management, minimizing the need for manual intervention.
Final Step: Finalizing the Configuration and Rebooting the Server
With your routing setup configured, tested, and automated, the last step is to reboot your AlmaLinux server. This reboot is essential to confirm that all configurations persist across reboots, ensuring your server remains fully operational with the new settings.
Rebooting Your AlmaLinux Server
To initiate the reboot, use the following command:
reboot
This command will shut down and restart your server, applying all the configurations and services you have set up, including the routing setup.
Post-Reboot Verification
After the server reboots, it’s important to verify that everything is working as expected:
- Check the Systemd Service Status: Ensure that the Systemd service for routing started correctly by running:
systemctl status routing-setup.service
This confirms whether the service executed as intended during startup.
- Verify Routing Tables: Use the following commands to ensure the routing tables are correctly populated:
ip route show table eth0
ip route show table eth1
This step verifies that your default routes and IP rules are in place.
- Test Network Connectivity: Confirm that your IP addresses are functioning properly by pinging an external server:
ping -c 4 -I 192.0.2.17 8.8.8.8
ping -c 4 -I 192.0.2.18 8.8.8.8
Successful responses indicate that your IPs are correctly routed.
Troubleshooting
If any issues arise after the reboot, consider these steps:
- Review System Logs: Check the logs using
journalctl -xe
to identify potential problems during startup. - Manual Script Execution: If needed, run the
routing-setup.sh
script manually to ensure it works without errors. - Service File Review: Double-check the
routing-setup.service
file for any misconfigurations.
By following these steps, you confirm that your AlmaLinux server is properly set up to handle multiple IP addresses, with all routing rules applied as intended.
Wrapping Up
You have successfully configured your AlmaLinux server to manage multiple IP addresses efficiently. By creating custom routing tables, linking IP addresses, setting up default routes, and automating the process with a Systemd service, you have ensured that your server is well-prepared to handle network traffic reliably across reboots.
If you find that automated tools such as WHMCS or Cloud-init do not fully meet your needs, this manual approach provides you with greater control and customization over your server’s network configuration. Should you have any questions or wish to share your experience with this setup, please feel free to comment below. Your feedback is always welcome.