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

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

admin1年前 (2023-12-08)技术分享1080

在上节,我们学习了网卡的配置,把网络配置好之后,接下来,我们就要开始配置我们的 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级别安全设置指南” 的相关文章

旧电脑升级固态硬盘后卡顿的解决方法与思路

旧电脑升级固态硬盘后卡顿的解决方法与思路

方案一:SSD的常见故障有卡顿和掉盘两大类。根据发生故障的情形不同又可分为不同原因,今天麦田一棵葱给大家带来SSD的卡顿故障处理教程,敬请收藏以备不时之需。    电脑用着卡有很多种原因,并不一定是SSD造成的。比如游戏中感觉卡最可能是显卡配置较低或游戏特效设定过高导致...

Windows 11 无法加载.NET 3.5应用程序的安装及使用时的解决方法

Windows 11 无法加载.NET 3.5应用程序的安装及使用时的解决方法

最近因公司部分电脑升级至 Windows 11之后,重新安装某些需要加载.net3.5组件的应用软件时,都提示无法完成加载或安装.net 3.5而导致无法完成安装。使用离线安装包亦一样无法完成安装。一直提示如下错误:无法安装以下功能:.NET Framework 3.5 (包括.NET 2.0和3....

Linux操作指南

Linux操作指南

    Linux简介Linux是一种自由和开放源码的操作系统,存在着许多不同的Linux版本,但它们都使用了Linux内核。Linux可安装在各种计算机硬件设备中,比如手机、平板电脑、路由器、台式计算机。    【Linux起源】Linu...

企业信息化建设的四个步骤

企业信息化建设的四个步骤

   对于中小企业的IT经理来说,其是信息化建设的牵头者,压力很大。尤其是当公司正处于业务模式转型的时候,业务量激增,人工作业效率低,成本不断上升,同时又处于数字化浪潮的时代背景下,进行信息化建设几乎是唯一选择。对信息化建设,IT经理要先知而后行,行必有所为一、先知:清楚自家企业...

windows 下如何查看已连接过的无线密码

windows 下如何查看已连接过的无线密码

一、显示无线密码1、自动连接上无线网络2、打开 DOS3、使用命令 netsh wlan show profiles name=无线 SSID key=clear(明文)   ...

windows下在没有FTP的情况如何拉取Linux服务器的文件

windows下在没有FTP的情况如何拉取Linux服务器的文件

前提条件:1、windows系统安装putty2、linux系统开通了SFTP协议从windows系统拉取linux系统的备份数据。打开 CMD 命令符,在命令行里输入:c:\putty\pscp -sftp -v -r -P 端口 远程服务器用户@远程服务器地址:文件路径(绝对路径) 本地保存路径...

发表评论

访客

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