CentOS下SSH远程连接服务慢解决方案

本文详细介绍了如何通过调整SSH配置、禁用DNS解析、优化hosts文件和使用ssh-v调试命令来提高SSH连接速度并确保安全性。同时,解决连接慢问题的步骤包括修改sshd_config配置、检查本地主机名与IP地址一致性以及利用ssh-v调试命令查找慢连接的具体原因。

1、适用命令及方案如下:
【远程连接及执行命令】
ssh -p22 root@10.0.0.19
ssh -p22 root@10.0.0.19 /sbin/ifconfig
【远程拷贝:推送及拉取】
scp -P22 -r -p /etc root@10.0.0.19:/tmp/
scp -P22 -r -p root@10.0.0.19:/tmp/ /etc
【安全的FTP功能】
sftp -oPort=22 root@10.0.0.19
【无密码验证方案】
例如利用sshkey批量分发文件,执行部署操作。

2、连接慢的主要原因是DNS解析导致
解决方法:

1、在ssh服务端上更改/etc/ssh/sshd_config文件中的配置为如下内容:
UseDNS no
# GSSAPI options
GSSAPIAuthentication no
然后,执行/etc/init.d/sshd restart重启sshd进程使上述配置生效,在连接一般就不慢了。

2、如果还慢的话,检查ssh服务端上/etc/hosts文件中,127.0.0.1对应的主机名是否和
uname -n的结果一样,或者把本机ip和hostname(uname -n结果)加入到/etc/hosts里。

[root@C64~]# uname -n
C64
[root@C64~]# cat /etc/hosts
#modi by oldboy 11:12 2013/9/24
127.0.0.1   C64 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.18   C64
################


3、利用ssh-v的调试功能查找慢的原因

其实可以用下面的命令调试为什么慢的细节(学习这个思路很重要)。

[root@C64~]# ssh -v root@10.0.0.19
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for*
debug1: Connecting to 10.0.0.19 [10.0.0.19] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH_4*
debug1: Enabling compatibility mode forprotocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
The authenticity of host '10.0.0.19 (10.0.0.19)'can't be established.
RSA key fingerprint is ca:18:42:76:0e:5a:1c:7d:ef:fc:24:75:80:11:ad:f9.
Are you sure you want to continue connecting (yes/no)? yes
=======>老男孩老师评:这里就是提示保存密钥的交互提示。
Warning: Permanently added '10.0.0.19' (RSA) to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Next authentication method: password
root@10.0.0.19's password:
=======>老男孩老师评:这里就是提示输入密码的交互提示。
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Last login: Tue Sep 24 10:30:02 2013 from 10.0.0.18
在远程连接时如果慢就可以确定卡在哪了。
[root@C64_A~]# ssh -v oldboy@10.0.0.17
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for*
debug1: Connecting to 10.0.0.17 [10.0.0.17] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_dsa type 2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode forprotocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '10.0.0.17'is known and matches the RSA host key.
debug1: Found key in/root/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic

上述配置没配就发现卡到gssapi这。就大概知道是gssapi的问题。

实际上在linux系统优化部分就应该优化SSH服务的此处。

本文出自 “老男孩linux运维” 博客,请务必保留此出处http://oldboy.blog.51cto.com/2561410/1300964

### CentOS 7 SSH远程登录配置教程 在CentOS 7中,SSH远程登录的配置可以通过编辑`/etc/ssh/sshd_config`文件来实现。以下是详细的配置说明: #### 修改SSH配置文件 1. **允许或禁止Root用户远程登录** 编辑`/etc/ssh/sshd_config`文件中的`PermitRootLogin`选项可以控制是否允许Root用户通过SSH登录。设置为`no`表示禁止Root用户直接登录[^5]。 2. **更改默认端口** 默认情况下,SSH服务监听的是22端口。为了提高安全性,可以修改该端口。找到`#Port 22`这一行,取消注释并将数值改为其他端口号(如35222)。完成后需更新防火墙规则以放行新的端口[^5]。 ```bash sudo firewall-cmd --permanent --add-port=35222/tcp sudo firewall-cmd --reload ``` 3. **启用严格主机密钥检查** 如果希望每次连接都忽略主机密钥变化警告,则可以在客户端的`~/.ssh/config`或者全局配置文件`/etc/ssh/ssh_config`中加入如下参数[^1]: ```plaintext StrictHostKeyChecking no UserKnownHostsFile /dev/null ``` 4. **重启SSH服务** 更改任何配置之后都需要重新启动SSH服务使改动生效。 ```bash sudo systemctl restart sshd ``` #### 常见问题及其解决方案 1. **无法建立SSH连接** 当尝试访问服务器却失败时,可能是因为防火墙阻止了请求或是SSHD服务本身存在问题。确认防火墙状态以及开放必要的TCP端口是非常重要的一步[^4]。 2. **密码错误多次被锁定账户** 对于频繁遭受攻击的情况,推荐采用基于公私钥的身份验证方式代替传统的用户名密码组合,并考虑部署Fail2Ban之类的工具自动封禁恶意IP地址[^5]。 3. **忘记如何生成SSH Key Pair** 使用下面这条指令创建一对RSA类型的加密钥匙: ```bash ssh-keygen -t rsa ``` 接着利用`ssh-copy-id`命令分发公共部分至目标节点上完成信任关系构建过程[^4]。 ```python import os print(os.system('ssh-keygen -t rsa')) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值