


Hardening Linux Servers: Using Command Line Tools to Improve Security
Harden Linux servers: Use command line tools to improve security
Overview:
In today's network environment, server security is crucial. To protect your server from malicious attacks and unauthorized access, hardening your Linux server using command line tools is a necessary step. This article will introduce some commonly used command line tools, as well as their usage methods and sample codes to help you improve the security of your server.
SSH encrypted transmission:
SSH (Secure Shell) is an encrypted network protocol used for remote login and command execution. By using SSH, malicious users can be prevented from intercepting and eavesdropping on data in transit. Here is sample code to generate and use a key pair using SSH:
-
Generate an SSH key pair:
ssh-keygen -t rsa -b 4096
Copy after login Copy the public key to Remote server:
ssh-copy-id 用户名@IP地址
Copy after loginDisable password login (optional):
sudo vi /etc/ssh/sshd_config
Copy after login
Find the line #PasswordAuthentication yes
, Change it to PasswordAuthentication no
, then save and exit.
Restart SSH service:
sudo systemctl restart sshd
Copy after login
Firewall settings:
A firewall is a network security device used to monitor and control the network data flow on. Use a firewall to limit inbound and outbound traffic on your server, providing protection against malicious attacks. The following is sample code to set firewall rules using the iptables
command:
Install iptables:
sudo apt-get install iptables
Copy after loginCreate a new firewall Rules file:
sudo touch /etc/iptables.rules sudo vi /etc/iptables.rules
Copy after login
Add the following rules in the file:
*filter # 默认策略 :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] # 允许本地回环接口的访问 -A INPUT -i lo -j ACCEPT # 允许已经建立的、相关的连接进入 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 允许SSH连接 -A INPUT -p tcp --dport 22 -j ACCEPT # 允许其他必要的端口 # -A INPUT -p tcp --dport 80 -j ACCEPT # -A INPUT -p tcp --dport 443 -j ACCEPT # 允许ICMP (Ping)请求 -A INPUT -p icmp --icmp-type echo-request -j ACCEPT # 允许限制的IP地址范围 -A INPUT -s 允许连接的IP地址/子网掩码 -j ACCEPT # 允许某个IP地址范围的访问 # -A INPUT -s 允许连接的IP地址/子网掩码 -j ACCEPT # 拒绝所有其他入站流量 -A INPUT -j DROP COMMIT
Load firewall rules and set startup:
sudo iptables-restore < /etc/iptables.rules sudo touch /etc/network/if-pre-up.d/iptables sudo chmod +x /etc/network/if-pre-up.d/iptables sudo vi /etc/network/if-pre-up.d/iptables
Copy after login
Add the following to the file:
#!/bin/sh /sbin/iptables-restore < /etc/iptables.rules
Restart the server to apply the new firewall rules:
sudo reboot
Copy after login
Log and Monitoring:
Logging and monitoring are important components of server security. By regularly reviewing server logs, unusual activity can be discovered and acted upon. The following is sample code using common logging and monitoring tools:
View system logs:
sudo tail -f /var/log/syslog
Copy after loginView authorized user logs:
sudo tail -f /var/log/auth.log
Copy after loginMonitor network connections:
sudo apt-get install nethogs sudo nethogs
Copy after loginMonitor system resource usage:
sudo apt-get install htop sudo htop
Copy after login
Summary:
By using the above command line tools and sample code, you can enhance the security of your Linux server. From generating SSH key pairs and disabling password logins to setting up firewall rules and monitoring server logs, these steps can help you protect your server from malicious attacks and unauthorized access. When operating and maintaining the server, be sure to regularly review server logs and monitor system resource usage to ensure the security and stability of the server.
The above is the detailed content of Hardening Linux Servers: Using Command Line Tools to Improve Security. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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
