linux ssh 虚拟机下CentOS7开启SSH连接
一. 没开启,连接会报错

二. 开启后,连接成功

在虚拟机(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编辑器打开
将文件中,关于监听端口、监听地址前的 # 号去除

然后开启允许远程登录

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

保存文件,退出
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,发现连不通

如果可以连得通,可以直接跳至第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

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

相关推荐
-
「PHP」MVC框架是什么?为什么要用它2025-02-25 00:25:41
-
如何用PHP写一个比较安全的API系统(实现)2025-02-25 00:19:49
-
php 解析url获取相关信息2025-02-25 00:15:37
-
mysql命令总结和PyMysql2025-02-25 00:11:35
-
MySQL特性:深入理解ICP2025-02-25 00:11:07