Home Operation and Maintenance Linux Operation and Maintenance Harden Linux Servers: Improve Security with Command Line Tools

Harden Linux Servers: Improve Security with Command Line Tools

Sep 08, 2023 am 10:31 AM
linux server safety Command line tools

Harden Linux Servers: Improve Security with Command Line Tools

Hardening Linux Servers: Utilizing Command Line Tools to Improve Security

Overview:
With the development of the Internet, Linux servers are becoming more and more popular . However, as the number of servers continues to grow, server security issues have become increasingly prominent. In order to ensure the security of the server, administrators need to take some measures to harden the server. In this article, we will highlight some command line tools that can help administrators improve server security.

  1. Password policy management

On a Linux server, password policy is very important. By setting appropriate password policies, you can reduce the risk of passwords being guessed and cracked. The following are some command line tools that can be used to manage password policies:

  • passwd: used to change user passwords and can force complex passwords.
  • chage: used to set password expiration time and account lock options.
  • pam_pwquality: used to set password quality requirements, such as length, complexity, etc.

Sample code:

1

2

3

4

5

# 设置密码过期时间为30天

chage -M 30 username

 

# 设置密码必须包含数字和特殊字符,并且长度不少于6个字符

pam_pwquality --retry=3 --minlen=6 --minclass=2 --enforce-for-root

Copy after login
  1. Firewall configuration

The firewall is one of the important components to protect the server from unauthorized access. The following are some commonly used command line tools that can be used to configure firewalls:

  • iptables: The most commonly used firewall tool on Linux, you can control the incoming and outgoing of data packets by setting rules.
  • ufw: A firewall configuration tool on Ubuntu Linux that can simplify the configuration of iptables.
  • firewalld: Firewall configuration tool on CentOS and Fedora, providing more advanced configuration options.

Sample code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# 配置iptables,只允许SSH和HTTP流量通过

iptables -A INPUT -p tcp --dport 22 -j ACCEPT

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

iptables -A INPUT -j DROP

 

# 使用ufw配置防火墙

ufw allow ssh

ufw allow http

ufw default deny

ufw --force enable

 

# 使用firewalld配置防火墙

firewall-cmd --zone=public --add-service=ssh --permanent

firewall-cmd --zone=public --add-service=http --permanent

firewall-cmd --reload

Copy after login
  1. SSH configuration

SSH is the preferred way for administrators to remotely manage Linux servers, so keep SSH secure Very important. The following are some command line tools that can be used to harden SSH:

  • sshd_config: The configuration file of the SSH server. You can edit this file to restrict login methods, disable empty password login, etc.
  • ssh-keygen: Used to generate key pairs to provide more secure SSH connections.
  • fail2ban: Used to prevent SSH brute force cracking and can automatically disable IP addresses based on the number of failed logins.

Sample code:

1

2

3

4

5

6

7

8

9

10

11

12

13

# 禁用空密码登录

sed -i 's/#PermitEmptyPasswords yes/PermitEmptyPasswords no/' /etc/ssh/sshd_config

 

# 限制登录用户和用户组

sed -i 's/#AllowUsers/AllowUsers/' /etc/ssh/sshd_config

sed -i 's/#AllowGroups/AllowGroups/' /etc/ssh/sshd_config

 

# 生成SSH密钥对

ssh-keygen -t rsa -b 4096

 

# 安装fail2ban

apt-get install fail2ban -y

systemctl enable fail2ban

Copy after login

Conclusion:
By using these command line tools, administrators can harden Linux servers and improve server security. However, these tools are only part of hardening the server. You also need to pay attention to other security measures, such as timely updating of system patches, monitoring logs, etc. Only by taking comprehensive security measures can your server be protected against a variety of threats.

When managing a Linux server, please always remain vigilant and respond to security issues in a timely manner to ensure the security of the server.

The above is the detailed content of Harden Linux Servers: Improve Security with Command Line Tools. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance and security of PHP5 and PHP8: comparison and improvements Performance and security of PHP5 and PHP8: comparison and improvements Jan 26, 2024 am 10:19 AM

PHP is a widely used server-side scripting language used for developing web applications. It has developed into several versions, and this article will mainly discuss the comparison between PHP5 and PHP8, with a special focus on its improvements in performance and security. First let's take a look at some features of PHP5. PHP5 was released in 2004 and introduced many new functions and features, such as object-oriented programming (OOP), exception handling, namespaces, etc. These features make PHP5 more powerful and flexible, allowing developers to

