有时候我们需要知道电脑中有哪些进程/应用程序正在使用哪个端口。这时候,我们使用一个非常方便的命令:lsof命令lsof是listopenfiles的意思。在Unix系统中,一切都是文件,包括tcp/udp连接。示例:lsof-n-P-iTCP|grepLISTENflag:-n表示用数字而不是字母显示-P具体是指端口用数字而不是字母显示-i是指internet相关的协议,这里我们指定TCP|grepLISTENin在当前命令lsof-n-P-iTCP给出的所有结果中,搜索带有“LISTEN”的结果例如:caoda1@CaoDas-MBP:~/Documents$lsof-n-P-iTCP|grepLISTENpycharm407caoda1143uIPv40x15532e64ce8c66af0t0TCP127.0.0.1:6942(LISTEN)pycharm407caoda1222uIPv40x15532e64d4531a8f0t0TCP127.0.0.1:63342(LISTEN)rapportd449caoda13uIPv40x15532e64ce3bd6af0t0TCP*:56197(LISTEN)rapportd449caoda14uIPv60x15532e64ce3e50070t0TCP*:56197(LISTEN)com.apple631caoda145uIPv40x15532e64d9f7a6af0t0TCP*:60496(LISTEN)com.apple631caoda148uIPv60x15532e64ce3e61470t0TCP*:60497(LISTEN)mongod673caoda16uIPv40x15532e64d1a763ef0t0TCP127.0.0.1:27017(LISTEN)BlueJeans695caoda13uIPv40x15532e64ce3bba8f0t0TCP127.0.0.1:18171(LISTEN)Adobe\x20831caoda110uIPv40x15532e64d453112f0t0TCP127.0.0.1:15292(LISTEN)Avast\x20849caoda14uIPv60x15532e64ce3e4a470t0TCP*:27275(LISTEN)mysqld39165caoda128uIPv40x15532e64fa16c6af0t0TCP127.0.0.1:3306(LISTEN)我们可以看到:应用程序名称,进程ID,IP地址和端口号,以及它们的LISTEN状态grepmysqlmysqld887caoda129uIPv40x21154199f40554390t0TCP127.0.0.1:3306(LISTEN)我们可以清楚的看到mysql正在使用3306端口
