How to configure static network connection in Linux

How to configure static network connection in Linux

Configuring network connectivity for Linux systems can be challenging. Fortunately, many new Linux distributions come with network management tools that can help you automatically connect to wireless networks. But wouldn't it be nice to be able to set up a static network connection for your Linux box? This guide will show you how to check network connectivity from a CentOS/RHEL machine using different Linux tools and explain how to add static network configuration using nmcli tool.

Step 1: Check network connection

The ping command is a well-known program that can quickly check network connectivity. Enter the following into the command line:

ping -c3 opensource.com

The -c3 option means you only ping three times.

If you are connected to the Internet, you will receive a similar packet response.

Step 2: Check the connection information

You can check network information with the ip add command.

Running this command displays device information, IP address, etc. You will need this information (like device information and IP address) later to set up a static connection, so keep it in mind.

Step 3: Check network information

The network information can be found in the /etc/sysconfig/network-scripts directory by entering the following command:

ls /etc/sysconfig/network-scripts 

For example this screenshot shows ifcfg-enp0s3 and ifcfg-lo, but these depend on what version of Linux you are running and how the devices are set up.

Step 4: Display available connections

You can use the nmcli tool to display the current network connections available. Enter the following command:

nmcli con show 

This screenshot shows that there are two devices active: enp0s8 and enp0s3, which are called Wired Connection 1 and 2. But this may look different than what you see, depending on how your Linux environment is set up.

Step 5: Check if the network connection is turned on

We checked that you can receive packets using the ping command above, but now we are going to use the network command called systemctl to monitor, update, and troubleshoot network status. The command is:

systemctl status network 

If there are no issues with the network support program, you will see the status as active when you run this command.

Step 6: Add a static network connection

Now you are ready to add a static network connection. Using the device name obtained from ip add in step 2, enter the following command to add a new connection:

nmcli con add con-name "SomeName" ifname YOUR_DEVICE autoconnect yes type YOUR_CONNECTION_TYPE

Modify SomeName, YOUR_DEVICE and YOUR_CONNECTION_TYPE in the command according to your actual configuration.

Step 7: Verify that the connection has been added to the network script path

The new connection information can be modified using the nmcli tool. By the following command:

nmcli con mod

This command actually modifies the network configuration script in the /etc/sysconfig/network-scripts directory, which is another way to modify the connection information.

Check the /etc/sysconfig/network-scripts path again by entering the following command:

ls /etc/sysconfig/network-scripts 

You can see that the connection ifcfg-MyFavoriteCafe has been added.

Step 8: Verify you can see the connection

Check if MyFavoriteCafe is a visible and available connection. Use the following command to start the connection. Please note that SOME_CONNECTION_NAME should be your actual connection name (MyFavoriteCafe in this example)

nmcli con up SOME_CONNECTION NAME

It can also be turned off with the following command:

nmcli con down SOME_CONNECTION NAME

When adding a new connection, set autoconnect to true so that if you restart the network service it will be automatically enabled.

So far so good. The connection will be displayed when you run the following command:

Step 9: Change the connection to static

Open the file /etc/sysconfig/network-scripts/ifcfg-SOME_CONNECTION_NAME (MyFavoriteCafe in this example) using a text editor such as Vim, Emacs, or Nano.

To configure the connection as static, you need to modify one parameter and add three more parameters:

  • Change BOOTPROTO to static.
  • Add IPADDR. That is the static IP address you want to set, which can be seen through the ip add command.
  • Add NETMASK. This is the subnet mask, which can be found with the ip add command.
  • Add GATEWAY. This is the IP address of the default gateway, which can be found with ip add.

You may also need to add DNS, PREFIX, or other information, depending on how your network and computers are set up.

Once you have done this, save the file. Restart the network with the following command:

systemctl restart network

Check status:

systemctl status network

Step 10: Confirm that the new connection is active

This step must be done! Make sure your new connection is working properly. Run the nmcli con show command again to enable the new connection.

You can also verify that the connection is working by pinging a URL.

Finally, you can check the device information with the following command:

nmcli dev show DEVICE_NAME

Where DEVICE_NAME should be the actual name of your network device.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A detailed introduction to Linux dynamic network, static network and network configuration after cloning

<<:  Detailed explanation of JavaScript private class fields and TypeScript private modifiers

>>:  MySQL 5.7.21 winx64 installation and configuration method graphic tutorial under Windows 10

Recommend

The difference between char, varchar and text field types in MySQL

In MySQL, fields of char, varchar, and text types...

Very practical Tomcat startup script implementation method

Preface There is a scenario where, for the sake o...

Detailed explanation of the installation and use of Vue-Router

Table of contents Install Basic configuration of ...

js date and time formatting method example

js date time format Convert the date and time to ...

Detailed explanation of 10 common HTTP status codes

The HTTP status code is a 3-digit code used to in...

Logrotate implements Catalina.out log rotation every two hours

1. Introduction to Logrotate tool Logrotate is a ...

Vue+js click arrow to switch pictures

This article example shares the specific code of ...

A brief discussion on Linux virtual memory

Table of contents origin Virtual Memory Paging an...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...

The whole process of implementing the summary pop-up window with Vue+Element UI

Scenario: An inspection document has n inspection...

JavaScript Function Currying

Table of contents 1 What is function currying? 2 ...

Vue implements nested routing method example

1. Nested routing is also called sub-routing. In ...

The main differences between MySQL 4.1/5.0/5.1/5.5/5.6

Some command differences between versions: show i...

The scroll bar position is retained when scrolling the vant list component

The scroll bar position is retained when scrollin...