我在 Parallels Desktop 中设置了 Debian 服务器 VM。我将其配置为使用 Bridge 网络。DHCP 已启用,但我将 VM 配置为使用静态 IP。
在我的虚拟机中的 Debian 上,Web 服务器运行(127.0.0.1:5000
)并且网络配置正确:
$ sudo cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug enp0s5
# iface enp0s5 inet dhcp
iface enp0s5 inet static
address 10.211.55.201
netmask 255.255.255.0
gateway 10.211.55.1
$ sudo ifconfig
enp0s5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.211.55.201 netmask 255.255.255.0 broadcast 10.211.55.255
inet6 fe80::21c:42ff:fedf:37b prefixlen 64 scopeid 0x20<link>
ether 00:1c:42:df:03:7b txqueuelen 1000 (Ethernet)
RX packets 564 bytes 78327 (76.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 308 bytes 49738 (48.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 15 bytes 1394 (1.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15 bytes 1394 (1.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
$ netstat -pln
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 899/python3
tcp6 0 0 :::22 :::* LISTEN -
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] STREAM LISTENING 13667 840/systemd /run/user/1000/systemd/private
unix 2 [ ACC ] STREAM LISTENING 13673 840/systemd /run/user/1000/gnupg/S.dirmngr
unix 2 [ ACC ] STREAM LISTENING 13675 840/systemd /run/user/1000/gnupg/S.gpg-agent.browser
unix 2 [ ACC ] STREAM LISTENING 13677 840/systemd /run/user/1000/gnupg/S.gpg-agent.extra
unix 2 [ ACC ] STREAM LISTENING 13679 840/systemd /run/user/1000/gnupg/S.gpg-agent.ssh
unix 2 [ ACC ] STREAM LISTENING 13681 840/systemd /run/user/1000/gnupg/S.gpg-agent
unix 2 [ ACC ] STREAM LISTENING 11990 - /run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 13009 - /run/systemd/private
unix 2 [ ACC ] STREAM LISTENING 13011 - /run/systemd/userdb/io.systemd.DynamicUser
unix 2 [ ACC ] STREAM LISTENING 13012 - /run/systemd/io.system.ManagedOOM
unix 2 [ ACC ] STREAM LISTENING 13020 - /run/lvm/lvmpolld.socket
unix 2 [ ACC ] STREAM LISTENING 13024 - /run/systemd/fsck.progress
unix 2 [ ACC ] STREAM LISTENING 13032 - /run/systemd/journal/stdout
unix 2 [ ACC ] SEQPACKET LISTENING 13034 - /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 10664 - /run/systemd/journal/io.systemd.journal
$ curl http://127.0.0.1:5000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
...
现在我尝试从主机(MacOS)访问 Web 服务器:
> ping 10.211.55.201
PING 10.211.55.201 (10.211.55.201): 56 data bytes
64 bytes from 10.211.55.201: icmp_seq=0 ttl=64 time=1.425 ms
64 bytes from 10.211.55.201: icmp_seq=1 ttl=64 time=0.317 ms
64 bytes from 10.211.55.201: icmp_seq=2 ttl=64 time=0.290 ms
64 bytes from 10.211.55.201: icmp_seq=3 ttl=64 time=0.386 ms
^C
--- 10.211.55.201 ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.290/0.605/1.425/0.475 ms
> ssh [email protected] echo test
test
> curl http://10.211.55.201:5000
curl: (7) Failed to connect to 10.211.55.201 port 5000 after 1011 ms: Connection refused
端口 5000 上的 Web 服务器是oxen-io/oxen-observer。我安装了所有依赖项并使用启动了服务器FLASK_APP=observer flask run --reload --debugger
。这是一个使用 Flask 托管 Web 服务器的 Python 项目,我想启动它以进行调试。我没有更改任何 Flask 配置,我只是更新了我的contrib/devnet-observer.ini
:
[uwsgi]
chdir = /home/marty/proj/oxen/observer
socket = devnet.wsgi
plugins = python3,logfile
processes = 6
manage-script-name = true
mount = /=observer:app
logger = file:logfile=/home/marty/proj/oxen/observer/uwsgi.log,maxsize=1048576
这肯定是一个错误的配置,因为 Web 服务器返回了 500,但这不应该阻止我的主机检索状态代码。我想先从我的主机访问 Web 服务器,然后再继续处理其他问题。Web 服务器显然可以从本地主机访问,那么我无法从我的主机访问它的原因是什么呢?
答案1
这是默认行为烧瓶(如果您在调试模式下运行,则应谨慎更改它,因为它会使您受到攻击,除非您确保仍然不允许它来自互联网)。
参见Flask 文档关于它以及如何通过将监听端口设置为所有接口来允许外部访问:
外部可见服务器
如果您运行服务器,您会注意到服务器只能从您自己的计算机访问,而不能从网络中的任何其他计算机访问。这是默认设置,因为在调试模式下,应用程序的用户可以在您的计算机上执行任意 Python 代码。
--host=0.0.0.0
如果您禁用了调试器或者信任网络上的用户,则只需在命令行中添加以下内容即可使服务器公开可用 :$ flask run --host=0.0.0.0
这会告诉你的操作系统监听所有公共 IP。