> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bicola.me/llms.txt
> Use this file to discover all available pages before exploring further.

# Linux 网络管理

> ping / curl / ss / lsof —— 部署后服务通不通的全套排查命令

<Note>
  部署后最高频的问题：「服务启动了但访问不通」。本页这些命令帮你按层定位 —— 是网络问题、防火墙问题，还是服务根本没监听。
</Note>

## 1. 连通性测试：ping

```bash theme={null}
ping baidu.com              # 持续 ping，按 Ctrl+C 停止
ping -c 4 baidu.com         # -c = count，只 ping 4 次
ping 192.168.1.1            # 也可以 ping IP
ping -i 2 baidu.com         # -i = --interval，间隔秒数
ping -W 1 -c 3 baidu.com    # -W 单次响应超时（秒）
```

**怎么判断**：

* 能收到回包 → **网络层**通
* 不通 → 可能是网络断、对方防火墙拒 ICMP（很多云主机默认禁 ping，**不能 ping 通不代表服务不通**）
* 看 `time=` 数值判断延迟、看 `0% packet loss` 判断丢包

## 2. HTTP 测试与下载

<AccordionGroup>
  <Accordion title="curl (Client for URLs) — 测 HTTP 服务">
    ```bash theme={null}
    curl http://localhost                    # 测本机 80 端口
    curl http://localhost:8080/health        # 测健康检查接口
    curl -I https://example.com              # -I = --head，只看响应头
    curl -v http://localhost                 # -v = --verbose，看完整请求/响应过程
    curl -L https://bit.ly/short             # -L = --location，跟随重定向
    curl --connect-timeout 5 url             # 连接超时（秒）

    # POST 请求
    curl -X POST -d "name=foo" http://api.example.com/users
    #    -X = --request 指定方法
    #    -d = --data    请求体

    # 带请求头
    curl -H "Authorization: Bearer xxx" \
         -H "Content-Type: application/json" \
         -d '{"name":"foo"}' \
         http://api/users

    # 保存响应到文件
    curl -o app.tar.gz https://xx/app.tar.gz   # -o = --output 自定义文件名
    curl -O https://xx.com/app.tar.gz          # -O 用原文件名
    ```

    **部署排查思路**：

    1. 在服务器本机 `curl http://localhost:端口` —— 通 = 服务起了
    2. 不通 → 服务没起 / 没监听这个端口
    3. 通了但外面访问不到 → 防火墙、安全组、或绑了 `127.0.0.1` 没绑 `0.0.0.0`
  </Accordion>

  <Accordion title="wget (web get) — 下载文件">
    ```bash theme={null}
    wget https://xx.com/app.tar.gz       # 下载到当前目录
    wget -O new.tar.gz url               # -O 自定义文件名
    wget -c url                          # -c = --continue，断点续传
    wget -q url                          # -q = --quiet，安静模式（脚本里用）
    wget --no-check-certificate url      # 跳过 HTTPS 证书校验（不安全，仅测试用）
    ```

    `curl -O` 和 `wget` 功能重叠，二者随便用一个即可。`curl` 测接口更方便，`wget` 下大文件更稳。
  </Accordion>
</AccordionGroup>

## 3. 端口与连接：ss / netstat / lsof

