您的位置:首页 > 其它

在Ubuntu中ssh报错: connect to host localhost port 22: Connection refused 解决办法

2014-09-27 00:26 706 查看
本人最近在Ubuntu 14.04上搭建Hadoop 2.5伪分布集群时偶然遇见ssh: connect to host localhost port 22: Connection refused 问题,尝试多种办法,最终得以解决,下面由我细细道来~~

安装既定步骤进行完hadoop相关配置后,运行$ ./sbin/start-dfs.sh命令时终端显示如下:

➜  hadoop  ./sbin/start-dfs.sh
Starting namenodes on [localhost]
localhost: ssh: connect to host localhost port 22: Connection refused
localhost: ssh: connect to host localhost port 22: Connection refused
Starting secondary namenodes [0.0.0.0]
0.0.0.0: ssh: connect to host 0.0.0.0 port 22: Connection refused


怀疑ssh服务未启动,于是$ service sshd restart

➜  hadoop  service sshd restart
sshd: unrecognized service


居然没有sshd服务,于是查了一下,本机初始状态安装的是openssh, 也不存在/etc/init.d/ssh这玩意儿,不能忍。于是怀疑本机22端口未开启,于是:

➜  hadoop  sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
➜  hadoop  ssh localhost
ssh: connect to host localhost port 22: Connection refused


还是不行!果断Google了一番(嘿嘿,个人热衷Google搜索,某度不敢恭维,在天朝当然得翻墙),当然搜了一圈,很多博客或论坛上的答复基本都是ps看是否启动ssh服务,然后重启。本人ssh登录公司服务器一点问题没有,登录localhost居然报错,于是怀疑子openssh安装可能出现问题,可能是openssh-server或者openssh-client,于是一股脑全部重装试试:

➜  hadoop  sudo apt-get install openssh-client openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
openssh-client is already the newest version.
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
openssh-server : Depends: openssh-client (= 1:6.6p1-2ubuntu1)
E: Unable to correct problems, you have held broken packages.





说openssh-server依赖于openssh-client,这是神马情况??于是:

➜  hadoop  sudo apt-get install openssh-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
openssh-client is already the newest version.
The following packages were automatically installed and are no longer required:
libxcb-randr0 libxcb-render-util0 libxcb-xkb1 mysql-common-5.6
Use 'apt-get autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


于是怀疑可能是libxcb-randr0、libxcb-render-util0、 libxcb-xkb1、 mysql-common-5.6这几个东东在作怪,于是:

➜  hadoop  sudo apt-get clean
➜  hadoop  sudo apt-get autoremove
➜  hadoop  sudo apt-get update
Hit http://www.rabbitmq.com testing InRelease
Ign http://us.archive.ubuntu.com trusty InRelease
Ign http://get.docker.io docker InRelease
Ign http://ppa.launchpad.net trusty InRelease
Ign http://extras.ubuntu.com trusty InRelease
Hit http://us.archive.ubuntu.com trusty Release.gpg
Hit http://extras.ubuntu.com trusty Release.gpg
Ign http://ppa.launchpad.net trusty InRelease
Hit http://www.rabbitmq.com testing/main amd64 Packages
Hit http://us.archive.ubuntu.com trusty Release
Hit http://extras.ubuntu.com trusty Release
Ign http://ppa.launchpad.net trusty Release.gpg
......

➜  hadoop  sudo apt-get -f install
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


再次重装openssh-server:

➜  hadoop  sudo apt-get install -y openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
openssh-server : Depends: openssh-client (= 1:6.6p1-2ubuntu1)
E: Unable to correct problems, you have held broken packages.


还是木有解决,重装openssh-client:

➜  hadoop  sudo apt-get install --reinstall openssh-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
Reinstallation of openssh-client is not possible, it cannot be downloaded.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.


于是继续在网上,看到一篇博客http://blog.csdn.net/feliciafay/article/details/6561414,大概意思是openssh-server依赖openssh-client,然后openssh-client依赖openssh-server,如此循环,博主最终把问题解决了,但我个人不能接受如此复杂的解决办法。于是找了另一篇博客http://www.cnblogs.com/xiaowenhu/p/3185631.html,即在安装openssh-client时候指定其版本,于是得到了启发,再次尝试:

