• 【Linux operation 38】解决Linux 端口被占用


    1、前言

    最近在linux服务器上部署应用,总是出现端口被占用的情况,有时关闭软件,kill进程也会出现端口占用。
    有时候关闭软件后,后台进程死掉,也会导致端口被占用。

    2、解决方法

    2.1、查找被占用的端口

    命令:netstat -nultp
    #netstat -nultp #查看端口使用情况
    #netstat -nultp | grep 22 #只查看端口22的使用情况

    linux-3v74:~ # netstat -nultp
    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      1656/sshd           
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1782/master         
    tcp        0      0 :::22                   :::*                    LISTEN      1656/sshd           
    tcp        0      0 ::1:25                  :::*                    LISTEN      1782/master         
    udp        0      0 0.0.0.0:68              0.0.0.0:*                           1001/wickedd-dhcp4  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    linux-3v74:~ # netstat -nultp | grep 22
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1656/sshd           
    tcp        0      0 :::22                   :::*                    LISTEN      1656/sshd 
    
    • 1
    • 2
    • 3

    命令: netstat -anp
    #netstat -anp #查看端口使用情况
    #netstat -anp | grep 22 #只查看端口22的使用情况

    linux-3v74:~ # netstat -anp
    Active Internet connections (servers and established)
    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      1656/sshd           
    tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1782/master         
    tcp        0      0 10.169.42.82:22         10.169.42.154:54363     ESTABLISHED 1861/sshd: root     
    tcp        0      0 10.169.42.82:22         10.169.42.154:54364     ESTABLISHED 1876/sshd: root@not 
    tcp        0    148 10.169.42.82:22         10.169.42.86:58230      ESTABLISHED 1964/1              
    tcp        0      0 :::22                   :::*                    LISTEN      1656/sshd           
    tcp        0      0 ::1:25                  :::*                    LISTEN      1782/master         
    udp        0      0 0.0.0.0:68              0.0.0.0:*                           1001/wickedd-dhcp4  
    Active UNIX domain sockets (servers and established)
    Proto RefCnt Flags       Type       State         I-Node PID/Program name    Path
    unix  2      [ ]         DGRAM                    15124  1/systemd           /run/systemd/journal/syslog
    unix  2      [ ACC ]     STREAM     LISTENING     27625  1826/gnome-session- @/tmp/.ICE-unix/1826
    unix  2      [ ACC ]     SEQPACKET  LISTENING     15128  1/systemd           /run/udev/control
    unix  2      [ ACC ]     STREAM     LISTENING     15132  1/systemd           /run/lvm/lvmetad.socket
    ......
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    linux-3v74:~ # netstat -anp | grep 22
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1656/sshd           
    tcp        0      0 10.169.42.82:22         10.169.42.154:54363     ESTABLISHED 1861/sshd: root     
    tcp        0      0 10.169.42.82:22         10.169.42.154:54364     ESTABLISHED 1876/sshd: root@not 
    tcp        0     52 10.169.42.82:22         10.169.42.86:58230      ESTABLISHED 1964/1              
    tcp        0      0 :::22                   :::*                    LISTEN      1656/sshd           
    unix  2      [ ACC ]     STREAM     LISTENING     22820  984/nscd            /var/run/nscd/socket
    unix  2      [ ACC ]     STREAM     LISTENING     22337  1/systemd           /var/run/pcscd/pcscd.comm
    unix  2      [ ACC ]     STREAM     LISTENING     22340  1/systemd           /run/dbus/system_bus_socket
    unix  2      [ ]         DGRAM                    27467  1822/systemd        /run/user/484/systemd/notify
    unix  2      [ ACC ]     STREAM     LISTENING     27469  1822/systemd        /run/user/484/systemd/private
    unix  2      [ ACC ]     STREAM     LISTENING     27322  1742/gdm-simple-sla @/tmp/dbus-6GUvF5JW
    unix  2      [ ACC ]     STREAM     LISTENING     22343  1/systemd           @ISCSIADM_ABSTRACT_NAMESPACE
    unix  22     [ ]         DGRAM                    10944  1/systemd           /run/systemd/journal/dev-log
    unix  3      [ ]         STREAM     CONNECTED     22784  1/systemd           /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     22782  981/dbus-daemon     
    unix  3      [ ]         STREAM     CONNECTED     27522  1836/at-spi-bus-lau 
    unix  3      [ ]         STREAM     CONNECTED     22862  981/dbus-daemon     
    unix  3      [ ]         STREAM     CONNECTED     22864  981/dbus-daemon     /run/dbus/system_bus_socket
    unix  3      [ ]         STREAM     CONNECTED     27440  1822/systemd        
    unix  2      [ ]         DGRAM                    22799  984/nscd            
    unix  3      [ ]         STREAM     CONNECTED     22449  974/VGAuthService   
    unix  3      [ ]         STREAM     CONNECTED     22356  1/systemd           
    unix  3      [ ]         STREAM     CONNECTED     22450  1/systemd           /run/systemd/journal/stdout
    unix  3      [ ]         STREAM     CONNECTED     22863  981/dbus-daemon     
    unix  3      [ ]         STREAM     CONNECTED     23622  1057/wickedd-nanny  
    unix  2      [ ]         DGRAM                    27455  1822/systemd        
    unix  3      [ ]         STREAM     CONNECTED     26622  1782/master         
    linux-3v74:~ # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.2、查看端口属于哪个程序?端口被哪个进程占用

    命令:lsof -i :22
    #如果用lsof -i命令查不到端口, 安装一下就ok了 yum install lsof -y
    Tip : lsof -i 需要 root 用户的权限来执行

    linux-3v74:~ # lsof -i :22
    COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    sshd    1656 root    3u  IPv4  25129      0t0  TCP *:ssh (LISTEN)
    sshd    1656 root    4u  IPv6  25135      0t0  TCP *:ssh (LISTEN)
    sshd    1861 root    4u  IPv4  27727      0t0  TCP 10.169.42.82:ssh->CN-W-GRSTV63.INT.vertivco.com:54363 (ESTABLISHED)
    sshd    1876 root    4u  IPv4  27827      0t0  TCP 10.169.42.82:ssh->CN-W-GRSTV63.INT.vertivco.com:54364 (ESTABLISHED)
    sshd    1964 root    4u  IPv4  30076      0t0  TCP 10.169.42.82:ssh->DESKTOP-47MK0MU.INT.vertivco.com:58230 (ESTABLISHED)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    2.3、杀掉占用端口的进程

    在查到端口占用的进程后,如果你要杀掉对应的进程可以使用 kill 命令:

    kill -9 PID
    
    • 1
  • 相关阅读:
    golang工程组件——redigo使用(redis协议,基本命令,管道,事务,发布订阅,stream)
    ApplicationContext种类
    【教3妹学算法-每日1题】采集果实
    【Apache Shiro】超详细笔记-1:功能、架构、集成Spring、认证
    【vscode+clangd】clangd不起作用的解决方案、compile_commands.json文件为空的解决方案
    node.js-包
    基于MATLAB开发AUTOSAR软件应用层模块-part9.AUTOSAR工具箱的功能介绍-2
    springboot导出Excel中,文件名带@,没有正常显示,显示为%40
    蓝牙运动耳机什么品牌性价比高,六款值得推荐的运动耳机分享
    【vue3源码】十一、初始vue3中的渲染器
  • 原文地址:https://blog.csdn.net/qq_22938603/article/details/126000458