How to know the name of the network card and the IP address with commands in Linux.
Each device in a local network or an external network has a unique identifier called IP address (Internet protocol) thanks to which this device can be located in the network.
Reasons to change the IP address:
- Need to reserve this IP address to a new computer
- Resolve connectivity failures
- Administrative tasks, etc.
Step 1: Get Network Interface Name
The first important point to take into account is to know what is the interface, or card, of the network in which the IP address that we want to modify is assigned, for this, we can use the following command:
On Ubuntu and Debian
As a result, we will see the following:
We can see that the network interfaces of the computer are deployed, since it can contain several, with specific information such as:
- Interface name
- Current IP address
- Network mask
- Propagation or broadcast address
- Packages sent
- Packages with errors and many more values.
Step 2: Change IP address with Terminal
The ifconfig command will also be useful to adjust the new IP address of the equipment, for this we must take into account the following syntax:
sudo ifconfig “Interface” “New_IP_Adress” netmask 255.255.255.0
For example, if we want to set the IP address 192.168.0.20 to the interface enp0s31f6, the current one of this equipment, we will execute the following line:
sudo ifconfig enp0s31f6 192.168.0.20 netmask 255.255.255.0
Press Enter, enter the password, and we can validate again only with ifconfig that the new IP has been established:
Now, another of the options we have is to modify the Gateway, thanks to the route command; this is only necessary if a management task establishes a new router in the company.
For this we will use the following syntax:
sudo route add default gw “New_Gateway” “Interface”
For example, we can execute the next line:
sudo route add default gw 192.168.0.11 enp0s3
To get the new configuration, it will be necessary to display the routing table, and for this, we can run the following command. There we can see our new IP address in the defined gateway.
In this way, it will be practical to adjust the IP address in Linux environments in cases where it is necessary to perform some management.