1. 程式人生 > >SSH的詳細安裝步驟及登入提示:connection refused的解決辦法

SSH的詳細安裝步驟及登入提示:connection refused的解決辦法

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

請按如下步驟檢查:

1、目標主機的ssh server端程式是否安裝、服務是否啟動,是否在偵聽22埠;

檢查方法:

[email protected]:~$ 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:

[email protected]s/1    

[email protected]:~$

其中/usr/sbin/sshd為ssh clinet/server中server端的守護程序,如果上述結果中沒有sshd出現,那麼可能就是你的server端程式沒有安裝(Ubuntu 11.04 預設沒有安裝ssh server,只安裝了ssh client),或者sshd服務沒有啟動,這兩者的解決辦法請見下文詳述。
2、是否允許該使用者登入;
3、本機是否設定了iptables規則,禁止了ssh的連入/連出;

檢查方法:

[email protected]:~$sudo  iptables -L

[sudo] password for june: 

Chain INPUT (policy ACCEPT)

target    prot opt source               destination         

ACCEPT    tcp  --  anywhere             anywhere            tcp dpt:ssh 

Chain FORWARD (policy ACCEPT)

target    prot opt source              destination         

Chain OUTPUT (policy ACCEPT)

target    prot opt source              destination         

[email protected]:~$
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 [email protected]    tuns為192.168.0.100機器上的使用者名稱,需要輸入密碼。
  斷開連線:exit

完整過程如下所示:

[email protected]~ 19:57:22>

ssh [email protected]

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@   WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

IT IS POSSIBLE THAT SOMEONE IS DOINGSOMETHING NASTY!

Someone could be eavesdropping on you rightnow (man-in-the-middle attack)!

It is also possible that a host key hasjust been changed.

The fingerprint for the RSA key sent by theremote host is

7f:57:35:cf:23:86:12:cb:e5:51:7a:a3:57:71:15:71.

Please contact your system administrator.

Add correct host key in/home/june/.ssh/known_hosts to get rid of this message.

Offending RSA key in/home/june/.ssh/known_hosts:8

RSA host key for 192.168.1.101 has changedand you have requested strict checking.

Host key verification failed.

[email protected]~ 20:30:55>

[email protected]~ 20:31:36>

rm /home/june/.ssh/known_hosts

[email protected]~ 20:31:46>

ssh [email protected]

The authenticity of host '192.168.1.101(192.168.1.101)' can't be established.

ECDSA key fingerprint is50:9b:b7:15:c0:57:aa:d6:22:7c:97:40:6e:49:6e:94.

Are you sure you want to continueconnecting (yes/no)? yes

Warning: Permanently added '192.168.1.101'(ECDSA) to the list of known hosts.

[email protected]'s password:

Welcome to Ubuntu 11.04 (GNU/Linux2.6.38-8-generic i686)

 *Documentation:  https://help.ubuntu.com/

Last login: Sat Jun 25 12:38:24 2011

[email protected]:~$

轉載地址:http://hi.baidu.com/leejun_2005/item/bfc0ded296cb8ebf32db907e