Hyper-v中CentOS-7的静态/动态网络配置及SSH连接小记
需求
- 本地win10 hyper-v中安装centos7,centos7可连接外网通过yum下载各种包
- centos7拥有静态ip,可以稳定的通过ssh工具(例如xshell)链接
思路
使用双网卡,一个负责外网,一个负责内网:
- 使用hyper-v自带默认虚拟交换机default switch的网卡自动外网连接
- 新建内部虚拟交换机与新建网卡相关联,配置静态ip,供SSH连接
步骤
- 新建类型为“内部”的虚拟交换机




2. 在“控制面板-网络连接”中设置此交换机地址,名字:ssh-connect




在进行这一步时,如果之前配置过hyper-v虚拟网卡,可能保存不上,提示:




参考这篇文章,删除对应注册表内的内容并重启即可:
networking – IP address assigned to non-existent adapter – Super User
简言之:
- 在注册表HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces中搜索报错提示的这个关联IP地址,找到这个节点的GUID:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}并删除
- 删除HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Adapters\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
- 删除HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
- 重启
3. 虚拟机关机前提下,为虚拟交换机新建网卡。(右键虚拟机-设置)




4. 第一块网卡关联default switch,第二块关联新建的虚拟交换机




5. 打开虚拟机,虚拟机内设置两块网卡的网络
配置eth0
在虚拟机中编辑/etc/sysconfig/network-scripts/ifcfg-eth0,把BOOTPROTO改为dhcp,ONBOOT改为yes:




配置eth1
拷贝ifcfg-eth0,并改名为ifcfg-eth1。修改BOOTPROTO为static,把NAME和DEVICE改为eth1,删除UUID(不能和eth0相同)。
加上IPADDR=192.168.218.2和NETMASK=255.255.255.0,IP必须与ssh-connect一个网段,不用设置GATEWAY




6. 重启网络或者reboot:service network restart,之后就可以ping通http://baidu.com和ssh-connetc地址使用xshell链接








另:
1. 如果xshell连接不上,需确保CentOS7安装了 openssh-server
yum list installed | grep openssh-server




2. 如果执行命令后无显示,则需安装openssh-server:
yum install openssh-server
3. 之后配置/etc/ssh/sshd_config




4. 开启ssh:
systemctl start sshd
5. ps -e | grep sshd 查看sshd服务是否启动




6. netstat -ntlp | grep :22 查看端口是否起开




至此,问题解决。
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/12524.html