Table of Contents
Disable root user login: " >Disable root user login:
2. Change the default port" >2. Change the default port
3. Prohibit access to users with blank passwords" >3. Prohibit access to users with blank passwords
4. Limit login/access attempts" >4. Limit login/access attempts
5. 使用 SSH 版本 2" >5. 使用 SSH 版本 2
6.关闭TCP端口转发和X11转发" >6.关闭TCP端口转发和X11转发
7. 使用 SSH 密钥连接" >7. 使用 SSH 密钥连接
8. SSH 连接的 IP 限制" >8. SSH 连接的 IP 限制
Linux 服务器安全的重要性" >Linux 服务器安全的重要性
Home System Tutorial LINUX 8 Ways to Secure SSH Server Connections on Linux

8 Ways to Secure SSH Server Connections on Linux

Feb 15, 2024 pm 03:50 PM
linux linux tutorial linux system linux command shell script embeddedlinux Getting started with linux linux learning

SSH is a widely used protocol for secure remote access to Linux servers. Most users use SSH connections with default settings to connect to remote servers. However, the default configuration presents security risks and requires caution.

In order to protect servers with open SSH access, especially when using public IP addresses, disabling root account logins is necessary. Cracking the root password will become easier, so we need to strengthen SSH security.

Here’s how to secure your SSH server connection on Linux:

Disable root user login:

In order to achieve this goal, you first need to disable SSH access for the root user and create a new user with root privileges. Turning off server access for the root user is a defensive strategy that prevents attackers from breaking into your system. For example, you can create a user named "exampleroot" as follows:

useradd -m exampleroot
passwd exampleroot
usermod -aG sudo exampleroot
Copy after login

The following is a brief description of the above commands:

  • useradd Create a new user, and the **-m parameter creates a folder in the home** directory of the user you created.
  • The passwd command is used to assign a password to a new user. Remember, the passwords you assign to your users should be complex and difficult to guess.
  • usermod -aG sudoAdd the newly created user to the Administrators group.

After the user creation process, some changes need to be made to the sshd_config file. You can find this file in /etc/ssh/sshd_config. Open the file using any text editor and make the following changes to it:

# Authentication: #LoginGraceTime 2m PermitRootLogin no 
AllowUsers exampleroot
Copy after login
The 在 Linux 上保护 SSH 服务器连接的 8 种方法

PermitRootLogin line will prevent the root user from gaining remote access using SSH. Including exampleroot in the AllowUsers list will grant the necessary permissions to the user.

Finally, restart the SSH service using the following command:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                              
⚡ sudo systemctl restart ssh
Copy after login

If it fails and you receive an error message, try the following commands. This may vary depending on the Linux distribution you are using.

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com 
sudo systemctl restart sshd
Copy after login

2. Change the default port

The default SSH connection port is 22. Of course, all attackers know this and therefore need to change the default port number to ensure SSH security. Although an attacker can easily find new port numbers through an Nmap scan, the goal here is to make the attacker's job more difficult.

To change the port number, open **/etc/ssh/sshd_config** and make the following changes to the file:

Include /etc/ssh/sshd_config.d/*.confPort 22099
Copy after login
在 Linux 上保护 SSH 服务器连接的 8 种方法

After this step, use sudo systemctl restart ssh to restart the SSH service again. Now you can access your server using the port you just defined. If you are using a firewall, you must also make the necessary rule changes here. When running the netstat -tlpn command, you can see that your SSH port number has changed.

3. Prohibit access to users with blank passwords

There may be users on your system that you accidentally created without a password. To prevent such users from accessing the server, you can set the PermitEmptyPasswords line value in the sshd_config file to no.

PermitEmptyPasswords no
Copy after login

4. Limit login/access attempts

By default, you can try entering your password as many times as necessary to access the server. However, an attacker could exploit this vulnerability to brute force the server. You can automatically terminate an SSH connection after a certain number of attempts by specifying the number of allowed password attempts.

To do this, change the MaxAuthTries value in the sshd_config file.

MaxAuthTries 3
Copy after login

5. 使用 SSH 版本 2

SSH 的第二个版本发布是因为第一个版本中存在许多漏洞。默认情况下,您可以通过将Protocol参数添加到sshd_config文件来启用服务器使用第二个版本。这样,您未来的所有连接都将使用第二个版本的 SSH。

Include /etc/ssh/sshd_config.d/*.conf Protocol 2
Copy after login

在 Linux 上保护 SSH 服务器连接的 8 种方法

6.关闭TCP端口转发和X11转发

攻击者可以尝试通过 SSH 连接的端口转发来访问您的其他系统。为了防止这种情况,您可以在sshd_config文件中关闭AllowTcpForwardingX11Forwarding功能。

X11Forwarding 
no AllowTcpForwarding no
Copy after login

7. 使用 SSH 密钥连接

连接到服务器的最安全方法之一是使用 SSH 密钥。使用 SSH 密钥时,无需密码即可访问服务器。另外,您可以通过更改sshd_config文件中与密码相关的参数来完全关闭对服务器的密码访问。

创建 SSH 密钥时,有两个密钥:PublicPrivate。公钥将上传到您要连接的服务器,而私钥则存储在您将用来建立连接的计算机上。

在您的计算机上使用ssh-keygen命令创建 SSH 密钥。不要将密码短语字段留空并记住您在此处输入的密码。如果将其留空,您将只能使用 SSH 密钥文件访问它。但是,如果您设置了密码,则可以防止拥有密钥文件的攻击者访问它。例如,您可以使用以下命令创建 SSH 密钥:

ssh-keygen
Copy after login

8. SSH 连接的 IP 限制

大多数情况下,防火墙使用自己的标准框架阻止访问,旨在保护服务器。但是,这并不总是足够的,您需要增加这种安全潜力。

为此,请打开**/etc/hosts.allow**文件。通过对该文件进行的添加,您可以限制 SSH 权限,允许特定 IP 块,或输入单个 IP 并使用拒绝命令阻止所有剩余的 IP 地址。

下面您将看到一些示例设置。完成这些之后,像往常一样重新启动 SSH 服务以保存更改。

在 Linux 上保护 SSH 服务器连接的 8 种方法

Linux 服务器安全的重要性

所有服务器管理员都应该考虑数据和数据安全问题。服务器安全是一个非常敏感的问题,因为攻击的主要焦点是 Web 服务器,它们几乎包含有关系统的所有信息。由于大多数服务器都在 Linux 基础架构上运行,因此熟悉 Linux 系统和服务器管理非常重要。

SSH 安全只是保护服务器的方法之一。可以通过停止、阻挡或减缓攻击来最大程度地减少您受到的伤害。除了提供 SSH 安全性之外,您还可以实施许多不同的方法来保护您的 Linux 服务器。

The above is the detailed content of 8 Ways to Secure SSH Server Connections on Linux. 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)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles