TCP/IP Configuration  «Prev  Next»

Lesson 7 Setting the IP address and netmask
ObjectiveUse ifconfig command to set IP address and netmask?

Modern Networking Tools which enable setting of 1) IP address and 2) netmask

Question: Which modern networking tools enable you to set the IP address and netmask on the Linux operating system?
In modern Linux operating systems, network configuration and management have evolved to include sophisticated tools that offer enhanced functionality and ease of use compared to traditional utilities. Among these, two standout tools enable administrators to set IP addresses and netmasks efficiently:
  1. `ip` from the `iproute2` suite and
  2. `nmcli` from NetworkManager.

  1. `ip` Command (`iproute2` Suite) : The `ip` command, part of the `iproute2` suite, is a versatile tool designed for managing network interfaces, routes, and policies. It replaces older tools like `ifconfig` and `route`, providing a more powerful and consistent interface for network configuration. To set an IP address and netmask on an interface using the `ip` command, you would use the following syntax:
    ip addr add <IP address>/<netmask> dev <interface name>
    

    For example, to assign the IP address `192.168.1.100` with a netmask of `255.255.255.0` (or `/24` in CIDR notation) to the `eth0` interface, you would use:
    ip addr add 192.168.1.100/24 dev eth0
    

    This command configures the specified interface with the desired IP address and netmask, effectively setting up the network interface for communication within the specified network.
  2. `nmcli` (NetworkManager Command Line Interface):`nmcli` is the command-line interface for NetworkManager, a dynamic network control and configuration system aimed at simplifying network management in Linux. NetworkManager is particularly well-suited for desktops, laptops, and servers with dynamic network configurations, such as those connecting to various networks or using wireless connections. To set an IP address and netmask on an interface using `nmcli`, the command would be structured as follows:
    nmcli con mod <connection name> ipv4.addresses <IP address>/<netmask>
    nmcli con up <connection name>
    

    For instance, to configure the connection named `Wired connection 1` with the IP address `192.168.1.100` and a netmask of `255.255.255.0`, you would execute:
    nmcli con mod "Wired connection 1" ipv4.addresses 192.168.1.100/24
    nmcli con up "Wired connection 1"
    

    This sequence of commands assigns the specified IP address and netmask to the network connection and activates the connection with the new settings.

Both `ip` and `nmcli` provide robust mechanisms for network configuration on Linux, catering to a range of scenarios from static server setups to dynamic client environments. Their comprehensive feature sets and flexibility make them indispensable tools in the toolkit of modern Linux network administrators.


Question: Can I use the "ifconfig command" to set the IP address and netmask on the Linux Operating System?
You can use the `ifconfig` command to set the IP address and netmask on the Linux operating system. To do this, you will need to specify the name of the network interface that you want to configure, as well as the new IP address and netmask. For example, to set the IP address of the network interface `eth0` to `192.168.1.100` and the netmask to `255.255.255.0`, you would use the following command:
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

You can also use the `ifconfig` command to set the IP address and netmask using CIDR notation. For example, to set the IP address of the network interface `eth0` to `192.168.1.100/24`, you would use the following command:
ifconfig eth0 192.168.1.100/2:4

Note: The `ifconfig` command is deprecated in some newer versions of Linux. It is recommended to use the `ip` command instead. However, the `ifconfig` command is still supported by most Linux distributions, and it is still commonly used by system administrators.
Important: Before setting the IP address and netmask of a network interface, make sure that you know the correct values to use. If you set the wrong IP address or netmask, you may not be able to connect to the network.

Setting IP Address Netmask using legacy ifconfig command

How do you use the ifconfig command to set the IP address and netmask? This use of the ifconfig command is almost always part of the boot procedure (or, in the case of PPP, part of the dial-up procedure).
The ifconfig command will set the IP address, netmask, and other flags on a device.
For example, to set IP address 131.197.25.13 and netmask 255.255.255.0, enter:
host# /sbin/ifconfig eth0 131.197.25.13 
netmask 255.255.255.0

ifconfig Command

The ifconfig command may also be used to configure interfaces for different networking protocols, and may be used to set other parameters on the interface (though doing so is rarely necessary). In addition, ifconfig will often take a host name, rather than an IP address, as an argument:
host# /sbin/ifconfig eth0 [machine_name] 
netmask 255.255.255.0

In this situation, the IP address will be extracted from the /etc/hosts file.

IP Address and Netmask

The assignment of the IP address and netmask is part of the boot process. Red Hat© Linux uses a directory of scripts called /etc/sysconfig/network-scripts to configure interfaces. Each interface has a file called if-interface (if-eth0, for example), where the basic parameters for the interface are set. Then ifconfig is run by the ifup script, which is started from the run level 3 startup scripts.

SEMrush Software