# 虚拟机共享宿主机 Clash

宿主机未开启 clash ，虚拟机网络相关命令回显

```plaintext
$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.15  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fd17:625c:f037:2:4acb:33f3:9480:68ce  prefixlen 64  scopeid 0x0<global>
        inet6 fd17:625c:f037:2:a00:27ff:fe2f:dab4  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::a00:27ff:fe2f:dab4  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:2f:da:b4  txqueuelen 1000  (Ethernet)
        RX packets 9  bytes 3723 (3.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30  bytes 4977 (4.8 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ ping www.baidu.com -c 5
PING www.wshifen.com (103.235.46.102) 56(84) bytes of data.
64 bytes from 103.235.46.102: icmp_seq=1 ttl=255 time=88.4 ms
64 bytes from 103.235.46.102: icmp_seq=2 ttl=255 time=91.6 ms
64 bytes from 103.235.46.102: icmp_seq=3 ttl=255 time=89.4 ms
64 bytes from 103.235.46.102: icmp_seq=4 ttl=255 time=91.7 ms
64 bytes from 103.235.46.102: icmp_seq=5 ttl=255 time=92.2 ms

--- www.wshifen.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4016ms
rtt min/avg/max/mdev = 88.357/90.649/92.151/1.500 ms
                                                                                                                             
$ ping 192.168.1.34 -c 5 
PING 192.168.1.34 (192.168.1.34) 56(84) bytes of data.
64 bytes from 192.168.1.34: icmp_seq=1 ttl=255 time=0.659 ms
64 bytes from 192.168.1.34: icmp_seq=2 ttl=255 time=0.421 ms
64 bytes from 192.168.1.34: icmp_seq=3 ttl=255 time=0.557 ms
64 bytes from 192.168.1.34: icmp_seq=4 ttl=255 time=1.36 ms
64 bytes from 192.168.1.34: icmp_seq=5 ttl=255 time=0.519 ms

--- 192.168.1.34 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4036ms
rtt min/avg/max/mdev = 0.421/0.703/1.361/0.337 ms
                                                                                                                             
$ ping www.google.com -c 5
PING www.google.com (199.59.148.96) 56(84) bytes of data.

--- www.google.com ping statistics ---
5 packets transmitted, 0 received, 100% packet loss, time 4095ms
```

可以 ping 通 baidu 和宿主机地址，无法 ping 通 google

虚拟机网络使用 NAT 模式，宿主机开启 clash

并且 clash 使用 TUN 模式【要求以管理员身份运行】，以及允许局域网连接

```plaintext
$ ping www.google.com -c 5
PING www.google.com (172.19.0.6) 56(84) bytes of data.
64 bytes from 172.19.0.6: icmp_seq=1 ttl=255 time=3.29 ms
64 bytes from 172.19.0.6: icmp_seq=2 ttl=255 time=1.53 ms
64 bytes from 172.19.0.6: icmp_seq=3 ttl=255 time=1.63 ms
64 bytes from 172.19.0.6: icmp_seq=4 ttl=255 time=1.69 ms
64 bytes from 172.19.0.6: icmp_seq=5 ttl=255 time=1.62 ms

--- www.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 1.526/1.951/3.292/0.672 ms

$ curl -i www.duckduckgo.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Wed, 21 Jan 2026 07:32:18 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: https://duckduckgo.com/
Strict-Transport-Security: max-age=31536000
Expires: Thu, 21 Jan 2027 07:32:18 GMT
Cache-Control: max-age=31536000

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

$ curl -i www.youtube.com     
HTTP/1.1 301 Moved Permanently
Content-Type: application/binary
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Wed, 21 Jan 2026 07:33:37 GMT
Location: https://www.youtube.com/
Server: ESF
Content-Length: 0
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
```

无论 ping 还是 curl 命令，都能正常回显，说明设置成功

虚拟机内浏览器访问，也正常

查到的他人解决办法还有额外步骤，就是【修改 .bashrc 文件】

添加以下内容

```plaintext
export http_proxy=http://192.168.1.34:7890
export https_proxy=https://192.168.1.34:7890
export all_proxy=socks5://192.168.1.34:7890
```

实测结果，起码我使用的虚拟机不需要额外设置