➜  hadoop  sudo apt-get install openssh-client=1:6.6p1-2ubuntu1
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
libpam-ssh keychain monkeysphere
The following packages will be DOWNGRADED:
openssh-client
0 upgraded, 0 newly installed, 1 downgraded, 0 to remove and 0 not upgraded.
Need to get 566 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main openssh-client amd64 1:6.6p1-2ubuntu1 [566 kB]
Fetched 566 kB in 19s (28.8 kB/s)
dpkg: warning: downgrading openssh-client from 1:6.6p1-2ubuntu2 to 1:6.6p1-2ubuntu1
(Reading database ... 195887 files and directories currently installed.)
Preparing to unpack .../openssh-client_1%3a6.6p1-2ubuntu1_amd64.deb ...
Unpacking openssh-client (1:6.6p1-2ubuntu1) over (1:6.6p1-2ubuntu2) ...
Processing triggers for man-db (2.6.7.1-1) ...
Setting up openssh-client (1:6.6p1-2ubuntu1) ...


openssh-client安装正确,至少版本正确,再次安装openssh-server:

➜  hadoop  sudo apt-get install openssh-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libck-connector0 ncurses-term openssh-sftp-server ssh-import-id
Suggested packages:
rssh molly-guard monkeysphere
The following NEW packages will be installed:
libck-connector0 ncurses-term openssh-server openssh-sftp-server
ssh-import-id
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 616 kB of archives.
After this operation, 3,420 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu/ trusty/main libck-connector0 amd64 0.4.5-3.1ubuntu2 [10.5 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu/ trusty/main ncurses-term all 5.9+20140118-1ubuntu1 [243 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu/ trusty/main openssh-sftp-server amd64 1:6.6p1-2ubuntu1 [34.1 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu/ trusty/main openssh-server amd64 1:6.6p1-2ubuntu1 [319 kB]
Get:5 http://us.archive.ubuntu.com/ubuntu/ trusty/main ssh-import-id all 3.21-0ubuntu1 [9,624 B]
Fetched 616 kB in 10s (61.1 kB/s)
Preconfiguring packages ...
Selecting previously unselected package libck-connector0:amd64.
(Reading database ... 195887 files and directories currently installed.)
Preparing to unpack ...bck-connector0_0.4.5-3.1ubuntu2_amd64.deb ...
Unpacking libck-connector0:amd64 (0.4.5-3.1ubuntu2) ...
Selecting previously unselected package ncurses-term.
Preparing to unpack .../ncurses-term_5.9+20140118-1ubuntu1_all.deb ...
Unpacking ncurses-term (5.9+20140118-1ubuntu1) ...
Selecting previously unselected package openssh-sftp-server.
Preparing to unpack .../openssh-sftp-server_1%3a6.6p1-2ubuntu1_amd64.deb ...
Unpacking openssh-sftp-server (1:6.6p1-2ubuntu1) ...
Selecting previously unselected package openssh-server.
Preparing to unpack .../openssh-server_1%3a6.6p1-2ubuntu1_amd64.deb ...
Unpacking openssh-server (1:6.6p1-2ubuntu1) ...
Selecting previously unselected package ssh-import-id.
Preparing to unpack .../ssh-import-id_3.21-0ubuntu1_all.deb ...
Unpacking ssh-import-id (3.21-0ubuntu1) ...
Processing triggers for man-db (2.6.7.1-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Processing triggers for ufw (0.34~rc-0ubuntu2) ...
Setting up libck-connector0:amd64 (0.4.5-3.1ubuntu2) ...
Setting up ncurses-term (5.9+20140118-1ubuntu1) ...
Setting up openssh-sftp-server (1:6.6p1-2ubuntu1) ...
Setting up openssh-server (1:6.6p1-2ubuntu1) ...
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
Creating SSH2 ED25519 key; this may take some time ...
ssh start/running, process 10352
Setting up ssh-import-id (3.21-0ubuntu1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.3) ...
Processing triggers for ureadahead (0.100.0-16) ...
Processing triggers for ufw (0.34~rc-0ubuntu2) ...


不再报之前的错!好像可以了诶~~

再次$ ssh localhost:

➜  hadoop  ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is 04:93:aa:b3:9c:32:bd:96:4f:c1:bf:78:19:f8:5b:e6.
Are you sure you want to continue connecting (yes)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64)

* Documentation:  https://help.ubuntu.com/ 
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.


成!功!了~

总结,其实仔细分析错误原因,直观点分析其原因就对了,而且个人感觉openssh不太好用,需要自己安装或者更新,容易走弯路。不过问题解决了是最重要的,希望各位同学不再因此走弯路咯~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