{"id":3019,"date":"2025-05-19T20:12:36","date_gmt":"2025-05-19T11:12:36","guid":{"rendered":"https:\/\/weblog.hirohiro716.com\/?p=3019"},"modified":"2025-05-21T19:43:34","modified_gmt":"2025-05-21T10:43:34","slug":"%e3%83%aa%e3%83%90%e3%83%bc%e3%82%b9ssh%e3%83%88%e3%83%b3%e3%83%8d%e3%83%ab%e3%81%a7%e9%81%a0%e9%9a%94%e5%9c%b0%e3%81%ab%e3%81%82%e3%82%8bubuntu%e3%82%92ssh%e3%81%a7%e6%93%8d%e4%bd%9c%e3%81%99","status":"publish","type":"post","link":"https:\/\/weblog.hirohiro716.com\/?p=3019","title":{"rendered":"\u30ea\u30d0\u30fc\u30b9ssh\u30c8\u30f3\u30cd\u30eb\u3067\u9060\u9694\u5730\u306b\u3042\u308bubuntu\u3092ssh\u3067\u64cd\u4f5c\u3059\u308b"},"content":{"rendered":"<p style=\"margin-top:2em;\">\n\u9060\u9694\u5730\u306ePC\u306fNAT\u5185\u3001\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u306f\u30b0\u30ed\u30fc\u30d0\u30ebIP\u6301\u3061\u3067\u540d\u524d\u89e3\u6c7a\u53ef\u80fd\u3068\u3044\u3046\u524d\u63d0\u3067\u3059\u3002\n<\/p>\n<pre class=\"brush: plain; title: \u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc; notranslate\" title=\"\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\">\r\nOS: Ubuntu 24.04\r\nssh\u306eversion: 9.6\r\n\u30c9\u30e1\u30a4\u30f3\u540d: server.example.com\r\n\u7ba1\u7406\u7528\u30e6\u30fc\u30b6\u30fc: hiro\r\n\u30c8\u30f3\u30cd\u30eb\u5c02\u7528\u30e6\u30fc\u30b6\u30fc: ssh-tunnel\r\n<\/pre>\n<pre class=\"brush: plain; title: \u9060\u9694PC; notranslate\" title=\"\u9060\u9694PC\">\r\nOS: Ubuntu 24.04\r\nssh\u306eversion: 9.6\r\nautossh\u306eversion: 1.4\r\n\u30e6\u30fc\u30b6\u30fc: user\r\n<\/pre>\n<p style=\"margin-top:2em;\">\n\u4e0b\u8a18\u306e\u3088\u3046\u306a\u611f\u3058\u3067\u63a5\u7d9a\u3059\u308b\u3002\u3042\u304f\u307e\u3067\u30a4\u30e1\u30fc\u30b8\u306a\u306e\u3067\u305d\u306e\u4ed6\u30b3\u30de\u30f3\u30c9\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3057\u3066\u3044\u306a\u3044\u3002\u3053\u306e\u307e\u307e\u4f7f\u308f\u306a\u3044\u65b9\u304c\u826f\u3044\u3002\n<\/p>\n<pre class=\"brush: plain; gutter: false; title: \u63a5\u7d9a\u30a4\u30e1\u30fc\u30b8; notranslate\" title=\"\u63a5\u7d9a\u30a4\u30e1\u30fc\u30b8\">\r\n    &#x5B;\u9060\u9694\u5730PC]\r\n      | # \u9006\u5411\u304d\u30c8\u30f3\u30cd\u30eb\u3092\u4f5c\u3063\u3066\u304a\u304f\r\n      | ssh -R 12345:localhost:22 ssh-tunnel@server.example.com\r\n      |\r\n      |\r\n      | # \u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u306e12345\u756a\u30dd\u30fc\u30c8\u3067\u9060\u9694\u5730PC\u306essh\u306b\u30ed\u30b0\u30a4\u30f3\u3067\u304d\u308b\r\n      | ssh -p 12345 user@localhost\r\n    &#x5B;\u4e2d\u7d99\u30b5\u30fc\u30d0]\r\n      |\r\n      |\r\n      | # \u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u306f12345\u756a\u3092\u30dd\u30fc\u30c8\u958b\u653e\u3057\u306a\u3044\u306e\u3067\r\n      | # ssh\u3067\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u306b\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u5185\u90e8\u304b\u3089\u63a5\u7d9a\u3059\u308b\r\n      | ssh hiro@server.example.com\r\n    &#x5B;\u30af\u30e9\u30a4\u30a2\u30f3\u30c8PC]\r\n<\/pre>\n<h4>\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u3067\u306e\u8a2d\u5b9a<\/h4>\n<p style=\"margin-top:2em;\">\n\u306f\u3058\u3081\u306b\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u4e0a\u3067\u6700\u4f4e\u9650\u306e\u8a2d\u5b9a\u3092\u884c\u3046\u3002\u5c02\u7528\u30e6\u30fc\u30b6\u30fc\u3092\u4f5c\u6210\u3057\u3066\u3001\u8d85\u9577\u3047\u30bb\u30ad\u30e5\u30a2\u306a\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u8a2d\u5b9a\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# useradd -s \/usr\/sbin\/nologin ssh-tunnel\r\n# passwd ssh-tunnel\r\n<\/pre>\n<p style=\"margin-top:2em;\">\nssh\u306e\u6a5f\u80fd\u3092\u5236\u9650\u3059\u308b\u305f\u3081ssh\u30b5\u30fc\u30d0\u30fc\u306e\u8a2d\u5b9a\u306b\u4e0b\u8a18\u3092\u8ffd\u8a18\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: plain; title: \/etc\/ssh\/sshd_config; notranslate\" title=\"\/etc\/ssh\/sshd_config\">\r\nMatch User ssh-tunnel\r\n        X11Forwarding no\r\n        AllowTcpForwarding remote\r\n        PermitTTY no\r\n        PermitOpen localhost:22\r\n        ForceCommand echo &#039;Not permitted.&#039;\r\n        ClientAliveInterval 60\r\n        ClientAliveCountMax 1\r\n<\/pre>\n<p style=\"margin-top:2em;\">\nssh\u30b5\u30fc\u30d3\u30b9\u3092\u30ea\u30ed\u30fc\u30c9\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# systemctl reload ssh\r\n<\/pre>\n<h4>\u9060\u9694\u5730PC\u3067\u306e\u8a2d\u5b9a<\/h4>\n<p style=\"margin-top:2em;\">\n\u9060\u9694\u5730PC\u3067\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u306b\u9006\u5411\u304d\u30c8\u30f3\u30cd\u30eb\u3092\u5e38\u6642\u63a5\u7d9a\u3055\u305b\u308b\u305f\u3081sshpass\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# apt install sshpass\r\n<\/pre>\n<p style=\"margin-top:2em;\">\n\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u30b9\u30af\u30ea\u30d7\u30c8\u306b\u8a18\u8ff0\u3059\u308b\u306e\u3067\u3001root\u3057\u304b\u89e6\u308c\u306a\u3044\u3088\u3046\u306b\u30d1\u30fc\u30df\u30c3\u30b7\u30e7\u30f3700\u3067\u63a5\u7d9a\u7528\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3002root\u306ecron\u3067\u6bce\u5206\u5b9f\u884c\u3059\u308b\u306e\u3067\u3001sshpass\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306e\u307f\u63a5\u7d9a\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: plain; title: \/root\/bin\/connect-reverse-ssh-tunnel.sh; notranslate\" title=\"\/root\/bin\/connect-reverse-ssh-tunnel.sh\">\r\n#!\/bin\/bash\r\nPASSWORD=&quot;password&quot;\r\nkillall -0 sshpass &gt; \/dev\/null 2&gt;&amp;1\r\nif &#x5B; $? = 1 ]; then\r\n    sshpass -p &quot;${PASSWORD}&quot; ssh -o &quot;ServerAliveInterval 30&quot; -o &quot;ServerAliveCountMax 3&quot; -o &quot;ConnectTimeout 15&quot; -o &quot;ExitOnForwardFailure yes&quot; -N -R 12345:localhost:22 ssh-tunnel@server.example.com\r\nfi\r\n<\/pre>\n<p style=\"margin-top:2em;\">\ncron\u306b\u30b8\u30e7\u30d6\u3092\u767b\u9332\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n* * * * * \/root\/bin\/connect-reverse-ssh-tunnel.sh\r\n<\/pre>\n<h4>ssh\u306e\u30aa\u30d7\u30b7\u30e7\u30f3<\/h4>\n<table>\n<tr>\n<th style=\"white-space:nowrap;\">-o &#8220;ServerAliveInterval 30&#8221;<\/th>\n<td>30\u79d2\u3054\u3068\u306b\u30b5\u30fc\u30d0\u30fc\u306e\u5fdc\u7b54\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<th style=\"white-space:nowrap;\">-o &#8220;ServerAliveCountMax 3&#8221;<\/th>\n<td>\u30b5\u30fc\u30d0\u30fc\u306e\u5fdc\u7b54\u78ba\u8a8d\u3092\u6700\u59273\u56de\u307e\u3067\u518d\u8a66\u884c\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<th style=\"white-space:nowrap;\">-o &#8220;ConnectTimeout 15&#8221;<\/th>\n<td>15\u79d2\u4ee5\u5185\u306b\u63a5\u7d9a\u3067\u304d\u306a\u3051\u308c\u3070\u30a8\u30e9\u30fc\u7d42\u4e86\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<th style=\"white-space:nowrap;\">-o &#8220;ExitOnForwardFailure yes&#8221;<\/th>\n<td>\u30dd\u30fc\u30c8\u30d5\u30a9\u30ef\u30fc\u30c7\u30a3\u30f3\u30b0\u304c\u78ba\u7acb\u3067\u304d\u306a\u304b\u3063\u305f\u5834\u5408\u306b\u30a8\u30e9\u30fc\u7d42\u4e86\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<th style=\"white-space:nowrap;\">-N<\/th>\n<td>\u30ea\u30e2\u30fc\u30c8\u30b7\u30a7\u30eb\u3092\u5b9f\u884c\u3057\u306a\u3044\u3002<\/td>\n<\/tr>\n<tr>\n<th style=\"white-space:nowrap;\">-R 12345:localhost:22<\/th>\n<td>\u30ea\u30e2\u30fc\u30c8\u30db\u30b9\u30c8\u306e12345\u756a\u30dd\u30fc\u30c8\u306b\u6765\u305f\u63a5\u7d9a\u3092\u3001localhost\u306e22\u756a\u30dd\u30fc\u30c8\u3078\u8ee2\u9001\u3059\u308b\u3002\ufeff<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>\u9060\u9694\u5730\u306ePC\u306fNAT\u5185\u3001\u4e2d\u7d99\u30b5\u30fc\u30d0\u30fc\u306f\u30b0\u30ed\u30fc\u30d0\u30ebIP\u6301\u3061\u3067\u540d\u524d\u89e3\u6c7a\u53ef\u80fd\u3068\u3044\u3046\u524d\u63d0\u3067\u3059\u3002 OS: Ubuntu 24.04 ssh\u306eversion: 9.6 \u30c9\u30e1\u30a4\u30f3\u540d: server.example.com \u7ba1\u7406\u7528\u30e6\u30fc\u30b6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-3019","post","type-post","status-publish","format-standard","hentry","category-ubuntu"],"views":920,"_links":{"self":[{"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=\/wp\/v2\/posts\/3019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3019"}],"version-history":[{"count":63,"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=\/wp\/v2\/posts\/3019\/revisions"}],"predecessor-version":[{"id":3102,"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=\/wp\/v2\/posts\/3019\/revisions\/3102"}],"wp:attachment":[{"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/weblog.hirohiro716.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}