{"id":3057,"date":"2026-11-19T12:20:20","date_gmt":"2026-11-19T12:20:20","guid":{"rendered":"https:\/\/hostry.com\/blog\/?p=3057"},"modified":"2026-11-19T12:20:20","modified_gmt":"2026-11-19T12:20:20","slug":"github-actions-deploy-vps-restricted-ssh-key","status":"publish","type":"post","link":"https:\/\/hostry.com\/blog\/github-actions-deploy-vps-restricted-ssh-key\/","title":{"rendered":"GitHub Actions Deploy to a VPS: Limit What a Stolen SSH Key Can Do"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-cover.webp\" alt=\"GitHub Actions deploy to a VPS with a restricted SSH key: every gate to the server is barred except one turnstile that lets a single release through\" class=\"wp-image-3054\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-cover.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-cover-300x169.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-cover-1024x576.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-cover-450x253.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-cover-768x432.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\" style=\"font-size:14px\"><em><strong>If your GitHub Actions deploy to a VPS uses an SSH key from a repository secret, check that key's line in authorized_keys. Most guides add it as a bare line, which gives anyone holding the private key a shell, file access and tunnels. With restrict,command= in front of the line, the same stolen key runs one deploy script that accepts one release tag.<\/strong><\/em><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The script in this setup switches the VPS to a release that is already on the server, and the restricted key cannot upload one. We recommend adding a fetch step to that script yourself: the server downloads the release for the validated tag from a release artifact or a read-only source it already trusts, so the CI key never needs write access.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the key in your deploy secret may have been read by someone else, through the wfx-loot campaign or any other leak, these six steps replace it with a key that can do one job:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong><a href=\"#new-user\">Create a new ed25519 key and a dedicated user<\/a><\/strong> on the VPS, with no sudo wildcard and no docker group membership.<\/li><li><strong><a href=\"#key-line\">Add the public key with restrict,command=<\/a><\/strong> pointing at a root-owned script, plus an expiry-time that ends in Z.<\/li><li><strong><a href=\"#allow-list\">Install the allow-list script<\/a><\/strong>, which accepts only <code>deploy vX.Y.Z<\/code> and checks it with bash&#8217;s <code>[[ =~ ]]<\/code>, because grep lets a newline through.<\/li><li><strong><a href=\"#host-key\">Pin the server&#8217;s host key<\/a><\/strong> in a secret and connect with <code>StrictHostKeyChecking=yes<\/code>; remove <code>ssh-keyscan<\/code> and <code>StrictHostKeyChecking=no<\/code> from the workflow.<\/li><li><strong><a href=\"#environment\">Move both secrets into a protected environment<\/a><\/strong> that only release tags can deploy to.<\/li><li><strong><a href=\"#check-log\">Rotate the old key out and search the SSH log<\/a><\/strong> for its fingerprint to see what it was used for.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Outputs below were reproduced with OpenSSH 9.6p1 (Ubuntu package 1:9.6p1-3ubuntu13.20) on Ubuntu 24.04, server and runner in separate network namespaces, with the runner also playing the thief. Output blocks keep the lab&#8217;s port 2222, user gha-deployer and \/opt\/gha-lab paths. Nothing in the lab contacted GitHub, so GitHub&#8217;s side comes from its documentation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"stolen-key\">What a stolen GitHub Actions deploy key can do on your VPS<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With a bare <span class=\"highlighted\">authorized_keys<\/span> line the key in the secret is a full login, and a forced command on its own removes the shell but leaves the tunnels open.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That key is what wfx-loot went after. From about 12:45 UTC on November 10, 2026, the campaign used stolen GitHub credentials to push code that ran in repositories&#8217; workflows and collected the secrets and variables those workflows could read, with SSH material on its target list. The <strong><a href=\"https:\/\/labs.cloudsecurityalliance.org\/research\/csa-research-note-wfx-github-ci-secret-stealer-20261112-csa\/\" target=\"_blank\" rel=\"noopener\">Cloud Security Alliance research note on the wfx campaign<\/a><\/strong> cites StepSecurity: the attack &#8220;exploits no vulnerability in GitHub Actions itself&#8221;; the tokens were real, just stolen.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To make a GitHub Actions SSH deploy secure, first see what a thief gets. The table shows one private key, used from the runner against three versions of the key line.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>What the thief tries<\/th><th>Bare key line<\/th><th>command= only<\/th><th>restrict,command=<\/th><\/tr><\/thead><tbody><tr><td>Interactive shell (<code>ssh -tt<\/code>)<\/td><td>Shell on \/dev\/pts\/0<\/td><td>Forced command runs on \/dev\/pts\/0<\/td><td>PTY allocation request failed on channel 0<\/td><\/tr><tr><td>Any command (<code>id; echo CMD_RAN<\/code>)<\/td><td>Runs<\/td><td>Forced into the script<\/td><td>Forced into the script<\/td><\/tr><tr><td>scp upload, sftp<\/td><td>File lands in \/tmp<\/td><td>Forced into the script, fails<\/td><td>Forced into the script, fails, no file written<\/td><\/tr><tr><td>-L, -W, -D to 127.0.0.1:8080<\/td><td>HTTP 200<\/td><td>HTTP 200<\/td><td>administratively prohibited, HTTP 000<\/td><\/tr><tr><td>-R listener on the VPS<\/td><td>Listening on 127.0.0.1:19090<\/td><td>Listening on 127.0.0.1:19090<\/td><td>remote port forwarding failed for listen port 19090<\/td><\/tr><tr><td>Agent forwarding (-A)<\/td><td>Runner&#8217;s agent key listed<\/td><td>SSH_AUTH_SOCK set<\/td><td>SSH_AUTH_SOCK unset<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The middle column matters to anyone who adds only a forced command. With <code>command=<\/code> alone, an HTTP request through each tunnel type reached the test service on the VPS&#8217;s 127.0.0.1:8080 with HTTP 200, and <code>ssh -N -R<\/code> opened a listener on the VPS at 127.0.0.1:19090. The sshd(8) manual in this package warns about it:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>the client may specify TCP and\/or X11 forwarding unless they are explicitly prohibited<\/p><cite>sshd(8), openssh-server 1:9.6p1-3ubuntu13.20<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">You can check what the server granted from the client side. After authentication, <code>ssh -v<\/code> prints a <span class=\"highlighted\">key options<\/span> line: <code>agent-forwarding port-forwarding pty user-rc x11-forwarding<\/code> for the bare line in the lab, only <code>command<\/code> for <code>restrict,command=<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-key-lines.webp\" alt=\"Comparison of what a stolen deploy key can do with a bare authorized_keys line, with command= only, and with restrict,command=\" class=\"wp-image-3048\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-key-lines.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-key-lines-300x200.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-key-lines-1024x683.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-key-lines-450x300.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-key-lines-768x512.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>Same key, three key lines: only restrict,command= closes the tunnels and agent forwarding as well as the shell.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"new-user\">A new key and a user that owns nothing else<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Start with a fresh key pair and a user that exists only for deploys. Options on the old key&#8217;s line would limit its future logins, but a copy of that key may already be elsewhere. A comment naming the repository and purpose will show up next to the fingerprint later.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># on your workstation\nssh-keygen -t ed25519 -N '' -C 'ci: example-org\/shop-api deploy' -f shop-ci-ed25519\n\n# on the VPS, as root\nuseradd --create-home --shell \/bin\/bash shopci\ninstall -d -m 700 -o shopci -g shopci \/home\/shopci\/.ssh<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The name shopci, the address 192.0.2.10 and the key comment are stand-ins for your own. Keep the account out of the docker group: Docker&#8217;s <strong><a href=\"https:\/\/docs.docker.com\/engine\/install\/linux-postinstall\/\" target=\"_blank\" rel=\"noopener\">Linux post-installation guide<\/a><\/strong> warns that &#8220;the docker group grants root-level privileges to the user&#8221;. Key-only login and the base setup are in our notes on <strong><a href=\"https:\/\/hostry.com\/blog\/secure-new-linux-vps-first-30-minutes\/\">locking a new server down to SSH keys<\/a><\/strong>. If a restart needs root, give sudoers one exact command line, since a trailing <code>*<\/code> also matches extra units and flags:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># visudo -f \/etc\/sudoers.d\/shopci\nshopci ALL=(root) NOPASSWD: \/usr\/bin\/systemctl restart shop-api.service\n\n# what the rule permits\nsudo -l -U shopci \/usr\/bin\/systemctl restart shop-api.service<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"key-line\">The restrict,command= line in authorized_keys<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The changes from here on are root work on the server. On a <strong><a href=\"https:\/\/hostry.com\/products\/vps\/\">Hostry Linux VPS<\/a><\/strong> you hold root for all of it, and if a LogLevel edit or a key change locks out your SSH login, open the panel&#8217;s VNC console and undo the last change from there.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Write one line to <span class=\"highlighted\">\/home\/shopci\/.ssh\/authorized_keys<\/span>, owned by shopci with mode 600. Paste the full contents of <code>shop-ci-ed25519.pub<\/code> after the options:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>restrict,expiry-time=\"20270301Z\",command=\"\/usr\/local\/bin\/shop-release\" ssh-ed25519 AAAAC3Nza...rest-of-the-public-key... ci: example-org\/shop-api deploy<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>restrict<\/code> turns off port, agent and X11 forwarding, PTY allocation and <span class=\"highlighted\">~\/.ssh\/rc<\/span>, including any restriction added in future versions. <code>command=<\/code> runs the script whatever the client asked for. Since no PTY is allowed, keep <code>-t<\/code> and <code>-tt<\/code> out of the workflow&#8217;s ssh call: the lab&#8217;s <code>ssh -tt<\/code> got <code>PTY allocation request failed on channel 0<\/code>, exit 255, and the script never ran.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>expiry-time as a rotation deadline<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>expiry-time=<\/code> arrived in <strong><a href=\"https:\/\/www.openssh.org\/txt\/release-7.7\" target=\"_blank\" rel=\"noopener\">OpenSSH 7.7<\/a><\/strong> (April 2018), and 9.6p1 honors it, so a key you forget to rotate stops working on a date you pick. The man page reads times in the system time zone unless they end in Z, and in practice that meant the sshd process&#8217;s zone. Three debug sshd instances, differing only in environment, got the same line <code>expiry-time=\"202611191125\"<\/code> without Z at 10:55 UTC:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### debug sshd started with: env -i                       -&gt; deployed v1.4.2\n### debug sshd started with: env -i TZ=Europe\/Chisinau    -&gt; Permission denied (publickey).\n                                                             debug1: ...authorized_keys:1: entry expired at 2026-11-19T11:25:00\n### debug sshd started with: env -i TZ=UTC                -&gt; deployed v1.4.2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Under Europe\/Chisinau, two hours ahead of UTC, 11:25 had already passed. Write the value with Z, as in <code>20270301Z<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-copied-key.webp\" alt=\"A copied key held up to a server whose main lock is covered, while one narrow slot accepts only a sealed release envelope\" class=\"wp-image-3052\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-copied-key.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-copied-key-300x169.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-copied-key-1024x576.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-copied-key-450x253.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-copied-key-768x432.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"allow-list\">An allow-list script for SSH_ORIGINAL_COMMAND<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The forced command receives the client&#8217;s request as one string in <span class=\"highlighted\">SSH_ORIGINAL_COMMAND<\/span>. Its job is to match the whole string against one pattern, refuse everything else and never hand any part of it to a shell. This script accepts <code>deploy<\/code> plus a version such as v1.4.2, rebuilds the tag from the captured numbers and switches a <code>current<\/code> symlink to a release already on the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n# \/usr\/local\/bin\/shop-release: forced command for the CI key.\n# Accepts exactly \"deploy vMAJOR.MINOR.PATCH\". Everything else is refused.\nset -euo pipefail\numask 027\nPATH=\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin\nAPP=\/home\/shopci\/app\nLOG=\/home\/shopci\/log\/release.log\nreq=\"${SSH_ORIGINAL_COMMAND-}\"\nlog() { printf '%s %s from=[%s] req=%q\\n' \"$(date -u +%FT%TZ)\" \"$1\" \"${SSH_CONNECTION%% *}\" \"$req\" &gt;&gt; \"$LOG\"; }\n\nre='^deploy v(0|[1-9][0-9]{0,3})\\.(0|[1-9][0-9]{0,3})\\.(0|[1-9][0-9]{0,3})$'\nif [[ ! \"$req\" =~ $re ]]; then\n  log REJECT\n  echo \"refused: expected 'deploy vX.Y.Z'\" &gt;&amp;2\n  exit 64\nfi\ntag=\"v${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}\"\nif [[ ! -d \"$APP\/releases\/$tag\" ]]; then\n  log NO_SUCH_RELEASE; echo \"refused: release $tag not present\" &gt;&amp;2; exit 65\nfi\nln -sfn \"releases\/$tag\" \"$APP\/current.new\"\nmv -T \"$APP\/current.new\" \"$APP\/current\"\nlog OK\necho \"deployed $tag\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>install -o root -g root -m 755 shop-release \/usr\/local\/bin\/shop-release\ninstall -d -o shopci -g shopci \/home\/shopci\/app \/home\/shopci\/app\/releases \/home\/shopci\/log<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The script is root-owned so the deploy user cannot edit it, and it sets <code>PATH<\/code> because a non-interactive session starts with a minimal one. <code>ln<\/code> and <code>mv<\/code> sit on separate lines because <code>set -e<\/code> ignores a failure on the left of <code>&amp;&amp;<\/code>, and the script would report a deploy that never happened. The lab staged <span class=\"highlighted\">releases\/v1.4.2<\/span> beforehand; if your script fetches releases, it should use <code>$tag<\/code>, rebuilt from the matched digits.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the result from your workstation with the new key; a request the script refuses is enough. With expiry-time in the line, the options read <code>commandexpires<\/code>, and the line appears twice:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -v -i shop-ci-ed25519 -o IdentitiesOnly=yes shopci@192.0.2.10 'check' 2&gt;&amp;1 | grep 'key options'<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>debug1: Remote: \/opt\/gha-lab\/home\/gha-deployer\/.ssh\/authorized_keys:1: key options: commandexpires\ndebug1: Remote: \/opt\/gha-lab\/home\/gha-deployer\/.ssh\/authorized_keys:1: key options: commandexpires<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The lab&#8217;s probes against this script ran through this client config, where <code>gha-vps<\/code> stands for the full connection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host gha-vps\n  HostName 192.0.2.10\n  Port 2222\n  User gha-deployer\n  IdentityFile \/opt\/gha-lab\/runner\/ci_deploy_ed25519\n  IdentitiesOnly yes\n  UserKnownHostsFile \/opt\/gha-lab\/runner\/known_hosts\n  StrictHostKeyChecking yes\n  BatchMode yes<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The probes returned these results, condensed to one row per request:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### ssh gha-vps deploy\\ v1.4.2            deployed v1.4.2                        exit=0\n### ssh gha-vps deploy\\ v9.9.9            refused: release v9.9.9 not present    exit=65\n### ssh gha-vps deploy\\ v1.4.2\\;\\ id      refused: expected 'deploy vX.Y.Z'      exit=64\n### ssh gha-vps deploy\\ \\$\\(id\\)          refused: expected 'deploy vX.Y.Z'      exit=64\n### ssh gha-vps deploy\\ \\`id\\`            refused ...                            exit=64\n### ssh gha-vps deploy\\ v1.4.2\\ \\&amp;\\&amp;\\ id  refused ...                            exit=64\n### ssh gha-vps deploy\\ v1.4.2\\|id        refused ...                            exit=64\n### ssh gha-vps deploy\\ v01.4.2           refused ...                            exit=64\n### ssh gha-vps 'deploy v1.4.2 '          refused ...                            exit=64\n### ssh gha-vps deploy\\ ..\/..\/..\/etc      refused ...                            exit=64\n### ssh gha-vps $'deploy v1.4.2\\nid'      refused ...                            exit=64\n### ssh gha-vps (no command)              refused ...                            exit=64<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to <code>%q<\/code>, the script&#8217;s log shows a newline as <code>$'...\\n...'<\/code> instead of splitting the entry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2026-11-19T10:52:20Z OK from=[192.0.2.20] req=deploy\\ v1.4.2\n2026-11-19T10:52:21Z REJECT from=[192.0.2.20] req=deploy\\ v1.4.2\\;\\ id\n2026-11-19T10:52:23Z REJECT from=[192.0.2.20] req=$'deploy v1.4.2\\nid'\n2026-11-19T10:52:24Z REJECT from=[192.0.2.20] req=''<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\" style=\"font-size:14px\"><strong>Why grep is the wrong validator<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A common shortcut pipes the variable into <code>grep -qE '^deploy v[0-9]+\\.[0-9]+\\.[0-9]+$'<\/code>. The anchors look strict, but grep matches line by line, and ssh delivers a newline inside the command string to the server unchanged. The lab&#8217;s comparison script printed a verdict and executed nothing; it was fed <code>deploy v1.4.2; id<\/code>, then <code>$'deploy v1.4.2\\nid'<\/code>, then <code>$'id\\ndeploy v1.4.2'<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>naive grep check: rejected\nnaive grep check: ACCEPTED ... d e p l o y   v 1 . 4 . 2 \\n i d\nnaive grep check: ACCEPTED ... i d \\n d e p l o y   v 1 . 4 . 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A script that passed such a request on to a shell would run the extra line. Bash&#8217;s <code>[[ =~ ]]<\/code> anchors to the whole string, so the script&#8217;s pattern refused both multi-line requests above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"host-key\">StrictHostKeyChecking in GitHub Actions: pin the host key<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Store the server&#8217;s host key line as a secret and connect with <code>StrictHostKeyChecking=yes<\/code>. In the lab that setup refused an impostor server, as did <code>accept-new<\/code> with the same pinned file; <code>StrictHostKeyChecking=no<\/code> and an <code>ssh-keyscan<\/code> at job start both connected to it and ran the deploy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the test, a second sshd with a different host key replaced the real one on the same address and port. It held the deploy key so the command could complete; a real machine in the path could accept any key or relay. An empty known_hosts with <code>StrictHostKeyChecking=no<\/code> simply added the impostor and deployed. The other cases:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### 5b ssh-keyscan at job start, then StrictHostKeyChecking=yes\n[192.0.2.10]:2222 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGmNfOQGEgsZdEi4X\/ayBVQtUFmRsL8QTuFmejleSFxj\ndeployed v1.4.2\nexit=0\n### 5c pinned real key in known_hosts, but StrictHostKeyChecking=no\n@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @\n[...]\nPassword authentication is disabled to avoid man-in-the-middle attacks.\nKeyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.\ndeployed v1.4.2\nexit=0\n### 5d pinned real key from secret, StrictHostKeyChecking=yes\n@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @\n[...]\nHost key for [192.0.2.10]:2222 has changed and you have requested strict checking.\nHost key verification failed.\nexit=255<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Case 5b looks careful, but a fresh runner has nothing to compare against, so the scan trusted whoever answered. In case 5c the client printed the warning banner, disabled password and keyboard-interactive login, then authenticated with the key and deployed with exit 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take the line from the server itself, over a session you already trust or the VPS console, and compare the fingerprint before you store it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># on the VPS\nssh-keygen -lf \/etc\/ssh\/ssh_host_ed25519_key.pub\nprintf '192.0.2.10 %s\\n' \"$(cut -d' ' -f1,2 \/etc\/ssh\/ssh_host_ed25519_key.pub)\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The second command prints the value for the secret. If sshd listens on a port other than 22, write the host as <code>[192.0.2.10]:2222<\/code>; the lab&#8217;s mismatch tests used that bracketed form.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"environment\">The workflow and a protected environment<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Move the private key and the host key line from repository secrets to an environment&#8217;s secrets, and allow only release tags to deploy to that environment. GitHub&#8217;s <strong><a href=\"https:\/\/docs.github.com\/en\/actions\/reference\/security\/secure-use\" target=\"_blank\" rel=\"noopener\">secure use reference<\/a><\/strong> spells out why repository secrets are a weak spot: &#8220;Any user with write access to your repository has read access to all secrets&#8221;. Environment secrets are released to a job only after the environment&#8217;s protection rules pass.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>name: release\non:\n  push:\n    tags: ['v*.*.*']\njobs:\n  ship:\n    runs-on: ubuntu-24.04\n    environment: production\n    steps:\n      - name: Ask the VPS to switch to this tag\n        env:\n          CI_KEY: ${{ secrets.SHOP_CI_ED25519 }}\n          HOST_LINE: ${{ secrets.SHOP_VPS_HOST_LINE }}\n        run: |\n          install -d -m 700 \"$HOME\/.ssh\"\n          printf '%s\\n' \"$CI_KEY\" &gt; \"$HOME\/.ssh\/shop-ci\"\n          chmod 600 \"$HOME\/.ssh\/shop-ci\"\n          printf '%s\\n' \"$HOST_LINE\" &gt; \"$HOME\/.ssh\/shop-known-hosts\"\n          ssh -i \"$HOME\/.ssh\/shop-ci\" -p 22 -o IdentitiesOnly=yes -o BatchMode=yes \\\n              -o StrictHostKeyChecking=yes \\\n              -o UserKnownHostsFile=\"$HOME\/.ssh\/shop-known-hosts\" \\\n              shopci@192.0.2.10 \"deploy $GITHUB_REF_NAME\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The tag travels in <code>$GITHUB_REF_NAME<\/code>, an environment variable, so it is never pasted into the script text, and the forced command checks it again. Change <code>-p 22<\/code> if sshd listens on another port.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Environments and required reviewers depend on your plan. In a private repository, environments need GitHub Pro, Team or Enterprise, and GitHub&#8217;s <strong><a href=\"https:\/\/docs.github.com\/en\/actions\/how-tos\/deploy\/configure-and-manage-deployments\/manage-environments\" target=\"_blank\" rel=\"noopener\">guide to managing environments<\/a><\/strong> notes that on Free, Pro and Team required reviewers &#8220;are only available for public repositories&#8221;. In the environment&#8217;s settings, limit deployment branches and tags to tags matching <code>v*.*.*<\/code>. If your plan offers required reviewers, add them with Prevent self-review and clear the <em>Allow administrators to bypass configured protection rules<\/em> box.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Anyone who can create a release tag or push to the deployed branch still ships code through this script. Control who creates release tags and, where your plan allows, keep a reviewer on the environment, which is also where the CSA note wants deploy-capable secrets.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-deploy-path.webp\" alt=\"Deploy path from a GitHub job in a protected environment over SSH with a pinned host key to a restrict,command= script, with a stolen key limited to valid release requests\" class=\"wp-image-3049\" srcset=\"https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-deploy-path.webp 1200w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-deploy-path-300x200.webp 300w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-deploy-path-1024x683.webp 1024w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-deploy-path-450x300.webp 450w, https:\/\/hostry.com\/blog\/wp-content\/uploads\/2026\/11\/github-actions-deploy-vps-restricted-ssh-key-deploy-path-768x512.webp 768w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>A stolen key that reaches the script can still send a valid request and switch the server to a release that already exists.<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"from-expiry\">GitHub Actions runner IP allowlist: where from= fits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>from=<\/code> only when the deploy comes from a self-hosted runner or one with a fixed address. GitHub&#8217;s <strong><a href=\"https:\/\/docs.github.com\/en\/actions\/reference\/runners\/github-hosted-runners\" target=\"_blank\" rel=\"noopener\">hosted runner reference<\/a><\/strong> says its address list is updated weekly and &#8220;we do not recommend that you use these as allowlists&#8221;, pointing to larger runners with static IPs or self-hosted runners. A self-hosted runner can also reach sshd over a private tunnel, as in our guide to <strong><a href=\"https:\/\/hostry.com\/blog\/ssh-over-wireguard-ubuntu-24-04\/\">reaching sshd only through WireGuard<\/a><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Where it fits, <code>restrict,from=\"192.0.2.20\",command=...<\/code> refused the same key from a second address on the runner. The client cannot tell that refusal from a wrong key, while sshd&#8217;s log names the reason:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gha-deployer@192.0.2.10: Permission denied (publickey).\n\n\/opt\/gha-lab\/home\/gha-deployer\/.ssh\/authorized_keys:1: Authentication tried for gha-deployer with correct key but not from a permitted host (host=192.0.2.21, ip=192.0.2.21, required=192.0.2.20).\nRefused by key options<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"check-log\">Rotate a leaked deploy key and check the SSH log<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Rotate in this order so deploys keep working, then read the log for what the old key did:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Install the new restricted line and the new secret, then let one tagged release go through.<\/li><li>Delete the old key&#8217;s line from every authorized_keys file it sits in.<\/li><li>Search the log for the old fingerprint, and treat each login you cannot match to a workflow run as the thief&#8217;s.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Every successful login prints the key&#8217;s SHA256 fingerprint, even at Ubuntu&#8217;s default <code>LogLevel INFO<\/code>. That is all INFO records, though. In the baseline run, the log did not show which command ran. Interactive shells, sftp uploads and -L, -R, -D or agent forwards had no separate entries.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FP=$(ssh-keygen -lf old-ci-key.pub | awk '{print $2}')    # or run ssh-keygen -lf on the server's authorized_keys\nsudo journalctl -u ssh --since 2026-11-10 | grep -F \"$FP\" | grep -oE '(Accepted|Failed) publickey for [^ ]+ from [^ ]+' | sort | uniq -c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>ssh-keygen -lf<\/code> also reads a whole authorized_keys file, options and all, if the old public key is gone. The count matches on the message text, so the timestamp and <code>sshd[pid]:<\/code> prefix of journald or <span class=\"highlighted\">\/var\/log\/auth.log<\/span> lines do not break it; the lab logged to a plain file, so journald itself was not checked. At INFO the search finds successful logins only: the refused <code>from=<\/code> attempt from 192.0.2.21 wrote no Failed line with the fingerprint.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For the new key, put <code>LogLevel VERBOSE<\/code> into <span class=\"highlighted\">\/etc\/ssh\/sshd_config.d\/60-loglevel.conf<\/span> and run <code>sudo sshd -t &amp;&amp; sudo systemctl reload ssh<\/code>. VERBOSE adds a Failed line with the fingerprint for refused uses, the authorized_keys line that matched, and a line for each forced command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Accepted key ED25519 SHA256:MagTN5...jA4 found at \/opt\/gha-lab\/home\/gha-deployer\/.ssh\/authorized_keys:1\nAccepted publickey for gha-deployer from 192.0.2.20 port 51218 ssh2: ED25519 SHA256:MagTN5...jA4\nUser child is on pid 2698\nStarting session: forced-command (key-option) '\/opt\/gha-lab\/bin\/gha-probe.sh' for gha-deployer from 192.0.2.20 port 51218 id 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At VERBOSE, the lab&#8217;s plain <code>sshd -E<\/code> log counted the refused attempt too:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ grep -F \"$FP\" sshd.log | grep -E '^(Accepted|Failed) publickey' | awk '{print $1,$6,$7}' | sort | uniq -c\n      3 Accepted 192.0.2.20 port\n      1 Failed 192.0.2.21 port<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A tunnel-only login with <code>ssh -N<\/code> never starts the forced command, so the script&#8217;s log misses it. Under <code>command=<\/code> without restrict such a tunnel worked and left no other trace. In VERBOSE it still appears as an Accepted line with no &#8220;Starting session&#8221; for that source port, which this awk lists:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ awk -v fp=\"$FP\" '\n\/Accepted publickey\/ &amp;&amp; index($0, fp) { for (i = 1; i &lt; NF; i++) if ($i == \"port\") login[$(i-1) \" port \" $(i+1)] = 1 }\n\/Starting session:\/ { for (i = 1; i &lt; NF; i++) if ($i == \"port\") cmd[$(i-1) \" port \" $(i+1)] = 1 }\nEND { for (k in login) print k, (k in cmd ? \"-&gt; session: forced-command\" : \"-&gt; NO SESSION (no command, no shell)\") }' 06-verbose-sshd.log | sort\n192.0.2.20 port 51218 -&gt; session: forced-command\n192.0.2.20 port 51220 -&gt; NO SESSION (no command, no shell)\n192.0.2.20 port 51224 -&gt; NO SESSION (no command, no shell)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Port 51220 was a tunnel that <code>restrict<\/code> refused, also logged as <code>refused local port forward<\/code>; port 51224 was allowed under <code>command=<\/code> alone and carried HTTP traffic.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>A bare authorized_keys line turns the SSH key in a GitHub Actions secret into a shell with tunnels. With restrict,command=, an allow-list script and a&#8230;<\/p>\n","protected":false},"author":6,"featured_media":3055,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,127,20],"tags":[],"class_list":["post-3057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-virtual-private-server","category-security","category-system-admin"],"read_time":11,"_links":{"self":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts\/3057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/comments?post=3057"}],"version-history":[{"count":1,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts\/3057\/revisions"}],"predecessor-version":[{"id":3063,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/posts\/3057\/revisions\/3063"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/media\/3055"}],"wp:attachment":[{"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/media?parent=3057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/categories?post=3057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hostry.com\/blog\/wp-json\/wp\/v2\/tags?post=3057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}