The IP address in Linux systems is essentially a logical address used to identify devices in a network, divided into two main categories: IPv4 and IPv6. In daily use, IPv4 is predominant, and the configuration methods are categorized into temporary and permanent configurations.
Classification of IP Addresses
01
The IP addresses in Linux systems follow the TCP/IP protocol specification, mainly divided into two categories, which differ significantly in format and address range.
1. IPv4 Address
-
Format: Composed of 4 groups of decimal numbers ranging from 0 to 255, separated by periods, for example, 192.168.1.100.
-
Address Range:
-
Public IP: Assigned by the ISP, can be accessed directly on the internet, such as 202.103.0.1.
-
Private IP: Used only for internal communication within a local area network, cannot be accessed directly from the internet.
-
Characteristics: Address resources are limited and have nearly been exhausted, requiring NAT (Network Address Translation) to share a public IP among multiple devices.
2. IPv6 Address
- Format: Composed of 8 groups of hexadecimal numbers ranging from 0 to FFFF, separated by colons, for example, 2001:0db8:85a3:0000:0000:8a2e:0370:7334 (can be simplified to 2001:db8:85a3::8a2e:370:7334, using double colons to replace consecutive groups of 0).
- Address Range: Approximately 3.4×10³⁸, sufficient to meet the future needs of the Internet of Things.
- Characteristics: Automatically supports configuration (SLAAC), no manual allocation is required, and built-in IPsec encryption provides higher security, but its prevalence in Linux servers is still lower than that of IPv4.
Configuration Methods for IP Addresses
02
In Linux, IP address configuration mainly includes “temporary configuration” (which is lost after a reboot) and “permanent configuration” (which is retained after a reboot). The paths for permanent configuration files differ across various system versions (such as CentOS, Ubuntu).
1. Temporary Configuration (suitable for testing scenarios)
Configured directly using the ip or ifconfig commands, and will be lost after restarting the network service or server.
-
View current IP: ip addr show (or simplified to ip a), which displays the IP information of all network interfaces.
-
Configure temporary IP (taking the network interface eth0 as an example):
- Assign IPv4 to eth0: sudo ip addr add 192.168.1.100/24 dev eth0 (/24 indicates a subnet mask of 255.255.255.0).
- Enable the network interface: sudo ip link set eth0 up.
- Delete temporary IP: sudo ip addr del 192.168.1.100/24 dev eth0.
2. Permanent Configuration (suitable for production environments)
Requires modification of system configuration files, with different paths for different distributions. Below are the configuration methods for two mainstream systems:
| System Distribution | Configuration File Path | Core Configuration Items (taking IPv4 as an example) |
|---|---|---|
| CentOS 7/8 | /etc/sysconfig/network-scripts/ifcfg-eth0 | BOOTPROTO=static (static IP)IPADDR=192.168.1.100 (IP address)NETMASK=255.255.255.0 (subnet mask)GATEWAY=192.168.1.1 (gateway)DNS1=8.8.8.8 (DNS server) |
| Ubuntu 18+/Debian | /etc/netplan/01-network-manager-all.yaml | ethernets: eth0: addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8] |
-
Activation method after configuration:
- CentOS: sudo systemctl restart network (CentOS 7) or sudo nmcli connection reload (CentOS 8+).
- Ubuntu: sudo netplan apply.
Related Concepts of IP Addresses
03
In addition to the IP address itself, it is essential to understand the three accompanying concepts: subnet mask, gateway, and DNS; otherwise, the IP cannot function correctly.
- Subnet Mask: Used to distinguish between the “network segment” and “host segment” in an IP address, for example, /24 corresponds to 255.255.255.0, indicating that the first three segments are the network segment, and the last segment is the host segment.
- Gateway: The “exit” for devices in a local area network to access the external network, usually the IP of a router or firewall, such as 192.168.1.1.
- DNS: A server that resolves domain names (such as www.baidu.com) to IP addresses. If DNS is not configured, websites cannot be accessed via domain names. Common public DNS includes 8.8.8.8 (Google) and 114.114.114.114 (domestic).
Recommended: Programming Tutorials→Luozhu Programming Assistant | Treasure Website Recommendation: A systematic learning website for programming
- Linux Guide (One): Linux Kernel and Linux System
- Linux Guide (Two): Linux Command Line and Graphical User Interface
- Linux Guide (Seven): Directory Introduction, Directory Characteristics and Functions, Common Directories
- Linux Guide (Nine): User Directory, Home Directory
- Linux Guide (Ten): Files, Everything is a File
- Linux Guide (Thirteen): Disks
- Linux Guide (Twenty): Networking
- Linux Commands (Seven): Linux Network Communication Commands