Security challenges in Golang development: How to avoid being exploited for virus creation? Security challenges in Golang development: How to avoid being exploited for virus creation? Mar 19, 2024 pm 12:39 PM

Security challenges in Golang development: How to avoid being exploited for virus creation? With the wide application of Golang in the field of programming, more and more developers choose to use Golang to develop various types of applications. However, like other programming languages, there are security challenges in Golang development. In particular, Golang's power and flexibility also make it a potential virus creation tool. This article will delve into security issues in Golang development and provide some methods to avoid G

How to handle cross-domain requests and security issues in C# development How to handle cross-domain requests and security issues in C# development Oct 08, 2023 pm 09:21 PM

How to handle cross-domain requests and security issues in C# development. In modern network application development, cross-domain requests and security issues are challenges that developers often face. In order to provide better user experience and functionality, applications often need to interact with other domains or servers. However, the browser's same-origin policy causes these cross-domain requests to be blocked, so some measures need to be taken to handle cross-domain requests. At the same time, in order to ensure data security, developers also need to consider some security issues. This article will discuss how to handle cross-domain requests in C# development

What is the relationship between memory management techniques and security in Java functions? What is the relationship between memory management techniques and security in Java functions? May 02, 2024 pm 01:06 PM

Memory management in Java involves automatic memory management, using garbage collection and reference counting to allocate, use and reclaim memory. Effective memory management is crucial for security because it prevents buffer overflows, wild pointers, and memory leaks, thereby improving the safety of your program. For example, by properly releasing objects that are no longer needed, you can avoid memory leaks, thereby improving program performance and preventing crashes.

Security and encrypted transmission implementation of WebSocket protocol Security and encrypted transmission implementation of WebSocket protocol Oct 15, 2023 am 09:16 AM

Security and Encrypted Transmission Implementation of WebSocket Protocol With the development of the Internet, network communication protocols have gradually evolved. The traditional HTTP protocol sometimes cannot meet the needs of real-time communication. As an emerging communication protocol, the WebSocket protocol has the advantages of strong real-time performance, two-way communication, and low latency. It is widely used in fields such as online chat, real-time push, and games. However, due to the characteristics of the WebSocket protocol, there may be some security issues during the communication process. Therefore, for WebSo

Does win11 need to install anti-virus software? Does win11 need to install anti-virus software? Dec 27, 2023 am 09:42 AM

Win11 comes with anti-virus software. Generally speaking, the anti-virus effect is very good and does not need to be installed. However, the only disadvantage is that the virus is uninstalled first instead of reminding you in advance whether you need it. If you accept it, you don’t need to download it. Other anti-virus software. Does win11 need to install anti-virus software? Answer: No. Generally speaking, win11 comes with anti-virus software and does not require additional installation. If you don’t like the way the anti-virus software that comes with the win11 system is handled, you can reinstall it. How to turn off the anti-virus software that comes with win11: 1. First, we enter settings and click "Privacy and Security". 2. Then click "Window Security Center". 3. Then select “Virus and threat protection”. 4. Finally, you can turn it off

How to optimize the performance and resource utilization of Linux servers How to optimize the performance and resource utilization of Linux servers Nov 07, 2023 pm 02:27 PM

How to optimize the performance and resource utilization of Linux servers requires specific code examples. Summary: Optimizing Linux server performance and resource utilization is the key to ensuring stable and efficient server operation. This article will introduce some methods to optimize Linux server performance and resource utilization, and provide specific code examples. Introduction: With the rapid development of the Internet, a large number of applications and services are deployed on Linux servers. In order to ensure the efficient and stable operation of the server, we need to optimize the performance and resource utilization of the server to achieve

Security analysis of Oracle default account password Security analysis of Oracle default account password Mar 09, 2024 pm 04:24 PM

Oracle database is a popular relational database management system. Many enterprises and organizations choose to use Oracle to store and manage their important data. In the Oracle database, there are some default accounts and passwords preset by the system, such as sys, system, etc. In daily database management and operation and maintenance work, administrators need to pay attention to the security of these default account passwords, because these accounts have higher permissions and may cause serious security problems once they are maliciously exploited. This article will cover Oracle default

See all articles