NAT设置下允许主机通过SSH访问VMware CentOS

2014.01.4 No Comments

NAT方式说明:

说明:使用Vmnet8虚拟交换机,此时虚拟机可以通过主机单向网络上的其他工作站,其他工作站不能访问虚拟机。

使用NAT方式,A1,A2可以访问B,但B不可以访问A1,A2。但A,A1,A2可以互访。

前期工作

1.首先设置虚拟机NAT,并确保能够联网,此处选择自定义,并选择VMnet8(NAT),如下图所示:

 

 

2.在主机环境下通过services.msc打开服务,并开启VMware NAT Service和VMware DHCP Service之后打开Network Connetion查看IPv4是否设置为Automatic(DHCP)。

 

 

3,在虚拟机中通过火狐浏览器连接网络,可以上网。确定可以联网之后,在终端输入ifconfig查看当前虚拟机的ip地址:

 

当前环境

 

本机采用无线网络连接,ip地址为:192.168.1.29

虚拟机ip地址为:192.168.2.130

并不在同一个地址段,注意此处虚拟机的ip地址与主机不能在同一个网段,否则在一个局域网内无法ping通。

 

设置步骤

1.设置NAT连接,通过开始-VMware-Virtual Network Editor打开如下界面,子网IP只要与你的虚拟机地址在同一个网段即可

 

 

在上面的列表中,选中“VMnet8”,点击“NAT  Settings...”打开NAT设置对话框。如下图所示。

 

 

选择添加,按照如下内容添加:其中端口号22为ssh的端口号,虚拟机ip地址填写ifconfig下的地址。描述可以随便写

 

 

2.确定虚拟机中的设置是VMnet8(NAT),两者必须配套。

至此,NAT连接下,虚拟机向本机映射配置完成。

 

启动SSH

 

1.在虚拟机终端查询ssh的状态:

1
2
[root@localhost qcy]# service sshd status
openssh-daemon (pid  4083is running...

2.如果SSH没有开启可以通过如下命令开启。

1
[root@localhost qcy]# service sshd start

3.查看SSH的配置信息,确保3是开启的。

1
2
[root@localhost qcy]# chkconfig --list sshd
sshd            0:off   1:off   2:off   3:on    4:off   5:off   6:off

4.防火墙规则中必须设置允许SSH访问:

1
2
3
[root@localhost qcy]# iptables -F
[root@localhost qcy]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost qcy]# iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

第一行清除所有的规则,2.3行分别添加22号端口的读写访问

5.在主机中使用ping命令查看是否可以相互访问:

 

 

同样在虚拟机中也可以测试:

1
2
3
4
5
6
7
8
9
10
[root@localhost qcy]# ping -c 4 192.168.1.29
PING 192.168.1.29 (192.168.1.2956(84) bytes of data.
64 bytes from 192.168.1.29: icmp_seq=1 ttl=128 time=0.231 ms
64 bytes from 192.168.1.29: icmp_seq=2 ttl=128 time=0.222 ms
64 bytes from 192.168.1.29: icmp_seq=3 ttl=128 time=0.254 ms
64 bytes from 192.168.1.29: icmp_seq=4 ttl=128 time=0.336 ms
                                              
--- 192.168.1.29 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.222/0.260/0.336/0.049 ms

 

6,这样就可以通过SSH和虚拟机的系统连接了,点击Quick Connect,在弹出框中输入虚拟机的ip地址以及虚拟机中的用户名,选择密码认证方式

 

 

第一次运行会有提示,选择yes进入,然后输入密码后即可进入:

 

 

 

如果失败:

 

首先要确保虚拟机和主机的联通,即ping命令可以互通,如果出现如下错误:

 

说明问题出在服务器端,即虚拟机一方。

 

1.在虚拟机中查看错误信息:

1
[root@localhost log]# vi /var/log/secure

以下是secure文件的最后两行:

1
2
Apr 27 15:14:43 localhost sshd[6865]: pam_unix(sshd:session): session closed for user qcy
Apr 27 15:15:04 localhost sshd[6904]: refused connect from 192.168.2.1 (192.168.2.1)

说明问题在于虚拟机拒绝来自192.168.2.1的连接。

 

2.在虚拟机中查看hosts.allow文件,

1
[root@localhost etc]# vi hosts.allow

注意在最后加入sshd:192.168.0.表示本机,当然可以直接加入sshd:ALL

1
2
3
4
5
6
7
8
9
10
11
#
# hosts.allow   This file contains access rules which are used to
#               allow or deny connections to network services that
#               either use the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#
sshd:192.168.0.

 

3..在虚拟机中查看hosts.deny文件

1
[root@localhost etc]# vi /etc/hosts.deny

发现其中有一句:sshd:ALL,意为拒绝所有的的连接,可以单纯地注释掉这一行。但是如果只想指定ip地址的主机访问的话,可以按照下图中的最后一行设置,EXCEPT排除掉你用于登录的主机。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#
# hosts.deny    This file contains access rules which are used to
#               deny connections to network services that either use
#               the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
#
sshd:ALL EXCEPT 192.168.1.29/255.255.255.0 192.168.2.0/255.255.255.0

4.问题得以解决!

注:以上步骤并不是每一个都是必须的,但是为了防止错误的发生,最好都做一次!

Related Posts:

评论已关闭。