Six key commands to improve the security of Linux servers
In the context of the current increasing awareness of information security, protecting the security of the server has become particularly important. As an open source operating system, Linux is widely used in server environments. In order to improve the security of Linux servers, we can adopt some key commands and strategies. This article will introduce six key commands and give corresponding code examples.
Update Packages
Keeping server packages up to date is one of the key steps to ensure security. We can update the packages on the server by running the following commands:
sudo apt update sudo apt upgrade
The first command is used to update the list of packages, and the second command upgrades the packages included in the list to the latest version . Please note that sometimes the upgrade process may take some time.
Configuring Firewall
A firewall is an important tool for protecting your server from malicious network activity. Before configuring the firewall, we need to install a tool called ufw
.
sudo apt install ufw
After the installation is complete, we can use the following commands to enable the firewall and configure the rules:
sudo ufw enable sudo ufw allow ssh sudo ufw allow http sudo ufw allow https
The first command enables the firewall, and the second to fourth commands allow SSH, HTTP and HTTPS traffic to pass through the firewall . You can also configure additional rules as needed.
Disable unnecessary services
Every service running on the server can become a potential attack target. Therefore, we should only enable necessary services and disable unnecessary services. The following is an example of a command to disable a service:
sudo systemctl disable <service-name>
For example, to disable the Apache server, we can execute the following command:
sudo systemctl disable apache2
Use a strong password and key
To prevent malicious users from guessing passwords and preventing passwords from being brute force cracked, we should set up a strong password policy. The following is an example of a command to set a password policy and use a key:
sudo nano /etc/ssh/sshd_config
Find the following line in the open file and modify it appropriately:
# PasswordAuthentication yes # PubkeyAuthentication yes
Replace PasswordAuthentication
Change the value to no
and change the value of PubkeyAuthentication
to yes
. Save the file and exit. Next, restart the SSH service:
sudo service ssh restart
Set login restrictions
In order to limit the number of attempts to log in to the server, we can configure login restrictions. The following is an example command to set login restrictions:
sudo nano /etc/ssh/sshd_config
Find the following line in the open file and modify it appropriately:
# MaxAuthTries 6 # MaxSessions 10
Change the value of MaxAuthTries
to 3
, change the value of MaxSessions
to 5
. Save the file and exit. Next, restart the SSH service:
sudo service ssh restart
Monitoring log files
Monitoring the server's log files can help us discover and respond to potential security threats in a timely manner. The following is an example of a command to monitor log files:
sudo tail -f /var/log/auth.log
This command will display the contents of the /var/log/auth.log
file in real time, which contains log information related to user authentication.
By running the above six key commands, we can effectively improve the security of the Linux server. However, security issues are an ever-changing area, and we should pay close attention to new security policies and technologies. I hope this article can become a reference for you and help you strengthen the security of your Linux server.
The above is the detailed content of Six key commands to improve Linux server security. For more information, please follow other related articles on the PHP Chinese website!