apache2 和 sshd 正在侦听,但只能从远程访问 sshd

Cha*_*ell 2 apache2 aws chef

我刚刚使用厨师在 Ubuntu (aws) 上安装了 apache2。

Ubuntu 服务器上的 Apache2:10.1.1.1

客户端:10.1.1.200

我还验证了 tcp 22 (sshd) 和 tcp 80 (apache2) 都在监听

root@Ubuntu:/var/www# netstat -anp | egrep '22|80'
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1469/sshd       
tcp6       0      0 :::80                   :::*                    LISTEN      16950/apache2   
tcp6       0      0 :::22                   :::*                    LISTEN      1469/sshd       
root@Ubuntu:/var/www# 
Run Code Online (Sandbox Code Playgroud)

流程看起来也不错

root@Ubuntu:/var/www# ps -ef | egrep 'apache2|sshd'
root      1469     1  0 Sep29 ?        00:00:00 /usr/sbin/sshd -D
root     16950     1  0 Sep29 ?        00:00:00 /usr/sbin/apache2 -k start
www-data 16953 16950  0 Sep29 ?        00:00:02 /usr/sbin/apache2 -k start
www-data 16954 16950  0 Sep29 ?        00:00:02 /usr/sbin/apache2 -k start
root@Ubuntu:/var/www# 
Run Code Online (Sandbox Code Playgroud)

服务也照常运行

root@Ubuntu:/var/www# service apache2 status
? apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           ??apache2-systemd.conf
   Active: active (running) since Fri 2017-09-29 23:36:49 UTC; 2h 13min ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 55
   Memory: 6.5M
      CPU: 4.637s
   CGroup: /system.slice/apache2.service
           ??16950 /usr/sbin/apache2 -k start
           ??16953 /usr/sbin/apache2 -k start
           ??16954 /usr/sbin/apache2 -k start

Sep 29 23:36:47 Ubuntu systemd[1]: Starting LSB: Apache2 web server...
Sep 29 23:36:47 Ubuntu apache2[16933]:  * Starting Apache httpd web server apache2
Sep 29 23:36:48 Ubuntu apache2[16933]: AH00557: apache2: apr_sockaddr_info_get() failed for Ubuntu
Sep 29 23:36:48 Ubuntu apache2[16933]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the '
Sep 29 23:36:49 Ubuntu apache2[16933]:  *
Sep 29 23:36:49 Ubuntu systemd[1]: Started LSB: Apache2 web server.
root@Ubuntu:/var/www# 
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试通过 远程访问服务器时http://10.1.1.1,没有任何反应。我在同一台服务器上使用 ssh 没有这个问题。

尝试从远程访问 Web 服务器时,我也运行了 tcpdump,但我什么也没看到

来自客户的测试

[root@Client]# curl 10.1.1.1:80
curl: (7) Failed connect to 10.1.1.1:80; Connection timed out
[root@Client]# 
Run Code Online (Sandbox Code Playgroud)

但是在服务器上什么也没看到

root@Ubuntu:/var/www# tcpdump -nni eth0 port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C
0 packets captured
0 packets received by filter
0 packets dropped by kernel
root@Ubuntu:/var/www# 
Run Code Online (Sandbox Code Playgroud)

但是,当我用 ssh 尝试同样的事情时,我可以远程访问它,并按预期在 tcpdump 上看到流量。

[root@Client]# curl 10.1.1.1:22
SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
curl: (56) Recv failure: Connection reset by peer
[root@Client]# 
Run Code Online (Sandbox Code Playgroud)

ssh 上的 tcpdump

root@Ubuntu:/var/www# tcpdump -nni eth0 port 22 and host 10.1.1.200
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
02:03:04.320761 IP 10.1.1.200.35890 > 10.1.1.1.22: Flags [S], seq 1658944714, win 26883, options [mss 8961,sackOK,TS val 52461068 ecr 0,nop,wscale 7], length 0
02:03:04.320923 IP 10.1.1.1.22 > 10.1.1.200.35890: Flags [S.], seq 2270168630, ack 1658944715, win 26847, options [mss 8961,sackOK,TS val 13211819 ecr 52461068,nop,wscale 7], length 0
02:03:04.321264 IP 10.1.1.200.35890 > 10.1.1.1.22: Flags [.], ack 1, win 211, options [nop,nop,TS val 52461069 ecr 13211819], length 0
02:03:04.321287 IP 10.1.1.200.35890 > 10.1.1.1.22: Flags [P.], seq 1:80, ack 1, win 211, options [nop,nop,TS val 52461069 ecr 13211819], length 79
02:03:04.321293 IP 10.1.1.1.22 > 10.1.1.200.35890: Flags [.], ack 80, win 210, options [nop,nop,TS val 13211819 ecr 52461069], length 0
02:03:04.326160 IP 10.1.1.1.22 > 10.1.1.200.35890: Flags [P.], seq 1:42, ack 80, win 210, options [nop,nop,TS val 13211821 ecr 52461069], length 41
02:03:04.326683 IP 10.1.1.1.22 > 10.1.1.200.35890: Flags [R.], seq 42, ack 80, win 210, options [nop,nop,TS val 13211821 ecr 52461069], length 0
02:03:04.326899 IP 10.1.1.200.35890 > 10.1.1.1.22: Flags [.], ack 42, win 211, options [nop,nop,TS val 52461075 ecr 13211821], length 0
02:03:04.326911 IP 10.1.1.1.22 > 10.1.1.200.35890: Flags [R], seq 2270168672, win 0, length 0
^C
9 packets captured
9 packets received by filter
0 packets dropped by kernel
root@Ubuntu:/var/www# 
Run Code Online (Sandbox Code Playgroud)

更新: iptables 输出

另外,这个盒子安装在aws上

root@Ubuntu:~# iptables -vL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
root@Ubuntu:~# 
Run Code Online (Sandbox Code Playgroud)

小智 6

Apache 正在侦听 IPv6,而不是 IPv4:

tcp6       0      0 :::80                   :::*                    LISTEN      16950/apache2
Run Code Online (Sandbox Code Playgroud)

tcp6并且:::80都是 Apache 正在侦听 IPv6 而不是 v4 的线索(这些都是tcp0.0.0.0:80否则)。

这个问题有很多解决方案。可能最好的方法是告诉 Apache 显式监听 IPv4。Listen从这样的事情改变你的指令:

Listen 80
Run Code Online (Sandbox Code Playgroud)

到:

Listen 0.0.0.0:80
Run Code Online (Sandbox Code Playgroud)