You can add an IP address to a network interface in Linux using the ifconfig command.
Here's how:
-
Identify the network interface: Use the ifconfig command without any arguments to list all available network interfaces. The output will show the name of each interface, such as eth0, wlan0, or enp0s3.
-
Add the IP address: Use the following command, replacing <interface_name> with the name of your network interface and <IP_address> with the desired IP address:
sudo ifconfig <interface_name> <IP_address> netmask <netmask>
- <netmask> is the subnet mask, which determines the network portion of the IP address. For example, a common subnet mask is 255.255.255.0.
-
Optional: Set the broadcast address: You can also set the broadcast address using the bcast option:
sudo ifconfig <interface_name> <IP_address> netmask <netmask> broadcast <broadcast_address>
- <broadcast_address> is the address used for broadcasting messages to all devices on the network.
-
Verify the configuration: After adding the IP address, use the ifconfig command again to verify that the new configuration is applied correctly.
Example:
To add the IP address 192.168.1.100 with a subnet mask of 255.255.255.0 to the network interface eth0:
sudo ifconfig eth0 192.168.1.100 netmask 255.255.255.0
Note:
- You may need root privileges (sudo) to run the ifconfig command.
- The ifconfig command is a legacy tool. For more modern and robust network configuration, consider using the ip command or network management tools like NetworkManager.