Home  >  Article  >  Operation and Maintenance  >  The command line is your defense: Protect your Linux server

The command line is your defense: Protect your Linux server

WBOY
WBOYOriginal
2023-09-08 13:46:42574browse

The command line is your defense: Protect your Linux server

The command line is your defensive weapon: protect your Linux server

With the rapid development of computer technology, Linux servers have become the first choice for many enterprises and individuals. However, along with it comes an increase in cybersecurity threats. To protect our servers from hackers and malware, we need to utilize some powerful tools and techniques. The command line is one of our defensive weapons. This article will introduce some commonly used command line tools and techniques to help you protect your Linux server.

  1. iptables: A powerful firewall tool

iptables is a very powerful firewall tool in Linux systems that can help us filter and manage network traffic. The following are some commonly used iptables commands:

  • View the current iptables rules:
iptables -L
  • Allow entry and exit of specific ports:
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
  • Ban access from a specific IP address:
iptables -A INPUT -s 192.168.1.100 -j DROP
  • Block all entry and exit from a specific port:
iptables -A INPUT -p tcp --dport 22 -j DROP
iptables -A OUTPUT -p tcp --sport 22 -j DROP
  1. fail2ban : Automatically block malicious IPs

fail2ban is an automated IP blocking tool that automatically blocks malicious IP addresses based on the number of failed login attempts. We can install and configure fail2ban by following these steps:

  • Install fail2ban:
sudo apt-get install fail2ban
  • Edit configuration file:
sudo vi /etc/fail2ban/jail.local

In Add the following content at the end of the file:

[sshd]
enabled = true
port = 22
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
  • Start the fail2ban service:
sudo systemctl start fail2ban
sudo systemctl enable fail2ban
  • Verify whether it takes effect:
sudo fail2ban-client status sshd
  1. SSH configuration: Increase security

By configuring the SSH server, we can improve the security of the server. The following are some commonly used SSH configuration examples:

  • Modify the SSH default port:

Edit the SSH configuration file:

sudo vi /etc/ssh/sshd_config

Find the following line and modify it to The port number you want (e.g. 2222):

#Port 22
Port 2222
  • Disable root remote login:

Add the following line in the SSH configuration file:

PermitRootLogin no
  • Configure SSH access restrictions:

Add the following line at the end of the SSH configuration file to only allow the specified IP address to access the SSH server:

AllowUsers user1@192.168.1.1 user2@192.168.1.2
  • Restart SSH Server:
sudo systemctl restart sshd
  1. Use Strong Passwords: Increase Account Security

Weak passwords are a common target for hackers. To protect our servers, we should use strong password policies. Here are some tips for generating and using strong passwords:

  • Use long passwords that contain uppercase and lowercase letters, numbers, and special characters.
  • Use a password management tool like KeePassXC or LastPass to generate and store complex passwords.
  • Change your password regularly and avoid reusing the same password across multiple websites and services.
  1. Update systems and applications regularly

It is important to keep systems and applications up to date as updates often include fixes for security vulnerabilities and enhancements patch. Use the following commands to update your system and applications:

  • Ubuntu/Debian systems:
sudo apt-get update
sudo apt-get upgrade
  • CentOS/RHEL systems:
sudo yum update

Summary:

Protecting your Linux server from hackers and malware is crucial. By using command line tools and techniques, we can enhance the security of our servers. Whether it's by configuring your firewall, using tools that automatically block malicious IPs, or improving your SSH configuration and using strong passwords, you can improve your server's security. Finally, regularly update systems and applications to ensure your servers always have the latest security patches.

The above is the detailed content of The command line is your defense: Protect your Linux server. 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