首页 > 技术文章 > 如何通过SSH工具(SecureCRT、XShell)连接Vmware虚拟机中的Linux(CentOS7)

wangqiideal 2019-03-04 15:10 原文

本文主要解决的问题:如何通过SSH工具连接到VMWare中改的Linux系统(CentOS7)

核心内容:Linux中需要安装openssh-server,并且启动了openssh-server服务。

如果没有安装,需安装;
如果没有启动,需要启动起来。


 

步骤:

1.检查CentOS中是否安装了openssh-server

在终端中输入: yum list installed | grep openssh-server

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

来进行安装openssh-server

2.切换到目录: /etc/ssh/,使用vim编辑器打开sshd_config配置文件,开启监听端口,监听地址。

主要的几句话需要放开,去掉#号:

#将文件中,关于监听端口、监听地址前的 # 号去除
Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

#然后开启允许远程登录
PermitRootLogin yes

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


以下是实际操作:可以比照参考,下面是已修改好的,可以使用ssh工具链接的的sshd_config配置文件。

打开sshd_config配置文件并编辑:

[root@sparkmaster ssh]# vi sshd_config

# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.

# If you want to change the port on a SELinux system, you have to tell
# SELinux about this change.
# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
#
Port 22
AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

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

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

#PubkeyAuthentication yes

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys

#AuthorizedPrincipalsFile none

#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no

# Kerberos options


3.按ESC键,退出编辑模式,输入 :wq 回车,保存刚才的编辑操作。

4.开启sshd 服务,输入 sudo service sshd start

或者root用户下直接输入 service sshd start

5.检查 sshd 服务是否已经开启,输入ps -e | grep sshd
如果输出:
2740(进程ID,每台机器可能不同,忽略它) 00:00:00 sshd

说明sshd服务已经启动成功。
如果linux中未安装netstat,则无法使用下面这个命令查看22端口是否开启监听


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

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


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


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

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


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

11.在主机中,输入 ping 192.168.112.128,测试主机是否能连通CentOS,发现连不通,需要执行12,13这两步。

如果可以连得通,可以直接跳至第14 步

12.在主机,打开网络配置,选择网络适配器 VMnet8 的 TCP/IPv4 的属性,进行一下网络配置

配置IP地址:
使用下面的Ip地址:
IP地址:192.168.112.1
子网掩码:255.255.255.0
默认网关:192.168.112.255

要求子网掩码、默认网关均和CentOS一致,并将IP地址修改为 192.168.112.1,即保证主机的 IP 和 CentOS 的 IP 在同一网络区段中


13.再在主机中,输入 ping 192.168.112.128,已经可以连接得通了

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

XShell 客户端 :hostname:192.168.112.128,使用SSH协议,22端口。

SecureCRT客户端:hostname:192.168.112.128,使用SSH2协议,22端口。


15.为了免去每次开启 CentOS 时,都要手动开启 sshd 服务,可以将 sshd 服务添加至自启动列表中,输入s
ystemctl enable sshd.service


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

如果输出内容包含:
sshd.service enabled
说明服务已添加到自启动列表中了。

 

参考文章:linux ssh 虚拟机下CentOS7开启SSH连接

 

推荐阅读