CentOS is a free distribution based on the source code of Red Hat Enterprise Linux, in fact, it is built from these sources with almost no changes, the developers just cut out all the Red Hat branding. But unlike Red Hat, CentOS is completely free and receives regular updates a short time after they are released for Red Hat, since they are also built from source.
Very often CentOS is used as an operating system for servers. In one of the previous articles, we looked at how the installation of CentOS 7 is performed. Today we will consider setting up a CentOS 7 server after installation. We will cover all the basic settings that you will need to change to get your server up and running.
Setting up CentOS after installation
Next, we will look at all the steps that you need to complete to fully configure CentOS on a server, just select what you need and apply on your machine.
1. Setting a static IP address
The first thing to do is to set up the network. Personal computers use DHCP to obtain an IP address, and the computer will have a different address each time it starts, the server must always run at the same address, so we assign it a static IP. You also need to configure DNS and a default gateway. But first, install the net-tools utility:
yum install net-tools
First, let’s see the available network interfaces and the current IP address:
ip addr show
Now you can proceed to configure the interface through the /etc/sysconfig/network-scripts/ifcfg-enp0s3 file, for example, using the vi editor:
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
After making changes, this file will look like this:
Then it remains to restart the network to apply the new settings:
service network restart
Then, if necessary, you can change the IP address in the same way.
2. Computer name
The next thing we need to do is change the computer name. The current computer name is stored in the HOSTNAME variable:
echo $HOSTNAME
To change it, you need to edit the /etc/hostname file and replace the old hostname with the new one.
vi /etc/hostname
You can also use the hostnamectl command:
hostnamectl set-hostname "Hostname"
3. Update CentOS
After installation, it is customary to update the software to the newest version in order to install all security updates. To update the list of packages in the repositories and install new versions, run the following command:
yum update && yum upgrade
4. Install a browser
In many cases, you have to use CentOS from the command line without a GUI, so you may need a browser to find something on the Internet or check the health of sites from the command line. To install the links browser, type:
yum install links
You can find other console browsers for Linux, such as Lynx or Elinks.
5. Setting the time zone
The correct time zone setting is very important for the server. This will remove confusion in the logs and allow your applications to display the correct date and time. The timedatectl utility is used for configuration.
First get the list of timezones:
timedatectl list-timezones
Then install the one you need, for example, Europe/Kyiv:
timedatectl set-timezone Europe/Kyiv
Then check:
timedatectl
7. Locale setting
The locale determines the language and encoding that will be used on your system, for example, to enable the english language, set the value to en_US.UTF-8
localectl set-locale LANG=en_US.UTF-8
Then let’s see what happened:
localectl
Then set the keyboard layout:
localectl set-keymap us
8. Disable SELinux
The SELinux policy set is intended to control access to files on a Linux system, but if you do not intend to use them, then this feature can be disabled. To do this, run:
sed -i 's/(^SELINUX=).*/SELINUX=disabled/' /etc/selinux/config
Then restart your computer and check if the feature has indeed been disabled:
sestatus
9. Create a user
Using the system as root is unsafe, and even more unsafe to leave open access to the root account via ssh. First, create a regular user and set a password for it:
useradd Username
# passwd Password
Then add the user to the wheel group to allow the user to run as an administrator:
usermod -G wheel Username
Now it remains to tweak the sudo settings, for this add the following line, if it is not already there:
visudo
10. Enable Third Party Repositories
Adding third-party repositories to a production server is not a good idea, and in some cases can lead to bad results. However, sometimes you may need programs that are not in the official repositories. Therefore, consider how to add multiple repositories.
To add the Enterprise Linux Repository (EPEL) run:
yum install epel-release
# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
10. SSH setup
Most often, we have to work with servers not directly, but over the network, via SSH. Usually, the SSH service is already installed and activated, but there are a few settings that need to be made for it to work properly. First you need to configure the use of only a secure protocol, to do this, open the /etc/ssh/ssh_config file and remove the Protocol 2.1 line. And instead add:
You also need to disable login as superuser:
11. Install Apache Web Server
If you plan to use the machine as a web server, you will need Apache. With it, you can host websites, multimedia content, client programs, and more. To install, run:
yum install httpd
When the installation is complete, before you can proceed to work, you need to allow HTTP in the firewall:
firewall-cmd --add-service=http
# firewall-cmd -permanent -add-port=3221/tcp
# firewall-cmd --reload
Now it remains to add Apache to startup:
systemctl start httpd.service
# systemctl enable httpd.service
Then you can check if it works with links:
links 127.0.0.1
12. Install PHP
PHP is a modern web application and scripting language. It is often used as a general purpose programming language. To install, run:
yum install php
After installation, you need to restart Apache:
systemctl restart httpd.service
Next, let’s create a test file with a script to check if the installation is correct:
echo -e "<?phpnphpinfo();n?>" > /var/www/html/phpinfo.php
Then open the generated file in a browser:
links http://127.0.0.1/phpinfo.php
13. Database installation
MariaDB is a database based on MySQL source code. Red Hat based Linux distributions use MariaDB instead of MySQL. Databases are an indispensable thing on a server, so setting up CentOS after installation should include installing it. To install MariaDB, type:
yum install mariadb-server mariadb
Then run and add to startup:
systemctl start mariadb.service
# systemctl enable mariadb.service
And allow the service in the firewall:
firewall-cmd --add-service=mysql
It remains to run the configuration script:
/usr/bin/mysql_secure_installation
14. Install GCC
GCC stands for GNU Compiler Collection and is a set of compilers that are considered the standard for compiling programs on Linux. But it doesn’t ship with CentOS by default, so type:
yum install gcc
You can then look at the GCC version:
gcc -v
15. Install Java
Java is a general purpose, object-oriented programming language. It is not installed by default, so configuring CentOS 7 after installation may include installing it. To do this, run:
yum install java
Then check the version:
java
Findings
In this article, we looked at how the CentOS 7 server is configured after installation. As you can see, there are many elementary actions that are desirable to be done before using the server in production. If you have any questions, ask in the comments!