当前位置:首页 > 技术分享 > 正文内容

Linux远程连接SSH的BABY级别安全设置指南

admin2年前 (2023-12-08)技术分享1189

在上节,我们学习了网卡的配置,把网络配置好之后,接下来,我们就要开始配置我们的 SSH 远程连接了,毕竟我们不可能时时可以使用桌面或远程桌面的方式做运维。因此我们要学习一下,如何使用我们工具,通过 SSH 的连接方式,连接我们的服务器,对服务器进行运维。

本节内容,要分两部分来讲解。本节内容是以 CentOS 6的配置来当实例。其它的版本,可能会出现不一样的设置(相差很小小),不过 CentOS 8变化会大一点。

一、服务器设置

1、查看是否已经安装 ssh(openssh-server),已安装会出现已经安装的ssh版本信息,如果没有,则会提示no found

[chenmz@localhost ~]$ yum list installed | grep openssh-server
openssh-server.x86_64   5.3p1-94.el6    @anaconda-CentOS-201311272149.x86_64/6.5
[chenmz@localhost ~]$

2、没有安装,使用命令在线安装。一般情况下,Linux最小安装与 WEB版本默认没有安装,其它版本几乎都会安装,有些在安装系统时,选择功能时,也会漏选的情况没有安装。

[chenmz@localhost ~]$ yum install openssh-server

3、安装完成后,开始配置 sshd_config文件(配置文件目录/etc/ssh/sshd_config),可以使用我们之前学习过的命令 vim/vi 来编辑配置文件,当然如果怕错,可以使用cp命令备份一份出来,改得不知道怎么办了,可以恢复回来!(这个很重要,也要习惯做这个动作,特别是我们对系统的文件进行修改时,都要习惯做备份,万一真的出了什么问题,又找不出来原因,可以通过恢复初始的文件来解决。)

备份命令:

[root@localhost network-scripts]# cp sshd_config sshd_config.bak     //备份一个
[root@localhost network-scripts]# cd /etc/ssh/     //进入配置文件目录
[root@localhost ssh]# ll
总用量 164
-rw-------. 1 root root 125811 11月 23 2013 moduli
-rw-r--r--. 1 root root   2047 11月 23 2013 ssh_config     //客户端配置文件(不用管)
-rw-------. 1 root root   3896 7月   2 2020 sshd_config   //服务器配置文件(要配置的文件)
-rw-------. 1 root root   3881 7月   1 2020 sshd_config~
-rw-------. 1 root root   3879 7月   1 2020 sshd_config.bak //要是怕错,可以先cp备份一份
-rw-------. 1 root root    668 7月  13 2015 ssh_host_dsa_key
-rw-r--r--. 1 root root    590 7月  13 2015 ssh_host_dsa_key.pub
-rw-------. 1 root root    963 7月  13 2015 ssh_host_key
-rw-r--r--. 1 root root    627 7月  13 2015 ssh_host_key.pub
-rw-------. 1 root root   1679 7月  13 2015 ssh_host_rsa_key
-rw-r--r--. 1 root root    382 7月  13 2015 ssh_host_rsa_key.pub
[root@localhost ssh]# vim sshd_config

配置文件本身是已经帮我们写好了全部的设置内容,只不过前面加了#备注号,我们要做的就是删除掉备注号,让设置的内容生效即可。全部的配置信息如下,而我们关注的配置行却仅仅只有几行即可,我也做了说明:

#       $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm 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:/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 change a# default value.
//以下是配置的信息开关,去掉#表示开,加上#表示关闭
Port 22  //必开,这个是连接的端口,默认22,建议修改成高位数
#AddressFamily anyListenAddress 0.0.0.0 //必开,监听地址
ListenAddress ::          //必开,监听地址
# Disable legacy (protocol version 1) support in the server for new
# installations. In future the default will change to require explicit
# activation of protocol 1Protocol 2       //必开,这个是协议2,Centos 7以上默认只有2开可以连接 
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 1024
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTHSyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2mPermitRootLogin no //允许root用户远程登陆,必开,但yes设为no,有关安全
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys
#AuthorizedKeysCommand none
#AuthorizedKeysCommandRunAs nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no //允许空密码登陆,建议关闭
PasswordAuthentication yes  //默认已开,允许使用密码验证登陆
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yesChallengeResponseAuthentication no
# Kerberos options#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
#KerberosUseKuserok yes
# GSSAPI options
#GSSAPIAuthentication 
noGSSAPIAuthentication yes //建议开启,基于GSSAPI的用户认证
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
#GSSAPIStrictAcceptorCheck yes //建议开启,退出登陆清缓存
#GSSAPIKeyExchange no
# Set this to 'yes' to enable PAM authentication, account processing, 
# and session processing. If this is enabled, PAM authentication will 
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
#UsePAM noUsePAM yes
# Accept locale-related environment variablesAcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGESAcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENTAcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGEAcceptEnv XMODIFIERS
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no#X11Forwarding noX11Forwarding yes    //建议开启转发
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
# no default banner path
#Banner none
# override default of no subsystemsSubsystem       sftp    /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
#       X11Forwarding no
#       AllowTcpForwarding no
#       ForceCommand cvs server

