Skip to main content

Adding an IP Address and a Static Route

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

Managing network settings is a crucial ability for a System Administrator to have. In today's environment, adding and deleting IP addresses and static routes is an expected capability. In this activity, we will be creating one script to add an IP address and a static route, then another script to remove an IP address and a static route.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Adding an IP Address and a Static Route

Introduction

Managing network settings is a crucial ability for a System Administrator to have. In today's environment, adding and deleting IP addresses and static routes is an expected capability. In this activity, we will be creating one script to add an IP address and a static route, then another script to remove an IP address and a static route.

The Scenario

The lead System Administrator has asked us to create a couple of scripts to be used by a developer which will modify the network interface. They need the IP address 10.0.5.20/24 assigned to the host, and a static route to provide access to another host which will be provisioned on a different subnet with the specification of 10.0.6.0/24. Our system already has an address of 10.0.5.19/24 assigned to the ens5 interface.

We have been asked to create the /root/net-up.sh script, which will add the address 10.0.5.20/24 to the ens5 interface, and the /root/net-down.sh script which will delete the same address from the same interface, using ip a commands.

We have also been asked to have the /root/net-up.sh script add a route so the host 10.0.5.5 will be used as a router to provide access to the host on the 10.0.6.0/24 subnet, using the ens5 interface and utilizing the ip r command. The /root/net-down.sh script should delete the same route from the interface using the ip r command. Unfortunately, the host 10.0.6.19/24 has not yet been provisioned, so we will have to rely on the routing table output, and will not be able to verify connectivity with this host.

Once we believe our net-up.sh and net-down.sh scripts have been created and updated, we'll have to verify and double-check that they work correctly.

Logging In

Use the credentials provided on the hands-on lab overview page, and log in as cloud_user. We'll need to be root right away, so let's run sudo -i as soon as we get in, then we can continue.

Add and Delete the IP Address 10.0.5.20/24 from the ens5 Interface, and Create net-up.sh and net-down.sh Scripts

Adding the Address

To see how the network interfaces are currently configured, run ip a by itself. To add the address 10.0.5.20/24 to the ens5 interface, we'll run this:

ip a add 10.0.5.20/24 dev ens5

If we check again with another ip a, we'll see that the inet address has been added to the ens5 interface.

Adding the Command to the Appropriate Script

The command does what we want, so let's create the script, and make it executable:

echo ip a add 10.0.5.20/24 dev ens5 > net-up.sh
chmod +x net-up.sh

Deleting the Address

Now let's delete that address from the interface:

ip a del 10.0.5.20/24 dev ens5

Check again with ip a to make sure. If everything is fine, we can move on.

Adding the Command to the Appropriate Script

This new command needs to go into the net-down.sh script, and we need to make it executable:

echo ip a del 10.0.5.20/24 dev ens5 > net-down.sh
chmod +x net-down.sh

Running the Scripts

Now let's try running both scripts, and checking whether they're working as we go:

./net-up.sh
ip a
./net-down.sh
ip a

The first ip a (meaning ip a by itself) command should show our address was added to the interface, and the second should show that it was deleted.

Update net-up.sh and net-down.sh Scripts to Add and Delete a Route, Using 10.0.5.5 as a Router, to Provide Access to the 10.0.6.0/24 Network

Adding the Route

Using ip r by itself will show the current routing status. There is nothing in the output showing anything relating to the 10.0.6.0/24 subnet, but we can add it with this:

ip r add 10.0.6.0/24 via 10.0.5.5 dev ens5

Now if we run ip r again, we'll see a line regarding 10.0.6.0/24.

Adding the Command to the Appropriate Script

Let's get this new command into our net-up.sh script:

echo ip r add 10.0.6.0/24 via 10.0.5.5 dev ens5 >> net-up.sh

Note the double >> here. We're appending this command to the script. > would overwrite what's there. We're going to do the same thing in a bit with removing this route.

Deleting the Route

To delete the route we just created, we'll run this:

ip r del 10.0.6.0/24 via 10.0.5.5 dev ens5

Check with ip r to make sure it's in fact gone.

Adding the Command to the Appropriate Script

If the route is gone, we're all set to add this command to the net-down.sh script:

echo ip r del 10.0.6.0/24 via 10.0.5.5 dev ens5 >> net-down.sh

Remember, use >>, not just >.

Verify the Scripts Are Working

Execute the net-down.sh script. Check the current IP address information and it should not contain 10.0.5.20. Nor should the routing table contain 10.0.5.5. Let's run the script, then check those two things (IP and then route):

./net-down.sh
ip a | grep 10.0.5.20
ip r | grep 10.0.5.5

Now let's execute the net-up.sh script and check those to things again (IP and then route):

./net-up.sh
ip a | grep 10.0.5.20
ip r | grep 10.0.5.5

We should see that ens5 has those IP and router addresses. Let's check net-down.sh again to make sure it's working too:

./net-down.sh
ip a | grep 10.0.5.20
ip r | grep 10.0.5.5

We shouldn't see any output from the ip a and ip r commands.

Conclusion

We are finished. We've created a couple of scripts that let our developer get in and manipulate networking settings easily. Congratulations! ens5