The Linux operating system has been in development for more than twenty-five years, and during this time it has gained great popularity among system administrators and programmers. Because of its architecture, Linux is most often used on servers and other similar platforms for hosting projects. Like any other operating system, Linux needs maintenance, tuning, and troubleshooting.
In this article, we will look at Linux administration for beginners, what a novice administrator who has just got his own server, for example, VPS on the Internet, needs to know, what to do with it, what to look for and how to avoid problems.
Linux administration
Linux administration is a very broad area and naturally we cannot cover it completely in this article. But let’s try to cover the main tasks that arise before the administrator, whether it’s a server or a home computer. Here are the main tasks we will cover:
- Remote access;
- Network diagnostics;
- System resource monitoring;
- Checking the health of services;
- View logs;
- Software installation.
We will not cover each of the topics in great detail, perhaps some of the basics of Linux administration have been covered in more detail in previous entries, then there will be a link to them. There are usually no problems with the initial server setup. You can use one of the well-known control panels, for example, the VestaSP control panel, which allows you to install and configure everything automatically, but you need to monitor the system performance and transfer files to the server.
Remote access to a Linux server
Most often, webmasters and administrators use the SSH and FTP protocols for remote access and uploading files to the server. Using SSH, you can not only transfer files, but also execute various Linux commands on the server. The FTP protocol only allows you to upload files to the server, move and rename them. In short, for example, to transfer site files from one server to another, we first create an archive using tar:
tar cvzf backup.tar.gz /folder/with/files
Note that you don’t need to pass the p option, otherwise the file permissions won’t be preserved, you will then need to set the permissions manually. When the archive is ready, we use scp copy to transfer it to the server:
scp backup.tar.gz user@ip_Server:/var/www/public_html/
Then we log in to the server and unpack the archive:
ssh user@ip_Server
$ cd /var/www/public_html/
$ tar xvzf backup.tar.gz
After that, it remains to change the owner for the unpacked data to the username of the web server:
chown -R www-data /var/ww/public_html/project/
Most of the server administration you will have to do so it is better to understand how to use ssh.
Linux Network Diagnostics
This point of administering Linux servers is more suitable for computers to which you have physical access, but can in some cases be useful on the server as well. The easiest way to check if the computer has network access is to run the ping command:
ping example.com
If the command works correctly, and you see the transmission of packets to the remote host, then everything is fine. If not, I’d like to understand why. Check if the ip address and netmask for this connection is specified:
ifconfig
Make sure the network access gateway is set correctly:
ip route
Usually, this can tell you that the network configuration is incorrect, for example, the computer is not receiving the necessary data via DHCP, or the static settings are incorrect. Also, the problem may be in the DNS. There may be a network, but the server cannot get an ip address based on the domain name, you can ping some external ip to check:
ping 8.8.8.8
If the network does not work, and it is configured correctly, then you can still try to find out on which node the connection is broken. To do this, use the traceroute command:
traceroute 8.8.8.8
All this data will help to understand what the error was and how to solve it.
System resource monitoring
It can often happen that the server starts to work very slowly, the web services take a very long time to respond to requests, and even the SSH connection is slow. Most likely, the reason for this may be an overload of processor or memory resources. If all the memory is occupied, the system will flush the data to the disk, to the swap partition, which also greatly slows down the server. To see how much memory is available, use the free command:
free -h
Naturally, if only 40-50 MB is free, then this system is very small and everything will work very slowly. The next step is to find out which process is consuming the most memory, for this you can use the htop command:
htop
In the utility, you can sort processes by CPU usage, %CPU% column, or by %MEM% memory consumption. So you can very easily understand what the problem is and who is overloading the system. For example, the Apache web server consumes too much memory, so it might be more efficient to use Nginx.
Also, in some cases, we may be interested in loading the Linux disk and which processes are overloading the hard disk. The iotop utility is used for this. Just run the utility without parameters:
iotop
Service health check
Linux system administration also includes service management. Most distributions now use systemd as their init system. Accordingly, Linux services are managed using it. To see if a service, such as the nginx web server, is running, run:
sudo systemctl status httpd
In the numerous output of the utility, you should see the message Active (running), which means that everything is fine and the service is working as it should. You may also need to restart the service:
sudo systemctl restart httpd
Or run it if it hasn’t been run before:
sudo systemctl start httpd
If the service has not started, then you can view information about this using the status command or run:
journalctl -xe
View logs
If any service or system component is not working, then the first thing to do is to look at the logs. If it doesn’t help, turn on debug mode and watch the logs. In 90% you will find the answer why nothing works in the program logs. Logs of all services and system logs are located in the /var/log/ folder. Some services create separate folders for their files, such as /var/log/nginx or /var/log/apache.
If you did not find a solution in the usual log, then you can switch the program to debug mode or turn on the display of the most detailed information. This is usually given in the program’s configuration file. It makes no sense to give specific examples, since each service is different. But let’s look at a few commands you can use:
tail -f /path/to/log/file
With this command you can view the changes at the end of the log file in real time. If the -f option is not specified, then the tail command will show the last ten lines from the log:
tail /path/to/log/file
You can also use any text editor or the cat utility to view the log file.
Software installation
Installing software is one of the most common administrative tasks. On Linux, most programs can be installed from official or third-party repositories. Some programs need to be built from source. A package manager is used to install software from repositories. There are two main package managers that are used on servers, yum which is used by CentOS and apt which is used by Ubuntu. Package managers work in a similar way and we have covered them all in separate articles, see Installing CentOS software and Installing Ubuntu software. For example, to install a package on Ubuntu use the following command:
sudo apt install package_name
And on CentOS/RedHat:
sudo yum install package_name
To remove a program, use the remove command instead of install. But more importantly for servers updating programs. Never turn off automatic updates, and try to keep your system up to date. All software products should be kept up to date as new vulnerabilities are constantly being discovered in them and patches for them should be obtained in time.
Findings
In this article, we have covered in general terms Linux administration for beginners, this is a very broad topic, so it is difficult to cover it in one article. We covered network diagnostics, installing packages, viewing logs, and other basic steps. If you have any questions, ask in the comments!