- CentOS 7 Linux Server Cookbook(Second Edition)
- Oliver Pelz Jonathan Hobson
- 750字
- 2021-07-23 14:28:51
Building a static network connection
In this recipe, we will learn how to configure a static IP address for a new or existing CentOS server.
While a dynamically assigned IP address or DHCP reservation may be fine for most desktop and laptop users, if you are setting up a server, it is often the case that you will require a static IP address. From web pages to e-mail, databases to file sharing, a static IP address will become a permanent location from which your server will deliver a range of applications and services, and it is the intention of this recipe to show you how easily it can be achieved.
Getting ready
To complete this recipe, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice.
How to do it...
For the purpose of this recipe, you will be able to find all the relevant files in the directory, /etc/sysconfig/network-scripts/
. First, you need to find out the correct name of the network interface that you want to set as static. If you need to set more than one network interface as static, repeat this recipe for every device.
- To do this, log in as root and type the following command to get a list of all of your system's network interfaces:
ip addr list
- If you have only one network card installed, it should be very easy to find out its name; just select the one not named
lo
(which is the loopback device). If you got more than one, having a look at the IP addresses of the different devices can help you choose the right one. In our example, the device is calledenp0s3
. - Next, make a backup of the network interface configuration file (change the
enp0s3
part accordingly, if your network interface is named differently):cp /etc/sysconfig/network-scripts/ifcfg-enp0s3/etc/sysconfig/network-scripts/ifcfg-enp0s3.BAK
- When you are ready to proceed, open the following file in your favorite text editor by typing what is shown next:
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
- Now, work down the file and apply the following changes:
NM_CONTROLLED="no" BOOTPROTO=none DEFROUTE=yes PEERDNS=no PEERROUTES=yes IPV4_FAILURE_FATAL=yes
- Now, add your IP information by customizing the values of
XXX.XXX.XXX.XXX
as required:IPADDR=XXX.XXX.XXX.XXX NETMASK= XXX.XXX.XXX.XXX BROADCAST= XXX.XXX.XXX.XXX
- We must now add a default gateway. Typically, this should be the address of your router. To do this, simply add a new line at the bottom of the file, as shown next, and customize the value as required:
GATEWAY=XXX.XXX.XXX.XXX
- When ready, save and close the file before repeating this step for any remaining Ethernet devices that you want to make static. When doing this, remember to assign a different IP address to each device.
- When finished, save and close this file before restarting your network service:
systemctl restart network
How it works...
In this recipe, you have seen the process associated with changing the state of your server's IP address from a dynamic value obtained from an external DHCP provider, to that of a static value assigned by you. This IP address will now form a unique network location from which you will be able to deliver a whole host of services and applications. It is a permanent modification, and yes, you could say that the process itself was relatively straightforward.
So, what have we learned from this experience?
Having started the recipe by identifying your network interface name of choice and creating a backup of the original Ethernet configuration files, we then opened the configuration file located at /etc/sysconfig/network-scripts/ifcfg-XXX
(with XXX
being the name of your interface, for example, enp0s3
). As being static no longer requires the services of the network manager, we disabled NM_CONTROLLED
by setting the value to no
. Next, as we are in the process of moving to a static IP address, BOOTPROTO
has been set to none
, as we are no longer using DHCP. To complete our configuration changes, we then moved on to add our specific network values and set the IP address, the netmask, broadcast, and the default gateway address.
In order to assist the creation of a static IP address, the default gateway is a very important setting in as much as it allows the server to contact the wider world through a router.
When finished, we were asked to save and close the file before repeating this step for any remaining Ethernet devices. Having done this, we were then asked to restart the network service in order to complete this recipe and to enable our changes to take immediate effect.
- MongoDB for Java Developers
- Java程序設計與計算思維
- Mastering Rust
- Building Minecraft Server Modifications
- 焊接機器人系統操作、編程與維護
- Android玩家必備
- HTML5秘籍(第2版)
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Qt5 C++ GUI Programming Cookbook
- Hands-On Kubernetes on Windows
- JQuery風暴:完美用戶體驗
- Mastering Gephi Network Visualization
- C語言程序設計實踐
- React and React Native
- 高性能MVVM框架的設計與實現:San