Linux中查看端口是否被占用,并杀死进程

  1. netstat -anp |grep 端口号
    如下,我以3306为例,netstat -anp |grep 3306(此处备注下,如果我是以普通用户操作,则需要加上sudo,如果是以root用户操作,不用加sudo即可查看)
root@hadoop-master lib# netstat  -anp  |grep  3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2953/mysqld     
tcp        0      0 127.0.0.1:32994         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 10.27.92.164:3306       10.26.94.145:36248      ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33242         ESTABLISHED 2953/mysqld     
tcp        0      0 10.27.92.164:3306       10.26.94.145:36246      ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33002         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33003         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:33012         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:3306          127.0.0.1:32998         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:33010         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33238         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33242         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33240         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33000         127.0.0.1:3306          ESTABLISHED 9591/java       

主要看监控状态为LISTEN表示已经被占用,最后一列显示被服务mysqld占用,查看具体端口号,只要有如图这一行就表示被占用了

  1. netstat -nultp
    该命令是查看当前所有已经使用的端口情况
root@hadoop-master lib# netstat -nultp  |grep  3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2953/mysqld 
  1. netstat -anp |grep 端口号
root@hadoop-master lib# netstat  -anp  |grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2953/mysqld     
tcp        0      0 127.0.0.1:32994         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 10.27.92.164:3306       10.26.94.145:36248      ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33242         ESTABLISHED 2953/mysqld     
tcp        0      0 10.27.92.164:3306       10.26.94.145:36246      ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33002         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33003         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:33012         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:3306          127.0.0.1:32998         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:33010         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33238         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33242         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33240         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33000         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 10.27.92.164:3306       10.26.94.145:36254      ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:3306          127.0.0.1:33010         ESTABLISHED 2953/mysqld     
tcp        0      0 127.0.0.1:32998         127.0.0.1:3306          ESTABLISHED 9591/java       
tcp        0      0 127.0.0.1:33003         127.0.0.1:3306          ESTABLISHED 9591/java       

杀死进程:kill -9 pid

你可能感兴趣的:(Linux中查看端口是否被占用,并杀死进程)