{"id":346,"date":"2022-03-08T14:30:42","date_gmt":"2022-03-08T06:30:42","guid":{"rendered":"https:\/\/www.stusc.com\/?p=346"},"modified":"2022-03-08T14:32:11","modified_gmt":"2022-03-08T06:32:11","slug":"linux-%e6%9f%a5%e7%9c%8b%e7%ab%af%e5%8f%a3%e5%8d%a0%e7%94%a8%e6%83%85%e5%86%b5","status":"publish","type":"post","link":"https:\/\/www.stusc.com\/?p=346","title":{"rendered":"Linux \u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<p>Linux \u67e5\u770b\u7aef\u53e3\u5360\u7528\u60c5\u51b5\u53ef\u4ee5\u4f7f\u7528&nbsp;<strong>lsof<\/strong>&nbsp;\u548c&nbsp;<strong>netstat<\/strong>&nbsp;\u547d\u4ee4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">lsof<\/h2>\n\n\n\n<p>lsof(list open files)\u662f\u4e00\u4e2a\u5217\u51fa\u5f53\u524d\u7cfb\u7edf\u6253\u5f00\u6587\u4ef6\u7684\u5de5\u5177\u3002<\/p>\n\n\n\n<p>lsof \u67e5\u770b\u7aef\u53e3\u5360\u7528\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lsof -i:\u7aef\u53e3\u53f7<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h3>\n\n\n\n<p>\u67e5\u770b\u670d\u52a1\u5668 8000 \u7aef\u53e3\u7684\u5360\u7528\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># lsof -i:8000\nCOMMAND   PID USER   FD   TYPE   DEVICE SIZE\/OFF NODE NAME\nnodejs  26993 root   10u  IPv4 37999514      0t0  TCP *:8000 (LISTEN)<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230 8000 \u7aef\u53e3\u5df2\u7ecf\u88ab\u8f7b nodejs \u670d\u52a1\u5360\u7528\u3002<\/p>\n\n\n\n<p>lsof -i \u9700\u8981 root \u7528\u6237\u7684\u6743\u9650\u6765\u6267\u884c\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"984\" height=\"322\" src=\"https:\/\/www.stusc.com\/wp-content\/uploads\/2022\/03\/image.png\" alt=\"\" class=\"wp-image-347\" srcset=\"https:\/\/www.stusc.com\/wp-content\/uploads\/2022\/03\/image.png 984w, https:\/\/www.stusc.com\/wp-content\/uploads\/2022\/03\/image-300x98.png 300w, https:\/\/www.stusc.com\/wp-content\/uploads\/2022\/03\/image-768x251.png 768w, https:\/\/www.stusc.com\/wp-content\/uploads\/2022\/03\/image-850x278.png 850w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><\/figure>\n\n\n\n<p>\u66f4\u591a lsof \u7684\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">lsof -i:8080\uff1a\u67e5\u770b8080\u7aef\u53e3\u5360\u7528\nlsof abc.txt\uff1a\u663e\u793a\u5f00\u542f\u6587\u4ef6abc.txt\u7684\u8fdb\u7a0b\nlsof -c abc\uff1a\u663e\u793aabc\u8fdb\u7a0b\u73b0\u5728\u6253\u5f00\u7684\u6587\u4ef6\nlsof -c -p 1234\uff1a\u5217\u51fa\u8fdb\u7a0b\u53f7\u4e3a1234\u7684\u8fdb\u7a0b\u6240\u6253\u5f00\u7684\u6587\u4ef6\nlsof -g gid\uff1a\u663e\u793a\u5f52\u5c5egid\u7684\u8fdb\u7a0b\u60c5\u51b5\nlsof +d \/usr\/local\/\uff1a\u663e\u793a\u76ee\u5f55\u4e0b\u88ab\u8fdb\u7a0b\u5f00\u542f\u7684\u6587\u4ef6\nlsof +D \/usr\/local\/\uff1a\u540c\u4e0a\uff0c\u4f46\u662f\u4f1a\u641c\u7d22\u76ee\u5f55\u4e0b\u7684\u76ee\u5f55\uff0c\u65f6\u95f4\u8f83\u957f\nlsof -d 4\uff1a\u663e\u793a\u4f7f\u7528fd\u4e3a4\u7684\u8fdb\u7a0b\nlsof -i -U\uff1a\u663e\u793a\u6240\u6709\u6253\u5f00\u7684\u7aef\u53e3\u548cUNIX domain\u6587\u4ef6<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">netstat<\/h2>\n\n\n\n<p><strong>netstat -tunlp<\/strong>&nbsp;\u7528\u4e8e\u663e\u793a tcp\uff0cudp \u7684\u7aef\u53e3\u548c\u8fdb\u7a0b\u7b49\u76f8\u5173\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>netstat \u67e5\u770b\u7aef\u53e3\u5360\u7528\u8bed\u6cd5\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">netstat -tunlp | grep \u7aef\u53e3\u53f7<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>-t (tcp) \u4ec5\u663e\u793atcp\u76f8\u5173\u9009\u9879<\/li><li>-u (udp)\u4ec5\u663e\u793audp\u76f8\u5173\u9009\u9879<\/li><li>-n \u62d2\u7edd\u663e\u793a\u522b\u540d\uff0c\u80fd\u663e\u793a\u6570\u5b57\u7684\u5168\u90e8\u8f6c\u5316\u4e3a\u6570\u5b57<\/li><li>-l \u4ec5\u5217\u51fa\u5728Listen(\u76d1\u542c)\u7684\u670d\u52a1\u72b6\u6001<\/li><li>-p \u663e\u793a\u5efa\u7acb\u76f8\u5173\u94fe\u63a5\u7684\u7a0b\u5e8f\u540d<\/li><\/ul>\n\n\n\n<p>\u4f8b\u5982\u67e5\u770b 8000 \u7aef\u53e3\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># netstat -tunlp | grep 8000\ntcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      26993\/nodejs   <\/pre>\n\n\n\n<p>\u66f4\u591a\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">netstat -ntlp   \/\/\u67e5\u770b\u5f53\u524d\u6240\u6709tcp\u7aef\u53e3\nnetstat -ntulp | grep 80   \/\/\u67e5\u770b\u6240\u670980\u7aef\u53e3\u4f7f\u7528\u60c5\u51b5\nnetstat -ntulp | grep 3306   \/\/\u67e5\u770b\u6240\u67093306\u7aef\u53e3\u4f7f\u7528\u60c5\u51b5<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">kill<\/h2>\n\n\n\n<p>\u5728\u67e5\u5230\u7aef\u53e3\u5360\u7528\u7684\u8fdb\u7a0b\u540e\uff0c\u5982\u679c\u4f60\u8981\u6740\u6389\u5bf9\u5e94\u7684\u8fdb\u7a0b\u53ef\u4ee5\u4f7f\u7528 kill \u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kill -9 PID<\/pre>\n\n\n\n<p>\u5982\u4e0a\u5b9e\u4f8b\uff0c\u6211\u4eec\u770b\u5230 8000 \u7aef\u53e3\u5bf9\u5e94\u7684 PID \u4e3a 26993\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6740\u6b7b\u8fdb\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">kill -9 26993<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u7cfb\u7edf\u4e0b\u5982\u4f55\u67e5\u770b\u7aef\u53e3\u5f00\u653e\u72b6\u6001\uff0c\u7b80\u5355\u6765\u8bf4\u53ef\u901a\u8fc7lsof\u4e0enetstat\u4e24\u4e2a\u547d\u4ee4\u6765\u5b9e\u73b0\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[91,99,100,98],"class_list":{"0":"post-346","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-tech","7":"tag-linux","8":"tag-lsof","9":"tag-netstat","10":"tag-98","11":"h-entry","13":"h-as-article"},"_links":{"self":[{"href":"https:\/\/www.stusc.com\/index.php?rest_route=\/wp\/v2\/posts\/346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stusc.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stusc.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stusc.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stusc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=346"}],"version-history":[{"count":3,"href":"https:\/\/www.stusc.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions"}],"predecessor-version":[{"id":350,"href":"https:\/\/www.stusc.com\/index.php?rest_route=\/wp\/v2\/posts\/346\/revisions\/350"}],"wp:attachment":[{"href":"https:\/\/www.stusc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stusc.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stusc.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}