场景 hyperv创建虚拟机的时候,默认用的是default switch,这个default switch生成的ipv4地址是变化的
需求 有一个不变的ipv4地址,方便ssh远程连接
同时还可以连接外网
操作 虚拟机新加网络适配器 在hyperv中添加一块内部网卡,取名为kali
在kali的虚拟机中添加一个网络适配器,使用刚刚创建的kali
主机网卡配置 default switch不做任何更改,使用默认配置,如下:
新建的内部网卡,配置为自定义的东西
虚拟机网卡配置 配置前:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ┌──(root㉿kali)-[~] └─ 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link /ether 00:15:5d:38:01:18 brd ff:ff:ff:ff:ff:ff inet 172.20.18.191/20 brd 172.20.31.255 scope global dynamic noprefixroute eth0 valid_lft 66803sec preferred_lft 66803sec inet6 fe80::ab0d:697e:3a89:841/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link /ether 00:15:5d:38:01:1a brd ff:ff:ff:ff:ff:ff inet6 fe80::dab4:f432:f0bf:3a14/64 scope link noprefixroute valid_lft forever preferred_lft forever
修改网卡配置,添加eth1相关配置,并重启网络组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ┌──(root㉿kali)-[~] └─source /etc/network/interfaces.d/* auto lo iface lo inet loopback auto eth1 iface eth1 inet static address 192.168.20.202 netmask 255.255.255.0 ┌──(root㉿kali)-[~] └─
可以看到eth1下面已经有了ip地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ┌──(root㉿kali)-[~] └─ 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link /ether 00:15:5d:38:01:18 brd ff:ff:ff:ff:ff:ff inet 172.20.18.191/20 brd 172.20.31.255 scope global dynamic noprefixroute eth0 valid_lft 66549sec preferred_lft 66549sec inet6 fe80::ab0d:697e:3a89:841/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link /ether 00:15:5d:38:01:1a brd ff:ff:ff:ff:ff:ff inet 192.168.20.202/24 brd 192.168.20.255 scope global eth1 valid_lft forever preferred_lft forever
看下路由表,此时默认路由是从eth0网卡走的 ping 百度也是可以ping通的 说明网络没有问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ┌──(root㉿kali)-[~] └─ Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.20.16.1 0.0.0.0 UG 101 0 0 eth0 172.20.16.0 0.0.0.0 255.255.240.0 U 101 0 0 eth0 192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 ┌──(root㉿kali)-[~] └─ PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data. 64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=1 ttl=51 time=6.01 ms 64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=2 ttl=51 time=5.59 ms 64 bytes from 39.156.66.14 (39.156.66.14): icmp_seq=3 ttl=51 time=5.67 ms ^C64 bytes from 39.156.66.14: icmp_seq=4 ttl=51 time=5.72 ms --- www.a.shifen.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3082ms rtt min/avg/max/mdev = 5.588/5.744/6.006/0.157 ms ┌──(root㉿kali)-[~] └─
重启虚拟机后,添加的静态网卡ip地址是不变的,同样可以根据这个ip地址进行远程连接,就不需要每次都换ip了~
问题说明 如果路由表里面有两条默认路由,此时ping百度ping不通 可以通过修改网卡配置,删除gateway相关内容, 并重启网络来解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ┌──(kali㉿kali)-[~] └─$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.20.1 0.0.0.0 UG 0 0 0 eth1 0.0.0.0 172.20.16.1 0.0.0.0 UG 101 0 0 eth0 172.20.16.0 0.0.0.0 255.255.240.0 U 101 0 0 eth0 192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1 ┌──(kali㉿kali)-[~] └─$ ping www.baidu.com PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data. From 192.168.20.202 (192.168.20.202) icmp_seq=1 Destination Host Unreachable From 192.168.20.202 (192.168.20.202) icmp_seq=2 Destination Host Unreachable From 192.168.20.202 (192.168.20.202) icmp_seq=3 Destination Host Unreachable ^C --- www.a.shifen.com ping statistics --- 4 packets transmitted, 0 received, +3 errors, 100% packet loss, time 6153ms pipe 4 ┌──(kali㉿kali)-[~] └─$