2015-09-23 13:36:05 +0000 2015-09-23 13:36:05 +0000
6
6
Advertisement

SSH :连接到主机localhost端口22:拒绝连接。

Advertisement

在debian kali中,我试图连接ssh,得到以下错误:

SSH: connect to host localhost port 22: Connection refused

Background :

我试图在debian中连接ssh,我使用kali 2.0 sana

我已经尝试/做了什么。

`apt-get install openssh-server`

安装了openssh-server,并且是最新的

查询服务 ssh status

● ssh.service - OpenBSD Secure Shell server
   Loaded: loaded (/lib/systemd/system/ssh.service; enabled)
   Active: active (running) since Wed 2015-09-23 17:20:36 IST; 36min ago
 Main PID: 1594 (sshd)
   CGroup: /system.slice/ssh.service
           └─1594 /usr/sbin/sshd -D

重新配置 dpkg-reconfigure openssh-server,也成功了

现在我尝试连接 ssh root@localhost,它需要 root@localhost 密码,所以我做的是

vi /etc/ssh/sshd_config 并添加了拒绝 root 登录的命令。

我的sshd_config如下。

What ports, IPs and protocols we listen for Port 22
#Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0 Protocol 2
# HostKeys for protocol version 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security UsePrivilegeSeparation yes

现在再次尝试通过'ssh root@localhost´连接ssh,但我得到connect to host localhost port 22: Connection refused

我想我的iptables可能会阻止它,所以把它配置为……。

vim /root/firewall.rules
root@vignesh:~# iptables-save > /root/firewall.rules
root@vignesh:~# iptables -X
root@vignesh:~# iptables -t nat -F
root@vignesh:~# iptables -t nat -X
root@vignesh:~# iptables -t mangle -F
root@vignesh:~# iptables -t mangle -X
root@vignesh:~# iptables -P INPUT ACCEPT
root@vignesh:~# iptables -P FORWARD ACCEPT
root@vignesh:~# iptables -P OUTPUT ACCEPT
root@vignesh:~# iptables-save > /root/firewall.rules

然后我查询了iptables-save

# Generated by iptables-save v1.4.21 on Wed Sep 23 18:50:34 2015
*mangle
:PREROUTING ACCEPT [41217:4171959]
:INPUT ACCEPT [27727:3255690]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1834:219528]
:POSTROUTING ACCEPT [1835:219654]
COMMIT
# Completed on Wed Sep 23 18:50:34 2015
# Generated by iptables-save v1.4.21 on Wed Sep 23 18:50:34 2015
*nat
:PREROUTING ACCEPT [15456:1179155]
:INPUT ACCEPT [1858:255303]
:OUTPUT ACCEPT [223:14078]
:POSTROUTING ACCEPT [223:14078]
COMMIT
# Completed on Wed Sep 23 18:50:34 2015
# Generated by iptables-save v1.4.21 on Wed Sep 23 18:50:34 2015
*filter
:INPUT ACCEPT [26756:3173280]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1775:215770]
COMMIT

根据评论检查了

root@vignesh:~# netstat -an | grep 22
tcp 0 0 10.100.8.40:54036 216.58.220.46:80 ESTABLISHED
tcp 0 0 10.100.8.40:41573 216.58.220.14:80 ESTABLISHED
unix 3 [] STREAM CONNECTED 17722 @/tmp/dbus-JUNz9GwSon
unix 3 [] STREAM CONNECTED 13422    
unix 3 [] STREAM CONNECTED 17224    
unix 3 [] STREAM CONNECTED 17422    
unix 2 [] DGRAM 9222     
unix 3 [] STREAM CONNECTED 17221 /var/run/NetworkManager/private
unix 3 [] STREAM CONNECTED 17225 /var/run/NetworkManager/private
unix 3 [] STREAM CONNECTED 17229    
unix 3 [] STREAM CONNECTED 17220

现在我又尝试了ssh root@localhost 但又得到了错误。

请指导我,我在哪里丢失了部分?我如何才能把它连接起来?

Advertisement
Advertisement

答案 (5)

14
14
14
2015-10-01 05:01:43 +0000

你的netstat输出显示没有进程监听端口22,这就可以解释为什么你在尝试SSH时得到一个Connection refused

你的status信息中关于sshd守护进程的信息显示正在运行,但是没有监听端口与它相关联(或者看起来没有)。

此外,正如评论中告诉你的,你的sshd_config文件似乎不正确。你说你想禁止root登录,所以我建议为你的SSH守护进程做一个配置。

编辑/etc/ssh/sshd_config文件,并在其中加入以下内容。

Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 1024
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin no
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes
ClientAliveInterval 30
ClientAliveCountMax 99999

如果你担心安全问题,你可以限制SSH只对你想要的用户开放。例如,如果你想限制只有用户vignesh才能进行SSH,你可以添加另一个指令,比如这样。

AllowUsers vignesh

之后,只要重启sshd的服务即可。完成后,如果你运行netstat -atpn | grep 22,你应该会看到22端口在监听所有人。

3
3
3
2015-10-02 17:17:12 +0000

我不知道你是真的加了这行,还是没加注释。Port 22

如果在sshd/d_config中没有指定端口,sshd将不会监听任何端口。考虑到你在netstat的输出,这很可能是问题所在。

2
Advertisement
2
2
2015-10-02 04:53:58 +0000
Advertisement

首先确认你的root账号已经设置了密码如果root没有密码,你可以sudo passwd root然后输入root的新密码。

然后你可以用-v选项进行SSH连接,以获得详细的输出。

ssh root@localhost -v

如果仍然拒绝连接,请将ssh root@localhost -v命令的输出结果贴出来。

0
0
0
2017-04-27 10:04:59 +0000

的步骤。

假设服务器1的IP是192.x.x.1,服务器2的IP是192.x.x.2

在服务器1上。

ssh-keygen -t rsa
cd .ssh
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.x.x.2

现在是ssh server2.comssh 192.x.x.2

如果你得到一个连接错误(SSH ::),那么检查两个服务器上的/etc/hosts文件 它必须有SERVER1和SERVER2的IP,如果它不包含两个服务器的条目,那么更新两个服务器上的/etc/hosts文件,包括IP地址主机名等,然后检查你的问题是否会得到解决。

我也出现了同样的错误,通过修改两个主机文件中的IP来解决。

0
Advertisement
0
0
2017-09-14 08:51:53 +0000
Advertisement

今天我突然想到了这个问题。根据这个 链接 ,"Kali Linux没有启用SSH"

Advertisement

相关问题

6
10
3
19
4
Advertisement
Advertisement