实验环境:
物理机 Windows 10 x64
物理网卡信息
IPv4地址:192.168.123.205
子网掩码:255.255.255.0
默认网关:192.168.123.1
虚拟机VMware Workstation14 Cent OS 6.7 x64
VMnet8信息:
IPv4地址:192.168.21.0
子网掩码:255.255.255.0
默认网关:192.168.21.2
一、修改VMware的网络设置
位置:工具栏->编辑->虚拟网络编辑器[管理员]
NAT设置->修改网关IP(G):将192.168.21.2
修改为192.168.123.2
子网IP(I):将192.168.21.0
修改为192.168.123.0
子网掩码(M):默认255.255.255.0
二、进入Linux修改网络配置
输入ip addr查看网卡信息
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@localhost Desktop] # ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link /loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1 /8 scope host lo inet6 ::1 /128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link /ether 00:0c:29:3b:64:33 brd ff:ff:ff:ff:ff:ff inet 192.168.31.31 /24 brd 192.168.31.255 scope global eth0 inet6 fe80::20c:29ff:fe3b:6433 /64 scope link valid_lft forever preferred_lft forever 3: pan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN link /ether 1a:76:3b:91:89:e6 brd ff:ff:ff:ff:ff:ff |
<BROADCAST,MULTICAST,UP,LOWER_UP> mtu所在段落前的网卡编号eth0即为之前设置的NAT网卡
调整网卡脚本,输入指令vi /etc/sysconfig/network-scripts/ifcfg-对应网卡编号
这里以eth0为例,输入vi /etc/sysconfig/network-scripts/ifcfg-eth0
,输出vi编辑模式:
1
2
3
4
5
6
7
8
|
DEVICE="eth0" BOOTPROTO="dhcp" HWADDR="00:0C:29:3B:64:33" IPV6INIT="yes" NM_CONTROLLED="yes" ONBOOT="no" TYPE="Ethernet" UUID="d2818ab1-7ee1-44bf-b8b9-5bbecc6880f0" |
静态IP模式:BOOTPROTO="static"
自启动:ONBOOT="yes"
静态IP信息:IPADDR=192.168.123.123 //选一个物理网卡的网段192.168.123.x中不冲突的IP
子网掩码:NETMASK=255.255.255.0
子网网关 GATEWAY=192.168.163.2
修改后如下后保存
1
2
3
4
5
6
7
8
9
10
11
|
DEVICE="eth0" BOOTPROTO="static" HWADDR="00:0C:29:3B:64:33" IPV6INIT="yes" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Ethernet" UUID="d2818ab1-7ee1-44bf-b8b9-5bbecc6880f0" IPADDR=192.168.123.123 NETMASK=255.255.255.0 GATEWAY=192.168.123.2 |
三、重启网卡,检查Window网络适配器中NAT网卡的状态
重启指令:service network restart
VMnet8的属性之前自动获取方式的设置已经(自动)变成如下状态
注意:IP地址确认是否和物理网卡在同一网段内,且这个IP不要和之前的192.168.123.123搞混了,默认x.x.x.1就行了
子网掩码默认255.255.255.0,默认网关没有可以手动加上:192.168.123.2
本地执行:ssh root@192.168.123.123
提示输入密码后成功进入系统
测试修改静态IP后外部网络是否可用
1
2
|
[root@localhost ~] # ping baidu.com ping : unknown host baidu.com |
修改dns配置信息sudo vim /etc/resolv.conf
,在dns配置末尾追加虚拟网卡网关地址:
1
2
|
search localdomain nameserver 192.168.123.2 |
保存后稍等会片刻再次ping
1
2
3
4
5
6
7
8
|
[root@localhost ~] # ping baidu.com PING baidu.com.localdomain (220.181.38.148) 56(84) bytes of data. 64 bytes from 220.181.38.148: icmp_seq=1 ttl=128 time =45.5 ms 64 bytes from 220.181.38.148: icmp_seq=2 ttl=128 time =44.9 ms 64 bytes from 220.181.38.148: icmp_seq=3 ttl=128 time =45.4 ms 64 bytes from 220.181.38.148: icmp_seq=4 ttl=128 time =45.1 ms 64 bytes from 220.181.38.148: icmp_seq=5 ttl=128 time =45.2 ms 64 bytes from 220.181.38.148: icmp_seq=6 ttl=128 time =45.0 ms |
如果重启网卡或者系统会让DHCP自动获取dns导致之前修改的dns配置失效,所以需要在网卡配置中vi /etc/sysconfig/network-scripts/ifcfg-eth0
追加NM_CONTROLLED="no"
[即是否允许Network Manager管理,设置为no]重启网卡,再次配置修改dns配置sudo vim /etc/resolv.conf
追加nameserver 192.168.123.2
修改完毕之后重启网卡service network restart
,此时dns配置依然有效
注:设置成功后物理机更换IP后NAT网卡网段和物理网卡网段不一致了也不影响使用
到此这篇关于通过SSH连接本地linux虚拟机的过程记录的文章就介绍到这了,更多相关SSH连接本地linux内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Robin_hc/article/details/94417056