CentOS 7 Server Deployment Cookbook
上QQ阅读APP看书,第一时间看更新

Setting a static IP address

This recipe shows you how to configure a static IP address. Unless you configured a static address during installation, CentOS uses the Dynamic Host Configuration Protocol (DHCP) to obtain an IP address to communicate across the network. Using a dynamically assigned address is fine for most desktop and laptop systems, but those that host e-mail servers, file sharing and print services, and web servers should have an address that doesn't change. The static address provides a stable, known location on the network where users can access a system's services.

Getting ready

This recipe requires a CentOS system with a working network connection and administrative privileges provided by logging in with the root account. It assumes that your primary Ethernet device is named enp0s3 and is currently configured with DHCP. If your device is named differently, substitute its name appropriately in the following commands.

How to do it...

Follow these steps to configure a static IP address:

  1. Open the Ethernet device's configuration file, found under /etc/ sysconfig/network-scripts,with your text editor:
    vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
    
  2. Change the value of BOOTPROTO to none:
    BOOTPROTO="none"
    
  3. At the end of the file, add the IPADDR, NETMASK, and BROADCAST entries to set the desired IP address. Assign them values that properly reflect your network:
    IPADDR="192.168.56.100"
    NETMASK="255.255.255.0"
    BROADCAST="192.168.56.255"
    

    The interface is configured with a static IP address

  4. Save your changes and close the file.
  5. Open the /etc/sysconfig/network file using your editor:
    vi /etc/sysconfig/network
    
  6. Add a GATEWAY entry to identify your network's gateway:
    GATEWAY="192.168.56.1"
    
  7. Save your changes and close the file.
  8. Restart the networking service for the configuration changes to take effect:
    systemctl restart network.service
    

How it works...

In this recipe, you learned how to assign a static IP address to an Ethernet device. It assumed the name of your primary Ethernet device to be enp0s3, thus ifcfg-enp0s3 would be the name of the device's configuration file. If your device is named differently (for example, eth0, eno1677, and so on) then you need to adjust the recipe's directions accordingly.

First, we changed the value for BOOTPROTO from dhcp, the protocol used to obtain an IP address dynamically, to none since we are setting the address ourselves. Then we added the IPADDR, NETMASK, and BROADCAST entries to provide the details of the static IP address. Next, we specified the network's default gateway using GATEWAY in /etc/ sysconfig/network. This allows us to route traffic beyond the local subnetwork.

After you restart the networking service, you can confirm the new address using the ip command. ip addr show will display information about the current state of your system's network devices:

ip addr show displays your system's networking information

See also

For more information on configuring network settings in CentOS, refer to the Configure IP Networking chapter in the RHEL 7 Networking Guide (https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Networking_Guide/ch-Configure_IP_Networking.html).