虚拟机下CentOS7开启SSH连接
自己在VMware中装了CentOS 6.3,然后主机(或者说xshell)与里面的虚拟机连不通,刚学习,一头雾水,查了半天,也不知道怎么弄。
在虚拟机(Vmware Workstation)下,安装了CentOS7,现在想通过SSH工具连接虚拟机中的CentOS7
1、 首先,要确保CentOS7安装了 openssh-server,在终端中输入 yum list installed | grep openssh-server

此处显示已经安装了 openssh-server,如果又没任何输出显示表示没有安装 openssh-server,通过输入 yum install openssh-server

来进行安装openssh-server
2、 找到了 /etc/ssh/ 目录下的sshd服务配置文件 sshd_config,用Vim编辑器打开
将文件中,关于监听端口、监听地址前的 # 号去除
(备注:博主省去了一些操作方法,我作为菜鸟,还是补充下 vim 进入文本,按"i"开始编辑,编辑好之后按"esc"退到命令模式,按":wq"保存 并退出)

然后开启允许远程登录

最后,开启使用用户名密码来作为连接验证

保存文件,退出
3、 开启 sshd 服务,输入 sudo service sshd start

检查 sshd 服务是否已经开启,输入ps -e | grep sshd

或者输入netstat -an | grep 22 检查 22 号端口是否开启监听

4、 在Vmware Workstation中,查看CentOS7的属性,发现网络连接方式是采用的 NAT 方式连接的

5、 在Vmware Workstation中,点击编辑=》虚拟网络编辑器,进入虚拟网络编辑器,查看发现 NAT 模式的连接采用的网络适配器名称为VMnet8

6、在 windows 主机中,在命令行中输入ipconfig 查看主机IP,找到 VMnet8 的连接信息,此处 ip 为192.168.30.1

7、在CentOS中,输入ifconfig查看网络连接地址,发现CentOS的网络地址为192.168.112.128

8、在CentOS中,输入ping 192.168.30.1 测试是否能连通主机,发现可以连通

9、在主机中,输入 ping 192.168.112.128,测试主机是否能连通CentOS,发现连不通
(备注:我这边也没联通,用了博主的方式也不行,因为不是同一个原因,我的问题在于本机的网络设置中,虚拟机的以太网IP被我设置成固定的了, 其实应该是由路由器分配的,这边的情况是这样...所以将以太网3中TCP/IP 4属性改为自动获取IP地址)

如果可以连得通,可以直接跳至第12 步
10、在主机,打开网络配置,选择网络适配器 VMnet8 的 TCP/IPv4 的属性,进行一下网络配置

要求子网掩码、默认网关均和CentOS一致,并将IP地址修改为 192.168.112.1,即保证主机的 IP 和 CentOS 的 IP 在同一网络区段中
11、再在主机中,输入 ping 192.168.112.128,已经可以连接得通了

12、在SSH工具(此处使用的XShell)中,新建连接,输入 CentOS 的 IP 地址、用户名、密码即可连接成功


连接成功

13、为了免去每次开启 CentOS 时,都要手动开启 sshd 服务,可以将 sshd 服务添加至自启动列表中,输入systemctl enable sshd.service
(备注:我用的是CentOS 6.3 版本老,命令是[root@localhost ~]# chkconfig sshd on 注意[ ...~]表示的是/root路径)

可以通过输入systemctl list-unit-files | grep sshd,查看是否开启了sshd 服务自启动
