{"id":234498,"date":"2022-01-30T10:54:56","date_gmt":"2022-01-30T02:54:56","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=234498"},"modified":"2022-01-21T09:55:59","modified_gmt":"2022-01-21T01:55:59","slug":"linux-shiyou-port","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-shiyou-port.html","title":{"rendered":"Linux\u4e2d\u662f\u8c01\u5360\u7528\u4e86\u6211\u7684\u7aef\u53e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528netstat\uff0css\u548clsof\u547d\u4ee4\u627e\u51fa\u54ea\u4e9b\u670d\u52a1\u6b63\u5728\u4fa6\u542c\u54ea\u4e9b\u7aef\u53e3\u3002\u8be5\u8bf4\u660e\u9002\u7528\u4e8e\u6240\u6709\u57fa\u4e8eLinux\u548cUnix\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u4f8b\u5982macOS\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

Address already in use\u8fd9\u4e2a\u63d0\u793a,\u60f3\u5fc5\u5927\u5bb6\u9047\u5230\u8fc7,\u600e\u4e48\u80fd\u5feb\u901f\u627e\u5230\u95ee\u9898\u5e76\u89e3\u51b3\u5462?\u4e0b\u9762\u6709\u51e0\u79cd\u59ff\u52bf\u53ef\u4ee5\u4e86\u89e3\u4e00\u4e0b.
\n\u5728\u5bf9\u7f51\u7edc\u8fde\u63a5\u6216\u7279\u5b9a\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u95ee\u9898\u8fdb\u884c\u6545\u969c\u6392\u9664\u65f6\uff0c\u9996\u5148\u8981\u68c0\u67e5\u7684\u4e8b\u60c5\u4e4b\u4e00\u5e94\u8be5\u662f\u7cfb\u7edf\u4e0a\u5b9e\u9645\u4f7f\u7528\u4e86\u54ea\u4e9b\u7aef\u53e3\uff0c\u4ee5\u53ca\u54ea\u4e2a\u5e94\u7528\u7a0b\u5e8f\u6b63\u5728\u4fa6\u542c\u7279\u5b9a\u7684\u7aef\u53e3\u3002
\n\"\"<\/a><\/p>\n

# \u4ec0\u4e48\u662f\u76d1\u542c\u7aef\u53e3<\/strong><\/span><\/div>\n

\u7f51\u7edc\u7aef\u53e3\u7531\u5176\u7f16\u53f7\uff0c\u5173\u8054\u7684IP\u5730\u5740\u548c\u901a\u4fe1\u534f\u8bae(\u4f8b\u5982TCP\u6216UDP)\u7684\u7c7b\u578b\u6807\u8bc6\u3002<\/p>\n

\u4fa6\u542c\u7aef\u53e3\u662f\u5e94\u7528\u7a0b\u5e8f\u6216\u8fdb\u7a0b\u5728\u5176\u4e0a\u4fa6\u542c\u7684\u7f51\u7edc\u7aef\u53e3\uff0c\u5145\u5f53\u901a\u4fe1\u7aef\u70b9\u3002<\/p>\n

\u6bcf\u4e2a\u76d1\u542c\u7aef\u53e3\u90fd\u53ef\u4ee5\u4f7f\u7528\u9632\u706b\u5899\u6253\u5f00\u6216\u5173\u95ed(\u8fc7\u6ee4)\u3002\u4e00\u822c\u800c\u8a00\uff0c\u5f00\u653e\u7aef\u53e3\u662f\u4e00\u4e2a\u7f51\u7edc\u7aef\u53e3\uff0c\u5b83\u63a5\u53d7\u6765\u81ea\u8fdc\u7a0b\u4f4d\u7f6e\u7684\u4f20\u5165\u6570\u636e\u5305\u3002<\/p>\n

\u4f60\u4e0d\u80fd\u8ba9\u4e24\u4e2a\u670d\u52a1\u76d1\u542c\u540c\u4e00IP\u5730\u5740\u4e0a\u7684\u540c\u4e00\u7aef\u53e3\u3002<\/p>\n

\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6b63\u5728\u8fd0\u884c\u4e00\u4e2a\u76d1\u542c\u7aef\u53e380\u548c443\u7684Apache Web\u670d\u52a1\u5668\uff0c\u5e76\u4e14\u5c1d\u8bd5\u5b89\u88c5Nginx \uff0c\u5219\u540e\u8005\u5c06\u65e0\u6cd5\u542f\u52a8\uff0c\u56e0\u4e3aHTTP\u548cHTTPS\u7aef\u53e3\u662f\u5df2\u7ecf\u5728\u4f7f\u7528\u4e2d\u3002<\/p>\n

# \u7528netstat\u68c0\u67e5\u76d1\u542c\u7aef\u53e3<\/strong><\/span><\/div>\n

netstat\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u63d0\u4f9b\u6709\u5173\u7f51\u7edc\u8fde\u63a5\u7684\u4fe1\u606f\u3002<\/p>\n

\u8981\u5217\u51fa\u6240\u6709\u6b63\u5728\u4fa6\u542c\u7684TCP\u6216UDP\u7aef\u53e3\uff0c\u5305\u62ec\u4f7f\u7528\u7aef\u53e3\u7684\u670d\u52a1\u548c\u5957\u63a5\u5b57\u72b6\u6001\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n

sudo netstat -tunlp\r\n<\/pre>\n

\u6b64\u547d\u4ee4\u4e2d\u4f7f\u7528\u7684\u9009\u9879\u5177\u6709\u4ee5\u4e0b\u542b\u4e49\uff1a<\/p>\n

    \n
  1. -t-\u663e\u793aTCP\u7aef\u53e3\u3002<\/li>\n
  2. -u -\u663e\u793aUDP\u7aef\u53e3\u3002<\/li>\n
  3. -n -\u663e\u793a\u6570\u5b57\u5730\u5740\u800c\u4e0d\u662f\u89e3\u6790\u4e3b\u673a\u3002<\/li>\n
  4. -l -\u4ec5\u663e\u793a\u76d1\u542c\u7aef\u53e3\u3002<\/li>\n
  5. -p -\u663e\u793a\u4fa6\u542c\u5668\u8fdb\u7a0b\u7684PID\u548c\u540d\u79f0\u3002<\/li>\n<\/ol>\n

    \u4ec5\u5f53\u4f60\u4ee5root\u7528\u6237\u6216 sudo \u7528\u6237\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u65f6\uff0c\u624d\u4f1a\u663e\u793a\u6b64\u4fe1\u606f\u3002
    \n\u8f93\u51fa\u5c06\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

    Proto Recv-Q Send-Q Local Address Foreign Address State PID\/Program name\r\ntcp 0 0 0:22 0:* LISTEN 445\/sshd\r\ntcp 0 0 0:25 0:* LISTEN 929\/master\r\ntcp6 0 0 :::3306 ::* LISTEN 534\/mysqld\r\ntcp6 0 0 :::80 :::* LISTEN 515\/apache2\r\ntcp6 0 0 :::22 :::* LISTEN 445\/sshd\r\ntcp6 0 0 :::25 :::* LISTEN 929\/master\r\ntcp6 0 0 :::33060 :::* LISTEN 534\/mysqld\r\nudp 0 0 0:68 0:* 966\/dhclient\r\n<\/pre>\n

    \u5728\u6211\u4eec\u7684\u6848\u4f8b\u4e2d\uff0c\u91cd\u8981\u7684\u51e0\u5217\u662f\uff1a<\/p>\n

      \n
    1. Proto-\u5957\u63a5\u5b57\u4f7f\u7528\u7684\u534f\u8bae\u3002<\/li>\n
    2. Local Address -\u8fdb\u7a0b\u4fa6\u542c\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\u3002<\/li>\n
    3. PID\/Program name -PID\u548c\u8fdb\u7a0b\u540d\u79f0\u3002<\/li>\n<\/ol>\n

      \u5982\u679c\u8981\u8fc7\u6ee4\u7ed3\u679c\uff0c\u8bf7\u4f7f\u7528 grep\u547d\u4ee4\u3002\u4f8b\u5982\uff0c\u8981\u67e5\u627e\u5728TCP\u7aef\u53e322\u4e0a\u4fa6\u542c\u7684\u8fdb\u7a0b\uff0c\u4f60\u53ef\u4ee5\u8f93\u5165\uff1a<\/p>\n

      sudo netstat -tnlp | grep :22\r\n<\/pre>\n

      \u8f93\u51fa\u663e\u793a\u6b64\u8ba1\u7b97\u673a\u4e0a\u7684\u7aef\u53e322\u88abSSH\u670d\u52a1\u5668\u4f7f\u7528\uff1a<\/p>\n

      tcp 0 0 0:22 0:* LISTEN 445\/sshd\r\ntcp6 0 0 :::22 :::* LISTEN 445\/sshd\r\n<\/pre>\n

      \u5982\u679c\u8f93\u51fa\u4e3a\u7a7a\uff0c\u5219\u8868\u793a\u7aef\u53e3\u4e0a\u6ca1\u6709\u76d1\u542c\u3002<\/p>\n

      \u4f60\u4e5f\u53ef\u4ee5\u6839\u636e\u6761\u4ef6\u8fc7\u6ee4\u5217\u8868\uff0c\u4f8b\u5982PID\uff0c\u534f\u8bae\uff0c\u72b6\u6001\u7b49\u3002<\/p>\n

      netstat\u5df2\u8fc7\u65f6\uff0c\u88abss\u548c ip \u53d6\u4ee3\uff0c\u4f46\u5b83\u4ecd\u7136\u662f\u68c0\u67e5\u7f51\u7edc\u8fde\u63a5\u7684\u6700\u5e38\u7528\u547d\u4ee4\u3002<\/p>\n

      # \u7528ss \u68c0\u67e5\u76d1\u542c\u7aef\u53e3 <\/strong><\/span><\/div>\n

      ss\u662f\u65b0\u7684netstat\u3002\u5b83\u7f3a\u5c11netstat\u7684\u67d0\u4e9b\u529f\u80fd\uff0c\u4f46\u662f\u516c\u5f00\u4e86\u66f4\u591a\u7684TCP\u72b6\u6001\uff0c\u5e76\u4e14\u901f\u5ea6\u7a0d\u5feb\u3002\u547d\u4ee4\u9009\u9879\u57fa\u672c\u76f8\u540c\uff0c\u56e0\u6b64\u4ecenetstat\u5230ss\u7684\u8f6c\u6362\u5e76\u4e0d\u56f0\u96be\u3002<\/p>\n

      \u8981\u4f7f\u7528ss\u83b7\u53d6\u6240\u6709\u76d1\u542c\u7aef\u53e3\u7684\u5217\u8868\uff0c\u8bf7\u8f93\u5165\uff1a<\/p>\n

      sudo ss -tunlp\r\n<\/pre>\n

      \u8f93\u51fa\u4e0enetstat\u62a5\u544a\u7684\u8f93\u51fa\u51e0\u4e4e\u76f8\u540c\uff1a<\/p>\n

      State Recv-Q Send-Q Local Address:Port Peer Address:Port\r\nLISTEN 0 128 0:22 0:* users:((\"sshd\",pid=445,fd=3))\r\nLISTEN 0 100 0:25 0:* users:((\"master\",pid=929,fd=13))\r\nLISTEN 0 128 *:3306 *:* users:((\"mysqld\",pid=534,fd=30))\r\nLISTEN 0 128 *:80 *:* users:((\"apache2\",pid=765,fd=4),(\"apache2\",pid=764,fd=4),(\"apache2\",pid=515,fd=4))\r\nLISTEN 0 128 [::]:22 [::]:* users:((\"sshd\",pid=445,fd=4))\r\nLISTEN 0 100 [::]:25 [::]:* users:((\"master\",pid=929,fd=14))\r\nLISTEN 0 70 *:33060 *:* users:((\"mysqld\",pid=534,fd=33))\r\n<\/pre>\n
      # \u4f7f\u7528lsof \u68c0\u67e5\u76d1\u542c\u7aef\u53e3<\/strong><\/span><\/div>\n

      lsof\u662f\u529f\u80fd\u5f3a\u5927\u7684\u547d\u4ee4\u884c\u5e94\u7528\u7a0b\u5e8f\uff0c\u53ef\u63d0\u4f9b\u6709\u5173\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u7684\u4fe1\u606f\u3002<\/p>\n

      \u5728Linux\u4e2d\uff0c\u6240\u6709\u5185\u5bb9\u90fd\u662f\u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u5c06\u5957\u63a5\u5b57\u89c6\u4e3a\u5199\u5165\u7f51\u7edc\u7684\u6587\u4ef6\u3002<\/p>\n

      \u8981\u83b7\u53d6\u5177\u6709lsof\u7684\u6240\u6709\u4fa6\u542cTCP\u7aef\u53e3\u7684\u5217\u8868\uff0c\u8bf7\u8f93\u5165\uff1a<\/p>\n

      sudo lsof -nP -iTCP -sTCP:LISTEN\r\n<\/pre>\n

      \u4f7f\u7528\u7684\u9009\u9879\u5982\u4e0b:<\/p>\n

        \n
      1. -n-\u4e0d\u8981\u5c06\u7aef\u53e3\u53f7\u8f6c\u6362\u4e3a\u7aef\u53e3\u540d\u79f0\u3002<\/li>\n
      2. -p -\u4e0d\u89e3\u6790\u4e3b\u673a\u540d\uff0c\u663e\u793a\u6570\u5b57\u5730\u5740\u3002<\/li>\n
      3. -iTCP -sTCP:LISTEN -\u4ec5\u663e\u793aTCP\u72b6\u6001\u4e3aLISTEN\u7684\u7f51\u7edc\u6587\u4ef6\u3002<\/li>\n<\/ol>\n
        COMMAND PID USER FD TYPE DEVICE SIZE\/OFF NODE NAME\r\nsshd 445 root 3u IPv4 16434 0t0 TCP *:22 (LISTEN)\r\nsshd 445 root 4u IPv6 16445 0t0 TCP *:22 (LISTEN)\r\napache2 515 root 4u IPv6 16590 0t0 TCP *:80 (LISTEN)\r\nmysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN)\r\nmysqld 534 mysql 33u IPv6 19973 0t0 TCP *:33060 (LISTEN)\r\napache2 764 www-data 4u IPv6 16590 0t0 TCP *:80 (LISTEN)\r\napache2 765 www-data 4u IPv6 16590 0t0 TCP *:80 (LISTEN)\r\nmaster 929 root 13u IPv4 19637 0t0 TCP *:25 (LISTEN)\r\nmaster 929 root 14u IPv6 19638 0t0 TCP *:25 (LISTEN)\r\n<\/pre>\n

        \u5927\u591a\u6570\u8f93\u51fa\u5217\u540d\u79f0\u90fd\u662f\u4e0d\u8a00\u81ea\u660e\u7684:<\/p>\n

          \n
        1. COMMAND\uff0cPID\uff0cUSER-\u8fd0\u884c\u4e0e\u7aef\u53e3\u5173\u8054\u7684\u7a0b\u5e8f\u7684\u540d\u79f0\uff0cPID\u548c\u7528\u6237\u3002<\/li>\n
        2. NAME -\u7aef\u53e3\u53f7\u3002<\/li>\n<\/ol>\n

          \u8981\u67e5\u627e\u6b63\u5728\u4fa6\u542c\u7279\u5b9a\u7aef\u53e3(\u4f8b\u5982\u7aef\u53e33306)\u7684\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n

          sudo lsof -nP -iTCP:3306 -sTCP:LISTEN\r\n<\/pre>\n

          \u8f93\u51fa\u663e\u793aMySQL\u670d\u52a1\u5668\u4f7f\u7528\u7aef\u53e33306:<\/p>\n

          COMMAND PID USER FD TYPE DEVICE SIZE\/OFF NODE NAME\r\nmysqld 534 mysql 30u IPv6 17636 0t0 TCP *:3306 (LISTEN)<\/pre>\n","protected":false},"excerpt":{"rendered":"

          Address already in use\u8fd9\u4e2a\u63d0\u793a,\u60f3\u5fc5\u5927\u5bb6\u9047\u5230\u8fc7,\u600e\u4e48\u80fd\u5feb\u901f\u627e\u5230\u95ee\u9898\u5e76\u89e3\u51b3\u5462?\u4e0b\u9762\u6709\u51e0\u79cd\u59ff […]<\/p>\n","protected":false},"author":643,"featured_media":234507,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-234498","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=234498"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234498\/revisions"}],"predecessor-version":[{"id":234506,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/234498\/revisions\/234506"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/234507"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=234498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=234498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=234498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}