Linux学习填坑 修改ssh默认端口及禁用root账户

本文详细介绍如何通过修改SSH默认端口并禁用root账户登录来增强服务器的安全性。首先,通过编辑配置文件将默认端口22更改为其他端口;其次,通过创建新用户并调整配置文件禁止root直接登录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、修改ssh默认端口

1.1、修改ssh配置文件

打开 /etc/ssh/sshd_config 配置文件,没修改之前如下:

#       $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

删除#Port 22前面的#,在下一行添加一个新的端口号如:Port 10086 (最好大于10000 不能超过65535),防止修改端口失败无法登陆

Port 22
Port 10086

保存退出

1.2、重启ssh服务

# service sshd restart

1.3、测试修改端口ssh连接

用新添加的端口号进行测试连接

1.4、禁用22端口

        修改 /etc/ssh/sshd_config 配置文件,去掉Port 22,重启SSH服务


2、禁用root账户登录

2.1、添加用户

root添加一个名为 test 的用户:

# useradd test

为该用户设置密码:

# passwd test

然后需要输入两次密码:

Changing password for user test.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

2.2、修改ssh配置文件

        打开 /etc/ssh/sshd_config 文件,定位到此行:

#PermitRootLogin yes

去掉前面的#号,并将'yes'改为'no',保存退出

2.3、重启ssh服务

# service sshd restart

2.4、使用新用户登录

更改ssh连接属性,使用新添加的用户名及密码,以普通用户连接

2.5、使用root权限

        若某些操作权限不足,需要使用root权限可用以下命令:

# su root


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值