如何通過SSH工具(SecureCRT、XShell)連接Vmware虛擬機中的Linux(CentOS7)


本文主要解決的問題:如何通過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連接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM