VMware安装Centos7并使用XShell连接(下)

平时工作中,大部分都是通过客户端(比如XShell)连接开发、测试或者线上环境的服务器进行操作Linux,进行一些应用的部署或者一些调试日志操作。上篇安装好了Centos7之后,这篇来用XShell来进行连接操作

1.配置VMware虚拟网络

①处和②处的子网ip和子网掩码是VMware根据本地网络生成的,基本不用修改,保持网段一致就行了,即192.168.XXX.0后面的0必须是和255.255.255.00保持一致,需要记住的是NAT设置中的网关地址,待会需要在虚拟机的网卡中设置

配置改好之后,直接开启虚拟机

2.配置Centos的网卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[kevina@localhost ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:66:93:b8 brd ff:ff:ff:ff:ff:ff
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:14:73:24 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:14:73:24 brd ff:ff:ff:ff:ff:ff

使用ip addr查看网卡地址,发现虚拟机的网卡ens33是没有ip的,所以要手动设置一个静态ip,防止DHCP(局域网协议)随机分配ip

先尝试修改ip,将虚拟机的ip改到和本地宿主机一个网段

1
2
3
[kevina@localhost ~]$ su root
Password:
[root@localhost kevina]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

初始用户是没有权限修改文件的,需要切换到root用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static #将dhcp改为static静态ip
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=6ee13b06-261d-4fdd-ab10-52c339eabe2b
DEVICE=ens33
ONBOOT=yes #设置网卡启动方式为 开机启动
IPADDR=192.168.31.101 #静态ip
GATEWAY=192.168.31.2 #刚才NAT配置中的网关
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114 #DNS网络解析

保存退出(:wq)之后,重启一下网卡(service network restart),再查看ip(ip addr)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost kevina]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@localhost kevina]# service network restart
Restarting network (via systemctl): [ OK ]
[root@localhost kevina]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:66:93:b8 brd ff:ff:ff:ff:ff:ff
inet 192.168.31.101/24 brd 192.168.31.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::25cd:1259:e863:ac95/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:14:73:24 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:14:73:24 brd ff:ff:ff:ff:ff:ff
[root@localhost kevina]#

尝试用XShell连接192.168.31.101试试

1
2
3
4
5
6
7
8
9
10
11
Xshell 7 (Build 0049)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.

Type `help' to learn how to use Xshell prompt.
[C:\~]$

Connecting to 192.168.122.101:22...
Could not connect to '192.168.122.101' (port 22): Connection failed.

Type `help' to learn how to use Xshell prompt.
[C:\~]$

发现连不上,我们查看一下防火墙状态

1
2
[root@localhost kevina]# firewall-cmd --state
running

防火墙是运行状态的,再查看下开放连接22端口的ssh服务状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost kevina]# systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-12-19 04:35:11 CST; 6h left
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 1130 (sshd)
Tasks: 1
CGroup: /system.slice/sshd.service
└─1130 /usr/sbin/sshd -D

Dec 19 04:35:11 localhost.localdomain systemd[1]: Starting OpenSSH server daemon...
Dec 19 04:35:11 localhost.localdomain sshd[1130]: Server listening on 0.0.0.0 por....
Dec 19 04:35:11 localhost.localdomain sshd[1130]: Server listening on :: port 22.
Dec 19 04:35:11 localhost.localdomain systemd[1]: Started OpenSSH server daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost kevina]#

sshd服务也是正常的,最后再配置一下本地VMnet8的网络ip映射

注意③处的ip地址要和本地的宿主机的ip段要一致,才能映射到虚拟机上,网关也是VMware上默认生成的网关

然后删掉之前的连接,重新创建一个新的连接就可以了,点击接受并保存,输入用户名密码就可以连上了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Xshell 7 (Build 0049)
Copyright (c) 2020 NetSarang Computer, Inc. All rights reserved.

Type `help' to learn how to use Xshell prompt.
[C:\~]$

Connecting to 192.168.31.101:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last failed login: Sat Dec 19 04:39:22 CST 2020 on pts/0
There was 1 failed login attempt since the last successful login.
Last login: Fri Dec 18 21:46:20 2020 from 192.168.31.200
[root@localhost ~]#

成功的连上了虚拟机,以后直接开启虚拟就就可以后台运行了,直接在XShell客户端操作就行了