Home  >  Article  >  Operation and Maintenance  >  Six key commands to improve Linux server security

Six key commands to improve Linux server security

王林
王林Original
2023-09-08 14:58:47976browse

Six key commands to improve Linux server security

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.

  1. 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.

  2. 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.

  3. 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
  4. 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
  5. 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
  6. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn