360 Secure SkillHub
360 安全技能中心

Linux Port Resolver

面向 Linux 的端口冲突排查技能:用 ss/netstat 查出占用端口的进程,核对 PID 后按 SIGTERM→SIGKILL 顺序释放,或改用其他端口;并给出 systemd 单元、Docker 端口映射与 ExecStartPre 的预防配置及安全检查清单。不包含非 Linux 平台的排查步骤。

laolaoqi
laolaoqi
热度 23.98
最近更新 2026-05-08 11:41:46
分类:自动化依赖:代码执行应用场景:DevOps / 工作流 / 调试排错

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add linux-port-resolver
Metadata
name
linux-port-resolver
description
Resolve Linux port conflicts (EADDRINUSE/bind failed) with precision. Detect occupying process, free the port, and prevent recurrence. Use when a service fails to start with "port already in use", EADDRINUSE, bind failed, or when you need to check what's on a specific port.

Linux Port Resolver

Quick fix port conflicts on Linux without guesswork.

Quick Commands

# Find what's on a port
ss -tlnp | grep :PORT
# Alternative if ss not available
netstat -tlnp 2>/dev/null | grep :PORT
# Also check IPv6
ss -tlnp | grep :PORT

Resolution Steps

  1. Identify the process

    ss -tlnp | grep ":<PORT> "
    # Output: LISTEN 0 128 0.0.0.0:PORT 0.0.0.0:* users:(("process",PID,))
    
  2. Verify the PID - don't kill system processes

    ps -p PID -o pid,ppid,cmd,user
    
  3. Kill gracefully, escalate if needed

    kill PID                  # SIGTERM - graceful
    sleep 2 && ss -tlnp | grep ":<PORT> "  # verify
    kill -9 PID               # SIGKILL - force, last resort
    
  4. Or reconfigure - change the port in the service config instead

    • systemd: edit /etc/systemd/system/SERVICE.service, change port, systemctl daemon-reload && systemctl restart SERVICE
    • Docker: change ports: mapping in docker-compose.yml
    • Custom app: check config file or env var for port setting
  5. Prevent recurrence with ExecStartPre in systemd

    [Service]
    ExecStartPre=/bin/sh -c 'kill -9 $(ss -tlnp | grep ":PORT " | grep -oP "pid=\\K\\d+") 2>/dev/null; true'
    

Safety Checklist

  • Confirmed the port is actually in use
  • Identified the PID correctly
  • PID is not PID 1 or a critical system daemon
  • Verified there's no other instance still running
  • Checked both IPv4 and IPv6 listeners

📢 Need Help?

Need OpenClaw deployed on your server? I offer managed deployment & hosting: