search
HomeSystem TutorialLINUX8 Ways to Secure SSH Server Connections on Linux
8 Ways to Secure SSH Server Connections on LinuxFeb 15, 2024 pm 03:50 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux 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

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

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

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

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

5. 使用 SSH 版本 2

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

Include /etc/ssh/sshd_config.d/*.conf Protocol 2

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

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

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

X11Forwarding 
no AllowTcpForwarding no

7. 使用 SSH 密钥连接

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

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

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

ssh-keygen

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
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)