網路服務連線測試

From LemonWiki共筆
Jump to navigation Jump to search

如何測試與排除網路服務連線問題。找出問題發生的地方:(1) 伺服器上的本地服務 (localhost) (2) 從區域網路到伺服器 (3) 從廣域網路到伺服器

🌐 Switch language: EN, 漢字


如何連線到網路服務 (Web 服務)[edit]

如果服務是網站伺服器 (web service):

  • (1) 打開瀏覽器,(2) 輸入地址:http://127.0.0.1http://the.domain
  • Linux Lynx 指令:
    1. (如果尚未安裝lynx,則為可選操作) yum install lynx 適用於CentOS / sudo apt-get install lynx 適用於Ubuntu[1]
    2. lynx http://127.0.0.1 [2] 適用於Linux Os linux.png
  • telnet 指令
    1. telnet 127.0.0.1 <PORT> 例如 telnet 127.0.0.1 80telnet the.domain 80
    2. (在telnet連接80埠後,輸入) get + SPACE[3]
  • curl[4]: curl -vL http://IP:Port
    • (如果尚未安裝curl指令,則為可選操作) 在Windows/MacOSX/Linux上安裝和運行cURL | Ubidots幫助中心
    • 指令範例:
      • (1) 使用 curl -voL https://www.google.com 將輸出寫入<file>而非stdout
      • (2) 使用 curl -vL https://www.google.comcurl -voL /dev/null https://www.google.com 將輸出寫入stdout
      • (3) 使用 curl -vOL https://www.google.com/index.html 將輸出寫入<file>並使用遠程檔案名
    • 如果失敗,將顯示消息:"failed: Connection refused"
  • Linux wget 軟體例如 wget https://www.google.com
  • 使用第三方網站監控服務:Web Ping

如果服務不是網站伺服器,則使用telnet

  1. (可選) Win Os windows.png 上安裝Telnet用戶端 | Microsoft文件。在macOS high Sierra上重啟telnet – Ayuth Mangmesap (blackSource) – MediummacOS icon_os_mac.png
  2. telnet 127.0.0.1 <PORT> 如果連接網頁服務失敗。它將顯示消息:"telnet: 無法連接到遠程主機:連接被拒絕"

如果安裝了 Python 或 Node.js,則可使用替代網站服務

  • (適用 python 3.x) python -m http.server <port> (預設 port: 8000)[5]
  • (適用 Node.js) npx http-server -p <port> (預設 port: 8080)[6]

測試本地網路服務的連接性[edit]

WAN ○ -------> ○ LAN -------> ● 伺服器

Owl icon.jpg 如果您有管理權限通過SSH或遠程桌面服務登錄伺服器。

檢查您的電腦/伺服器是否被指派了IP位址[edit]

  • Linux Os linux.png :(1) 檢查IP位址。ifconfig。 (2) 重新分配IP位址並重新啟動網路服務。service network restart 用於CentOS[7]
  • Win Os windows.png :檢查IP位址。ifconfig/all
  • 更多關於網路問題

檢查網路服務是否已成功啟動?[edit]

  • Apache /etc/init.d/httpd status 在CentOS7上 [8]
  • nginx sudo service nginx status 在CentOS7上
  • Docker容器 docker ps 檢查容器是否在運行和暴露埠口[9]

檢查MySQL是否已成功啟動?[edit]

使用lsof指令[edit]

lsof -i:3306macOS icon_os_mac.png Linux Os linux.png 在Unix類操作系統中用於列出打開的文件及打開它們的進程。

  • lsof:這代表"列出打開的文件"。它是一個命令行工具,顯示有關由進程打開的文件的資訊。在Unix和Linux中,一切都被視為文件(包括硬體設備、套接字和目錄),因此lsof是系統管理員監控和排除系統行為問題的強大工具。
  • -i:這個選項告訴lsof僅顯示網絡文件。在Unix和Linux中,網絡連接被視為文件。-i選項可用於根據互聯網地址或網絡協議過濾連接。
  • :3306:這指定了埠號。埠3306通常被MySQL使用,這是一個流行的數據庫管理系統。通過包括:3306,您要求lsof列出所有與埠3306相關的網絡文件(包括套接字連接)。