4、配置文件的重点说明(符合基本的安全设置,如果需要更强的安全,得再深入去学习 ssh相关配置才可以)。

主要你把下面的9项开启并设置好,那服务的SSH配置就基本已经完成。可以使用客户端来远程连接了!

Port 22  //必开,这个是连接的端口,默认22,建议修改成高位数
ListenAddress 0.0.0.0 //必开,监听地址
ListenAddress ::          //必开,监听地址
Protocol 2       //必开,这个是协议2,Centos 7以上默认只有2开可以连接 
PermitRootLogin no //允许root用户远程登陆,必开,但yes设为no,有关安全
PasswordAuthentication yes  //默认已开,允许使用密码验证登陆
GSSAPIAuthentication yes //建议开启,基于GSSAPI的用户认证
GSSAPICleanupCredentials yes //建议开启,退出清缓存
X11Forwarding yes    //建议开启转发

5、防火墙的设置

默认的情况下,防火墙是关闭的,不用设置,如果你上面的设置要是都开启了,还是连接不上,可以查看 一下防火墙的设置,特别是你设置的端口不是常用的话,更要注意这里!

使用命令给防火墙加上放行端口

iptables -A INPUT -p tcp --dport 端口号-j ACCEPT

二、工具设置

直接上个图即可


设置服务器地址:

端口号:

然后就可以下一步就是输入帐号与密码(用户如果设置,请翻查之前的文章)

登陆成功后

要使用root权限怎么办呢?

直接使用命令su来切换即可!是不是很方便呢?

su - root


扫描二维码推送至手机访问。

版权声明:本文由信安苑发布,如需转载请注明出处。

本文链接:http://www.cmznet.cn/?id=15

分享给朋友:

“Linux远程连接SSH的BABY级别安全设置指南” 的相关文章

Linux系统的计划任务操作指南

Linux系统的计划任务操作指南

写完这一章,Linux的学习笔记整理就先告一段落了。因为日常使用的命令几乎都整理完了,当然Linux的命令不可能只有这些内容,但常用的基本都有了。如果熟悉了这几个章节的笔记,可以说对 Linux 算是个入门汉了。后面的更深度的学习,我也还在学习当中,知识有限,估计做不到给大家什么意见了。cron 任...

SQL Server 备份操作指南

SQL Server 备份操作指南

一、概述SQLSERVER有两个命令用于创建备份,分别是:BACKUP DATABASEBACKUP LOG这些命令具有可用于创建完整、差异、文件、事务日志备份等的各种选项,以及用于指定备份命令应如何运行以及与备份如何存储的其他选项。二、BACKUP DATABASEBACKUP DATABASE命...

企业内的最实用的工作方法

企业内的最实用的工作方法

在职场中,判断一个人工作能力的强弱,可以从六个方面来看:1)接到任务:绝不盲目接受工作2)遇到困难:自带方案请示工作3)工作动力:自我驱动,主动干活4)工作过程:擅用工具,事半功倍5)时间分配:聪明的时间管理大师6)向上关系:擅长经营领导(1)接到任务:绝不盲目接受工作职场是一个注重效率至上的地方。...

这是我看到过最好的工作方法与思路!学习一下吧

这是我看到过最好的工作方法与思路!学习一下吧

作者:谢春霖来源:知乎著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。☆ 我先来举一个栗子 :假设,你是一家创业公司的CEO。最近,你发现公司的业绩,一路下滑,你打算找业务部主管王小锤聊一下,看看到底发生了什么,下一步该如何应对。于是,你把他叫到了办公室,一脸严肃的问到:小锤,最...

Oracle数据库提示密码过期的处理方法

Oracle数据库提示密码过期的处理方法

密码过期会提示:java.sql.SQLException: ORA-28001: the password has expired 一、查看数据库密码规划默认值SQL> select * from db...

如何在 ESXI 8中开启CIM(存储监控)服务

如何在 ESXI 8中开启CIM(存储监控)服务

To be able to monitor the underlying hardware of an ESXi server, the most common method is to use the integrated CIM Server. The CIM Server reads the...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。