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

Detailed explanation of how to access MySQL database remotely through Workbench

Preface Workbench is installed on one computer, a...

How to implement form validation in Vue

1. Installation and use First, install it in your...

Detailed explanation of keepAlive use cases in Vue

In development, it is often necessary to cache th...

Detailed explanation of Javascript basics loop

Table of contents cycle for for-in for-of while d...

Detailed Linux installation tutorial

(Win7 system) VMware virtual machine installation...

nginx solves the problem of slow image display and incomplete download

Written in front Recently, a reader told me that ...

Window.name solves the problem of cross-domain data transmission

<br />Original text: http://research.microso...

How to remove the blue box that appears when the image is used as a hyperlink

I recently used Dreamweaver to make a product pres...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

Detailed explanation of using top command to analyze Linux system performance

Introduction to Linux top command The top command...

Specific method to add foreign key constraints in mysql

The operating environment of this tutorial: Windo...

Practical experience of implementing nginx to forward requests based on URL

Preface Because this is a distributed file system...

Detailed explanation of galera-cluster deployment in cluster mode of MySQL

Table of contents 1: Introduction to galera-clust...

MYSQL string forced conversion method example

Preface Since the types of the same fields in the...