<AccordionGroup>
  <Accordion title="ss (socket statistics) — 看端口监听（推荐）">
    ```bash theme={null}
    ss -tlnp                    # ⭐ 最常用：所有 TCP 监听端口 + 进程
    ss -tlnp | grep :80         # 看 80 端口被谁占了
    ss -tunap                   # TCP + UDP，所有连接 + 进程
    ss -s                       # summary，连接数统计概览
    ss -ant state established   # 看所有已建立的 TCP 连接
    ```

    **参数拆解**：

    | 参数   | 全称        | 含义                      |
    | ---- | --------- | ----------------------- |
    | `-t` | tcp       | 只看 TCP                  |
    | `-u` | udp       | 只看 UDP                  |
    | `-l` | listening | 只看 LISTEN 状态（监听中）       |
    | `-n` | numeric   | 显示数字端口（不解析为服务名，更快）      |
    | `-p` | process   | 显示占用进程（需 sudo 才能看到完整信息） |
    | `-a` | all       | 全部连接（含 ESTABLISHED）     |

    **典型输出**：

    ```text theme={null}
    LISTEN  0  511  0.0.0.0:80   0.0.0.0:*   users:(("nginx",pid=1234,fd=6))
                    │            │           └ 占用进程
                    │            └ 远端地址（* 表示任意）
                    └ 本地监听地址（0.0.0.0 = 所有网卡，127.0.0.1 = 仅本机）
    ```

    <Tip>
      监听 `127.0.0.1:80` 的服务**外网访问不到**！要监听 `0.0.0.0:80`（所有网卡）才行。这是新手最常踩的坑之一。
    </Tip>
  </Accordion>

  <Accordion title="netstat (network statistics) — 老命令">
    ```bash theme={null}
    netstat -tulpn         # 同 ss -tulpn
    netstat -an            # 所有连接
    netstat -rn            # 路由表
    ```

    `netstat` 来自 `net-tools`，新系统可能没装。**功能上 `ss` 完全替代它**，且 `ss` 速度快得多。仅在极老的系统上需要 netstat。
  </Accordion>

  <Accordion title="lsof (list open files) — 端口被谁占了">
    ```bash theme={null}
    lsof -i :80                 # 谁占了 80 端口
    lsof -i :3306               # 谁占了 3306（MySQL）
    lsof -p 1234                # PID 1234 打开了哪些文件 / socket
    lsof -i tcp:8080            # 只看 TCP
    lsof -u alice               # alice 用户打开的所有文件
    lsof /var/log/nginx/error.log   # 谁在用这个文件
    ```

    **「Address already in use」时第一个想到 `lsof -i :端口`**，立刻知道哪个进程占着不放。
  </Accordion>
</AccordionGroup>

## 4. 排查思路：服务通不通的标准流程

<Steps>
  <Step title="服务有没有起来？">
    ```bash theme={null}
    systemctl status myapp
    ps aux | grep myapp
    ```
  </Step>

  <Step title="服务在不在监听端口？">
    ```bash theme={null}
    sudo ss -tlnp | grep :8000
    ```

    没结果 = 没监听；监听在 `127.0.0.1` 而不是 `0.0.0.0` = 外部访问不到。
  </Step>

  <Step title="本机能不能访问？">
    ```bash theme={null}
    curl -v http://localhost:8000/health
    ```

    本机不通 = 应用 bug；本机通外部不通 = 防火墙 / 安全组。
  </Step>

  <Step title="网络层通不通？">
    ```bash theme={null}
    # 在客户端机器上
    ping <服务器IP>
    nc -zv <服务器IP> 8000     # nc = netcat，专门测端口连通
    telnet <服务器IP> 8000      # 老办法，看到 Connected 就是通
    ```
  </Step>

  <Step title="DNS 解析对不对？">
    ```bash theme={null}
    dig example.com             # dig = domain information groper（推荐）
    nslookup example.com        # name server lookup（旧但通用）
    cat /etc/resolv.conf        # 当前 DNS 服务器
    ```
  </Step>

  <Step title="防火墙放行了吗？">
    ```bash theme={null}
    sudo ufw status             # Ubuntu 防火墙
    sudo firewall-cmd --list-all   # CentOS 防火墙
    sudo iptables -L -n         # 通用
    ```

    云主机额外检查 **安全组 / 网络 ACL** 是否放行了端口。
  </Step>
</Steps>

## 5. 抓包与诊断（进阶了解）

```bash theme={null}
sudo tcpdump -i any port 80         # 抓 80 端口的包
sudo tcpdump -i eth0 host 1.2.3.4   # 抓和指定 IP 的通信
mtr baidu.com                       # 持续 traceroute，看每一跳的丢包率
traceroute baidu.com                # 追踪路由路径
```

新手不需要立刻掌握，但记住这些命令的存在 —— 真到了需要分析跨主机网络问题时，`tcpdump` 是终极武器。

<Card title="远程登录与文件传输 → SSH 专题" icon="terminal" href="/linux/ssh">
  `ssh` 远程登录、密钥配置、`scp` / `rsync` 文件传输、端口转发隧道，单独整理为一篇。
</Card>