輸出範例:localhost:mysql (LISTEN) 意味著MySQL已成功啟動

COMMAND     PID      USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
mysqld    93034 account   29u  IPv4 0xxxx      0t0  TCP localhost:mysql (LISTEN)

使用ps指令[edit]

ps aux | grep mysqlmacOS icon_os_mac.png

如果MySQL已成功啟動的輸出範例:

_mysql           11083   0.3  0.2 409657280  31424   ??  Ss    2:12下午   0:01.07 /usr/local/mysql/bin/mysqld --user=_mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=/usr/local/mysql/data/mysqld.local.err --pid-file=/usr/local/mysql/data/mysqld.local.pid --keyring-file-data=/usr/local/mysql/keyring/keyring --early-plugin-load=keyring_file=keyring_file.so
ACCOUNT        12349   0.0  0.0 408645328   1648 s011  S+    2:14下午   0:00.00 grep mysql

如果MySQL沒有成功啟動的示例輸出:

ACCOUNT        12349   0.0  0.0 408645328   1648 s011  S+    2:14下午   0:00.00 grep mysql

檢查埠是否被其他進程佔用?[edit]

檢查埠是否被其他協議使用

SG TCP/IP埠數據庫 維基百科 - TCP和UDP埠號列表

Example output of netstat command, more on Find process running on port

$ netstat -tulpn | grep LISTEN

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      - 

// means the 80 port service is running

Check if a port is blocked by browser[edit]

Check if a port is blocked by browser[10]


Is the port opened for external users?[edit]

Check the OS firewall or protected by antivirus software

    • Linux Os linux.png
      • iptables: (1) iptables -L to list all rules[11] (2) rule with smaller line-number will override the rule with larger line-number[12]
      • FirewallD rules: (1) sudo firewall-cmd --get-default-zone return 'public' (2) sudo firewall-cmd --list-all --zone=public[13] (3) Allow port with FirewallD
      • SELinux Policy Management: (1) After installed policycoreutils-python[14], input semanage port -l to list open ports. (2) Allow port with semanage.
    • Win Os windows.png Windows Firewall with Advanced Security -> configure firewall rules
    • protected by antivirus software?
  • connect to localhost ex: telnet 127.0.0.1 23 where 23 is the port number where the service running
    • Install Telnet Client on Win Os windows.png
    • if the local service is web server, you can also try lynx http://127.0.0.1 for Linux Os linux.png
  • double check the server/service logs

Test connectivity from LAN to the server[edit]

WAN ○ -------> ● LAN -------> ● server

Testing on LAN (local network, subnet scope): The different servers between LAN or with the same ip class (ex: 1.2.3.*) and may not have the firewall limit. Private network / Private IP address

  • Is the service alive?
  • Connect to the internal IP eg: telnet 172.18.0.1 23 (1) IP start with 172.18... is Private IP Network Numbers (2) where 23 is the port number where the service running
  • If the local service is web server, you can also try to connect the internal IP e.g. lynx http://172.18.0.1 for Linux Os linux.png
  • Double check the server/service logs
  • Is the internal IP correct?

Test connectivity from WAN to the remote server[edit]

WAN ● -------> ● LAN -------> ● server

We met the trouble on WAN (wide area network). If we do no have the administrative permission, the tests we can do as follows:

other issue:

Tools[edit]

  1. Good.gif CurrPorts: Monitoring TCP/IP network connections on Windows
  2. PFPortCheck Program: check TCP/UDP port from localhost(where you installed the program) to portforward.com
  3. TCPView for Windows
  4. Check server: Check host - online website monitoring, Check IP "checks the possibility of a TCP connection to host's specified port from different locations around the world." quoted from website. [Last visited: 2015-08-02]


相關文章[edit]

References[edit]


Troubleshooting of ...

Template