Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3670516
  • 博文数量: 880
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 6155
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
个人简介

To be a better coder

文章分类

全部博文(880)

文章存档

2022年(5)

2021年(60)

2020年(175)

2019年(207)

2018年(210)

2017年(142)

2016年(81)

分类: LINUX

2017-09-21 11:01:04

ssh 安装及登录提示:connection refused的解决办法


原文出处:http://hi.baidu.com/leejun_2005/blog/item/fbc27c4b20e83d3a08f7ef23.html?timeStamp=1309179713928

from:http://hi.baidu.com/tunaisen/blog/item/85e0a41805ed9fb24bedbcf3.html

如果出现ssh: connect to host XX.XX.XX.XX port 22: Connection refused

请按如下步骤检查:

1、目标主机的ssh server端程序是否安装、服务是否启动,是否在侦听22端口;

检查方法:

june@ubuntu:~$ ps-ef|grep sshd

root      2859     1  020:29?        00:00:00 /usr/sbin/sshd -D

root      2901  2859  020:31?        00:00:00 sshd:june[priv]   

june      2971  2901  020:31?        00:00:00 sshd:june@pts/1    

june@ubuntu:~$

其中/usr/sbin/sshd为ssh clinet/server中server端的守护进程,如果上述结果中没有sshd出现,那么可能就是你的server端程序没有安装(Ubuntu11.04 默认没有安装ssh server,只安装了sshclient),或者sshd服务没有启动,这两者的解决办法请见下文详述。
2、是否允许该用户登录;
3、本机是否设置了iptables规则,禁止了ssh的连入/连出;

检查方法:

june@ubuntu:~$sudo  iptables-L

[sudo] password for june: 

Chain INPUT (policy ACCEPT)

target    prot optsource               destination         

ACCEPT    tcp  --  anywhere             anywhere            tcpdpt:ssh 

 

Chain FORWARD (policy ACCEPT)

target    prot opt source              destination         

 

Chain OUTPUT (policy ACCEPT)

target    prot optsource              destination         

june@ubuntu:~$
4、查查ssh的配置文件

 ls -lrt /etc/ssh

针对第一点没有安装ssh server或者没开启sshd的用户,可以参考这篇:

Ubuntu如何开启SSH服务

SSH分客户端openssh-client和openssh-server
  如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt-get install openssh-client),如果要使本机开放SSH服务就需要安装openssh-server:sudo apt-get install openssh-server然后确认sshserver是否启动了:
ps -e |grep ssh
  如果看到sshd那说明ssh-server已经启动了。
  如果没有则可以这样启动:sudo /etc/init.d/ssh start
ssh-server配置文件位于/etc/ssh/sshd_config,在这里可以定义SSH的服务端口,默认端口是22,你可以自己定义成其他端口号,如222。
  然后重启SSH服务:
sudo /etc/init.d/ssh stop
sudo /etc/init.d/ssh start
  然后使用以下方式登陆SSH:
ssh tuns@192.168.0.100    tuns为192.168.0.100机器上的用户名,需要输入密码。
  断开连接:exit
















1、ssh -v

用ssh -v去连有问题的服务器,会有比较详细的调试信息在屏幕上输出,可以帮助判断是哪一步出了问题。

主要是看是客户端还是服务器的问题。如果是客户端的问题,应该log中有写。如果是没有什么有用信息,就可能是服务器端出问题了。

复制代码
[root@yl-web ~]# ssh -v root@10.1.101.35 OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013 debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for * debug1: Connecting to 10.1.101.35 [10.1.101.35] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type -1 debug1: identity file /root/.ssh/id_rsa-cert type -1 debug1: identity file /root/.ssh/id_dsa type -1 debug1: identity file /root/.ssh/id_dsa-cert type -1 debug1: identity file /root/.ssh/id_ecdsa type -1 debug1: identity file /root/.ssh/id_ecdsa-cert type -1 debug1: identity file /root/.ssh/id_ed25519 type -1 debug1: identity file /root/.ssh/id_ed25519-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_6.6.1 ssh_exchange_identification: read: Connection reset by peer
复制代码

2、连接服务器

现在看起来是服务器出问题了,虽然不能ssh到服务器,但一般来说主机会提供一些方法比去让你连接,比如通过物理终端连进去,具体情况具体对待了,总之就是要连接到服务器上。

3、写一个排错弯路,但也是有用的

如果有debug1: Connection refused by tcp wrapper之类的log可参考这一步。

就是说你的客户端ip可能被服务器给禁掉了,fail2ban或者其他的程序可能把你的客户端ip扔到/etc/hosts.deny中了。

通过vi /etc/hosts.allow查看

加上一行sshd: ALL。

然后重启ssh。

#service sshd restart

如果问题真的出在ip被禁,这样重启之后应该就ok了。

客户端重新ssh试一下:

复制代码
[root@yl-web ~]# ssh -v root@10.1.101.35 OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013 debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for * debug1: Connecting to 10.1.101.35 [10.1.101.35] port 22.
debug1: connect to address 10.1.101.35 port 22: Connection refused
ssh: connect to host 10.1.101.35 port 22: Connection refused
复制代码

说明我的问题不在这里。

4、两条有用的命令

在服务器上执行下面命令可以显示ssh的所有 log。

centos系统如下:

#service sshd stop #/usr/sbin/sshd -d

如果是ubuntu可能命令是:sudo service ssh stop ,sudo /usr/sbin/sshd -d。

问题出现了: /var/empty/sshd must be owned by root and not group or world-writable。

是权限的问题了,查看一下。

我的权限变成777了,而sshd这个目录

正常情况该目录的权限应该是:

[root@yl-web ~]# ll /var/empty/ total 0
drwx--x--x. 2 root root 6 May 13 03:41 sshd

 修改权限:

改好权限后重启sshd服务【service sshd restart】。客户端再ssh就ok,大功告成了。









阅读(1455) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~