Let us set up a Static IP address on network interfaces using system network-scripts. Also, you can use Network Manager Text User Interface as a facile alternative to actually edit network interfaces files.
Step 1: Disable Unwanted System Services in CentOS
1. Make sure that our system has some necessary editing and networking tools like netstat, ifconfig, wget, curl, and lsof installed for future configurations.
# yum install nano wget curl net-tools lsof
2. Once the tools have installed run ifconfig to get your Network Interfaces settings and status, and, then run netstat or lsof command to check what services are running by default on our server.
# ifconfig
# netstat -tulpn
# lsof -i
3. The netstat command output is pretty self-explanatory and shows a list of sockets associated with their running program name.
If, for example, our system will not be used as a mail service you can stop Postfix master daemon which runs on localhost and, also stop and disable other unwanted services using the following commands – the only service I advise not to stop or disable for now is SSH if you need remote control over the server.
Stop Postfix Service
# systemctl stop postfix
# systemctl disable postfix
# systemctl status postfix
Stop Avahi Daemon Service
# systemctl stop avahi-daemon
# systemctl disable avahi-daemon
# systemctl status avahi-daemon
4. You can, also, use old init commands to stop or disable services but since Red Hat now implements systemd process and service management, you should better get used to systemctl commands and use it often.
If you use Arch Linux then it should be a piece of cake to switch to systemd – although all init commands now are linked and pass-through systemd filter.
# service postfix stop
# chkconfig postfix off
5. If you want to get a list of all started services run the service command and for an exhaustive report use systemctl.
# service --status-all
# systemctl list-unit-files
6. To manage services run the systemctl command using the most important switches: start, stop, restart, reload, disable, enable, show, list-dependencies, is-enabled, etc. followed by your service name. Also, another important feature that the systemctl command can also run on a remote server through SSH service on a specified host using -H option and perform the same actions as locally.
Step 2: Configuring Static IP Address on CentOS
7. Before start editing Network Interface Card system files make sure that from now on and until you set static IP, you have physical or any other type of access to your server because this step requires bringing down your network interface and connections.
Although it can be done smoothly without disrupting your connectivity and activate connection after reboot. There is no way you can test it before reboot if you only have a single NIC attached. Still, I will present to you with the entire method and indicate the steps needed to be avoided in case you want to maintain your connectivity and test it afterward.
8. Now move to /etc/sysconfig/network-scripts/ path, open and choose your Network Interface you want to assign static IP for editing – to get all NICs names to use ifconfig or IP command as shown.
# ifconfig
OR
# ip addr
9. Next, use the following network template to edit the file and make sure that the ONBOOT statement is set on YES, BOOTPROTO is set to static or none and don’t change HWADDR and UUID values provided by default.
# nano /etc/sysconfig/network-scripts/ifcfg-enp0s3
Make the following changes as shown.
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=enp0s3 UUID=7546e483-16a0-499e-aaac-b37246b410a5 DEVICE=enp0s3 ONBOOT=yes IPADDR=85.42.10.6 NETMASK=255.255.255.0 GATEWAY=85.42.10.5 DNS1=85.42.10.5 DNS2=8.8.8.8 DOMAIN=rootadminz.lan
Configure IP Address in CentOS 8
10. After finishing editing the file, close it, and move to resolv.conf file if you want DNS servers enabled system-wide.
# nano /etc/resolv.conf
Here just add your DNS servers using nameserver statement.
nameserver 85.42.10.5
nameserver 8.8.8.8
11. Now Network Interface is configured with a static IP, the only thing remaining is to restart your network or reboot your system and use ifconfig or IP command to view the IP address and test configuration using ping command.
# systemctl restart NetworkManager
NOTE: After restart use the newly static IP address configured to perform remote login with SSH.
# systemctl status NetworkManager
# ifconfig
# ip addr show
Check New IP Address
Check New IP Address
Step 3: Setting Hostname in CentOS
12. To adjust system hostname system-wide, open hostname and hosts file located on /etc path and edit both the following way.
Hostname File
# nano /etc/hostname
Here you can add just the name of the system but it’s a good idea to append the .dot domain to.
server.rootadminz.lan
Hosts File
# nano /etc/hosts
Here add the same hostname as above on the 127.0.0.1 line before the localhost.localdomain statements.
127.0.0.1 server.rootadminz.lan localhost.localdomain …
Alternatively, you can set hostname using the hostnamectl command as shown.
# hostnamectl -set-hostname rootadminz.lan
13. To test if your hostname is correctly set use hostname command.
# hostname -s # For short name
# hostname -f # For FQDN mame
Step 4: Set Static IP Address on CentOS Using Nmtui Tool
14. NetworkManager Text User Interface (TUI) tool, nmtui, is an RHEL intuitive tool which provides a text interface to configure networking by controlling Network Manager, which helps to edit advanced network settings such as assign static IP addresses to Network Interfaces, activate or disable a connection, edit WI-FI connections, set your system hostname or create advanced Network interfaces like InfiniBand, bond, bridge, team or VLAN.
NetworkManager-tui is installed by default in RHEL/CentOS 7.0, but if for some reason its missing issue the following command to install it.
# yum install NetworkManager-tui
14. To start Network Manager Text User Interface run the nmtui command and use TAB or arrow keys to navigate through and press Enter to select an option. If you want to directly edit or connect a specific interface run the following options.
# nmtui edit enp0s3
# nmtui connect enp0s3