In daily use of Linux servers, an IP address is like the “door number” of the server, determining whether other devices can find it and communicate with it. Whether it’s building a website, deploying an application, or remotely connecting, correctly setting the IP address is fundamental. This article will guide you through configuring the IP address of a Linux server in the simplest way possible, suitable for beginners with no prior network knowledge.

1. What is an IP Address?

An IP address (Internet Protocol Address) is an identifier composed of numbers, used to uniquely locate a device on a network. Most IP addresses we encounter daily are in IPv4 format, consisting of 4 sets of numbers ranging from 0 to 255, for example, 192.168.1.100.

For example: If the Internet is compared to a city’s streets, the IP address is the door number of each building. Without it, other devices (such as your computer or phone) cannot access this server.

2. Two Ways to Set IP on Linux Servers

There are two common methods to set the IP address on a Linux server, and you can choose based on your needs:

1. Dynamic IP Acquisition (DHCP)

  • Principle: The server automatically obtains IP address, subnet mask, gateway, and other information from a DHCP server (usually a router) on the network.
  • Applicable Scenarios: Temporary testing, home/small LANs, or when manual IP configuration is not desired.

2. Static IP Configuration

  • Principle: Manually specify IP address, subnet mask, gateway, and other parameters. The server will use these settings permanently each time it starts.
  • Applicable Scenarios: Production environments, long-term use servers, to avoid service failures caused by IP changes (e.g., website or database services).

3. How to Check Current IP Address?

Before configuring the IP, confirm the current IP status of the server. After logging into the server, use the following commands:

ip addr

After execution, it will display network card information. For example:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:16:3e:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.5/24 brd 192.168.1.255 scope global dynamic noprefixroute eth0
       valid_lft 86399sec preferred_lft 86399sec
  • Key Information: eth0 (or similar name like ens33) is the network card name, and the 192.168.1.5 after inet is the current IP address.

Method 2: Using ifconfig (May Require Installation on Some Systems)

If the system does not have the ip command (e.g., CentOS 6), you can try:

ifconfig

In the output, the inet addr line under eth0 or ens33 is the IP address.

4. Setting a Static IP (CentOS/RHEL Example; Ubuntu/Debian Reference Below)

Step 1: Confirm Network Card Name

First, confirm the network card name (e.g., eth0, ens33) using ip addr or ifconfig. For this example, we’ll use ens33.

Step 2: Modify Network Configuration File

Linux network configuration files are typically located in the /etc/sysconfig/network-scripts/ directory, with filenames following the format ifcfg-<network card name> (e.g., ifcfg-ens33).

  1. Open the configuration file:
   sudo vi /etc/sysconfig/network-scripts/ifcfg-ens33

(If the file does not exist, create it with permissions set to 644.)

  1. Add/Modify Key Parameters:
    The core parameters for static IP configuration are as follows. Replace existing content or add these parameters:
   TYPE=Ethernet
   BOOTPROTO=static  # Set to "static" for static IP, "dhcp" for dynamic
   DEVICE=ens33      # Network card name, must match the filename
   ONBOOT=yes        # Enable the network card automatically at system startup
   IPADDR=192.168.1.100  # Static IP address (must match the local subnet)
   NETMASK=255.255.255.0 # Subnet mask (default for common LANs)
   GATEWAY=192.168.1.1   # Gateway address (usually the router's IP, e.g., 192.168.1.1)
   DNS1=8.8.8.8        # DNS server address (Google DNS; replace with ISP DNS like 114.114.114.114 if needed)
   DNS2=8.8.4.4        # Secondary DNS (optional)
  • Note: If unsure of your subnet, check the default gateway using ip route (e.g., default via 192.168.1.1 dev ens33 indicates the gateway is 192.168.1.1, and subnet mask is typically 255.255.255.0).

Step 3: Save and Exit

In the vi editor, press Esc and then enter :wq to save the changes and exit.

Step 4: Restart Network Service

After modifying the configuration, restart the network service to apply changes:

sudo systemctl restart network

(If “command not found” error occurs, use service network restart for CentOS 6.)

Step 5: Verify Configuration

Run ip addr again to confirm the IP address has been updated to your specified static IP (e.g., 192.168.1.100).

5. Setting Dynamic IP (DHCP, for Quick Testing)

If you only need temporary testing and do not want to manually configure the IP:
1. Open the network card configuration file: sudo vi /etc/sysconfig/network-scripts/ifcfg-ens33
2. Modify the parameter: BOOTPROTO=dhcp and delete or keep other parameters as default
3. Restart the network: sudo systemctl restart network
4. Verify: ip addr will show the IP address assigned by the DHCP server

6. Ubuntu/Debian (Netplan Configuration)

Ubuntu 18.04+ and Debian 9+ use Netplan to manage network configurations, with files located in the /etc/netplan/ directory (typically named like 01-netcfg.yaml).

Example Steps:

  1. Check network card name: Use ip addr to confirm the network card name (e.g., ens33).
  2. Edit the configuration file:
   sudo vi /etc/netplan/01-netcfg.yaml
  1. Add static configuration (for ens33):
   network:
     version: 2
     renderer: networkd
     ethernets:
       ens33:
         dhcp4: no  # Set to "no" for static IP, "yes" for dynamic
         addresses: [192.168.1.100/24]  # IP address/subnet mask length
         gateway4: 192.168.1.1        # Gateway
         nameservers:
           addresses: [8.8.8.8, 8.8.4.4]  # DNS servers
  1. Apply the configuration:
   sudo netplan apply

7. Common Issues and Solutions

  1. Unable to Connect After Configuration Change?
    - Check IP Conflict: Ensure the IP is not already assigned to another device (e.g., a router). Try changing the IP (e.g., 192.168.1.101).
    - Incorrect Gateway: Ping the gateway (e.g., ping 192.168.1.1). If it fails, verify the gateway IP is correct.
    - Service Not Restarted: Check if the network service is running with systemctl status network.

  2. Network Card Name Changed?
    - If the network card name changes from eth0 to ens33 (common in Ubuntu/Debian), simply update the DEVICE parameter in the configuration file and the filename.

  3. No Internet Access?
    - Verify DNS settings (e.g., ping www.baidu.com fails if DNS is incorrect).
    - Ensure the gateway points to a router with internet access.

Summary

The core steps to configure a Linux server’s IP address are: Confirm network card name → Modify configuration file → Restart network service → Verify changes. Static IP is suitable for long-term use, while dynamic IP is for temporary testing. Remember: the IP must match the local subnet, and incorrect gateway/DNS settings will cause network failures. With practice, you’ll master this quickly!

If issues arise, use commands like ip addr, ping, or cat /etc/resolv.conf to troubleshoot and identify the root cause.

Xiaoye