Skip to main content

IP Addressing

IP addressing is a fundamental concept in networking. An IP address is a unique numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication.

IPv4 vs IPv6

  • IPv4: An IPv4 address is a 32-bit number, typically represented in dotted-decimal notation (e.g., 192.168.1.1). It provides approximately 4.3 billion unique addresses.
  • IPv6: An IPv6 address is a 128-bit number, typically represented in hexadecimal notation (e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334). It was created to address the exhaustion of IPv4 addresses and provides a virtually unlimited number of unique addresses.

Subnet Mask & CIDR Notation

  • Subnet Mask: A subnet mask is used to divide an IP address into two parts: the network address and the host address. For example, a subnet mask of 255.255.255.0 indicates that the first 24 bits are the network portion and the last 8 bits are the host portion.
  • CIDR Notation: Classless Inter-Domain Routing (CIDR) notation is a more compact way to represent a subnet mask. It is written as a slash followed by the number of bits in the network portion of the address. For example, 192.168.1.1/24 is equivalent to 192.168.1.1 with a subnet mask of 255.255.255.0.

Private vs Public IP

  • Public IP: A public IP address is a globally unique address that is assigned by an Internet Service Provider (ISP). It is used for communication over the internet.
  • Private IP: A private IP address is a non-routable address that is used within a private network. Private IP addresses are defined in RFC 1918 and fall within the following ranges:
    • 10.0.0.0 to 10.255.255.255
    • 172.16.0.0 to 172.31.255.255
    • 192.168.0.0 to 192.168.255.255

Example: How to assign IP using nmcli or /etc/sysconfig/network-scripts/

Using nmcli (NetworkManager command-line tool)

# Assign a static IP address
sudo nmcli con mod 'enp0s3' ipv4.addresses 192.168.1.10/24

# Set the default gateway
sudo nmcli con mod 'enp0s3' ipv4.gateway 192.168.1.1

# Set the DNS servers
sudo nmcli con mod 'enp0s3' ipv4.dns "8.8.8.8 8.8.4.4"

# Set the connection to manual to apply the static IP
sudo nmcli con mod 'enp0s3' ipv4.method manual

# Restart the connection
sudo nmcli con up 'enp0s3'

Using /etc/sysconfig/network-scripts/ (for RHEL/CentOS)

Create or edit the ifcfg-<interface-name> file (e.g., ifcfg-eth0):

# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4

Then, restart the network service:

sudo systemctl restart network