# Can Claude Code SSH into a server?

Claude Code can run ssh from its shell, run on the server, or use an MCP server. What the agent reaches in each, where your key sits, with real ssh output.

Source: https://valakes.com/learn/claude-code-ssh/

Last updated: 2026-11-29

Yes. Claude Code has a shell, so it can run ssh host 'command' from your machine. It can also run on the server itself, and the desktop app sets that up with its SSH sessions. What the agent can reach, and where your key ends up, depends on the setup you choose.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 29 November 2026.

Yes. Claude Code has a shell tool, and `ssh app 'df -h'` is a shell command, so it can reach every server that your own account can reach with ssh. Claude Code can also run on the server itself, and the desktop app has SSH sessions that put it there. There are three setups, and they differ in what the agent can reach and in where your key ends up.

I did not run Claude Code for this article. What it says about Claude Code comes from the documentation pages listed below, read on 2026-11-29. The ssh output ran on a real client against two server containers: `app`, which takes your key, and `legacy`, which takes only a password.

## ssh from the shell of Claude Code

In the first setup Claude Code runs on your machine, and its Bash tool starts `ssh` like any other program. The tools reference says the Bash tool runs each command in a separate process. The permissions page says that in Manual mode a Bash command asks for approval unless it is in a built-in set of read-only commands, and the set it lists does not include ssh. Other permission modes change what asks, so check which one your session is in.

Your key stays where it is, in `~/.ssh` and your ssh-agent, and ssh reads it when the command starts. The agent reaches every host that your ssh config and your keys reach. A command it runs can also read the key file: the sandboxing page says a sandboxed command can read credential files such as `~/.ssh` by default, and it shows a rule that denies that. The same page says plain `ssh` cannot connect from inside the sandbox, because it ignores the proxy settings.

Here is what a command of this kind prints against a server that accepts your key. `BatchMode=yes` tells ssh never to ask a question:

```console
$ ssh -o BatchMode=yes deploy@app 'df -h /dev/shm'; echo "exit status: $?"
Filesystem      Size  Used Avail Use% Mounted on
shm              64M     0   64M   0% /dev/shm
exit status: 0
```

ssh printed nothing of its own. The output is what `df` wrote on the server. The exit status comes back as well. The ssh manual says ssh exits with the status of the remote command, and with 255 if ssh itself failed:

```console
$ ssh -o BatchMode=yes deploy@app 'ls /srv/missing'; echo "exit status: $?"
ls: cannot access '/srv/missing': No such file or directory
exit status: 2
```

The 2 is the status of `ls` on the server. The status of the remote command comes back as the status of ssh, so a script or an agent that checks it sees the failure.

## A host that wants a password

The second server accepts passwords and does not know your key. The same kind of command gets a refusal, and the status is 255, because this time ssh failed and the command never ran:

```console
$ ssh -o BatchMode=yes deploy@legacy 'df -h /dev/shm'; echo "exit status: $?"
deploy@legacy: Permission denied (publickey,password).
exit status: 255
```

The words in parentheses list the methods the server offers. Without `BatchMode`, and with no terminal and no input, as when a program starts ssh in the background, ssh asks and gets no answer:

```console
$ setsid -w ssh deploy@legacy 'df -h /dev/shm' < /dev/null; echo "exit status: $?"
Permission denied, please try again.
Permission denied, please try again.
deploy@legacy: Permission denied (publickey,password).
exit status: 255
```

Do not solve this by typing the password into the chat. A password in a prompt becomes part of what the model sees, as [the article on AI agents on servers](https://valakes.com/learn/ai-agents-on-servers/) explains. Put a key on the host instead; [the SSH keys article](https://valakes.com/learn/ssh-keys/) shows how.

## A host key nobody has confirmed

The first time ssh meets a host it has no record of, it asks you to confirm the fingerprint. Here `UserKnownHostsFile` points at an empty file, which is what a machine that never connected to `app` looks like, and nothing can answer the question:

```console
$ setsid -w ssh -o UserKnownHostsFile=/tmp/new-known-hosts deploy@app 'df -h /dev/shm' < /dev/null; echo "exit status: $?"
Host key verification failed.
exit status: 255
$ ssh -o BatchMode=yes -o UserKnownHostsFile=/tmp/new-known-hosts deploy@app 'df -h /dev/shm'; echo "exit status: $?"
Host key verification failed.
exit status: 255
```

Both ways end with the same line and the same status, and no key was accepted on your behalf. Check the fingerprint yourself, once, and let ssh save it; [the host key article](https://valakes.com/learn/host-key-changed/) shows how to read one.

## Claude Code on the remote server

The second setup moves the agent to the server. You log in with ssh yourself, with your key or your password at your own keyboard, install Claude Code there with the Linux install command of the overview page, and run `claude` in that session.

The desktop app does this from a window. Its documentation calls it an SSH session: Claude Code runs on the remote machine, and the desktop app is the interface. Desktop connects by running the OpenSSH client on your computer, so it uses your `~/.ssh/config`. The connection takes a host, a port and an optional path to a private key, and with the key left empty it uses your SSH config or your SSH agent. Desktop installs Claude Code on the remote machine the first time you connect. On a host it has not seen, it asks whether to trust the host key, and the page tells you to check the key first. For a server that sets `MaxSessions 1`, the page advises a login that works without a prompt, such as a key your SSH agent holds, because Desktop logs in at least twice each time it connects.

Either way the agent reaches only that machine, as the account of the login. Your SSH key stays on your computer, where the ssh client used it. What sits on the server is Claude Code's own login. The authentication page says that on Linux the credentials are stored in `~/.claude/.credentials.json` with mode 0600, so anyone who can read that account's files can read them. The troubleshooting page adds that in an SSH session the browser may open on the wrong machine, and that you copy the login URL from the terminal and open it on your own computer.

The agent can read and write whatever that account can, and run `sudo` if the account may. Use an account made for it, with no sudo, as [the article on AI agents on servers](https://valakes.com/learn/ai-agents-on-servers/) describes. If a task needs root, [Claude Code and sudo](https://valakes.com/learn/claude-code-sudo/) covers it.

## An SSH MCP server

The third setup gives Claude Code a tool that makes the SSH connection. The MCP page says that stdio servers run as local processes on your machine, and you add one with `claude mcp add`. The ssh client now lives in that program, so your key is wherever that program reads it from: your `~/.ssh`, an environment variable, or a setting.

The same page says where the settings of a server are kept. The local and user scopes go in `~/.claude.json`, and the project scope goes in `.mcp.json` in the project root, which is shared through version control. A key path or a password written into `.mcp.json` can go into the repository with it. The security page says Anthropic does not security-audit MCP servers, and the MCP page tells you to verify that you trust each one.

What the agent reaches is what the tools of that server allow. A tool that runs any command is as wide as the first setup. You can set Claude Code permissions for MCP servers, as the security page says. Read what each tool returns: a server that never hands back the key keeps it out of the conversation, and one that can print the key file does not.

## Mistakes people make

You allow a broad rule to stop the prompts. The permissions page says a `*` in a rule stands in for whatever text is in its place, so a rule such as `Bash(ssh app *)` lets every remote command run without asking. [Auto-approving agent commands](https://valakes.com/learn/auto-approve-agent-commands/) covers what to allow and what not.

You treat a deny rule as a wall. The same page says a rule matches the command as written and is not a security boundary. What limits the agent is the account on the server.

You turn off host key checking so that the error goes away. `StrictHostKeyChecking=no` also lets ssh go on when the key of a known host has changed, and that check is what tells you the machine behind the address is a different one.

You keep no record. After the agent has run commands on a server you will want to read what they were; see [the audit trail article](https://valakes.com/learn/agent-audit-trail/). For a server with a database, see also [the article on agents and production databases](https://valakes.com/learn/ai-agent-production-database/).

## In Valakes

Valakes is the third setup, with the connection held by an app on your computer. Claude Code connects to it as an MCP server named `valakes`, added with one `claude mcp add` command. The Help says the agent cannot read a password, a key or a token, and that it sees only hosts you opened to agents. It can never accept a host key: on a key that is not trusted yet the call fails, and you open the host yourself once. The setup is in [the Claude Code guide](https://valakes.com/guides/let-claude-code-run-commands-without-a-password/) and on [the Claude Code page](https://valakes.com/agents/claude-code/).

## In Valakes

The guide connects Claude Code to Valakes as an MCP server, so Valakes holds the SSH connection and Claude Code never gets a saved password or key. [Guide: How do I let Claude Code work on my server without giving it the password?](https://valakes.com/guides/let-claude-code-run-commands-without-a-password/)

[Read the docs: Agents](https://docs.valakes.com/agents/)

## References

- [Claude Code, overview and install](https://code.claude.com/docs/en/overview)
- [Claude Code, tools reference, the Bash tool](https://code.claude.com/docs/en/tools-reference)
- [Claude Code, configure permissions](https://code.claude.com/docs/en/permissions)
- [Claude Code, connect tools with MCP](https://code.claude.com/docs/en/mcp)
- [Claude Code, security](https://code.claude.com/docs/en/security)
- [Claude Code, sandboxing](https://code.claude.com/docs/en/sandboxing)
- [Claude Code, authentication](https://code.claude.com/docs/en/authentication)
- [Claude Code, troubleshoot installation and login](https://code.claude.com/docs/en/troubleshoot-install)
- [Claude Code, the desktop app, SSH sessions](https://code.claude.com/docs/en/desktop)
- [ssh(1) on Ubuntu 24.04, the exit status of ssh](https://manpages.ubuntu.com/manpages/noble/man1/ssh.1.html)
- [ssh_config(5) on Ubuntu 24.04, BatchMode and StrictHostKeyChecking](https://manpages.ubuntu.com/manpages/noble/man5/ssh_config.5.html)

## Related articles

- [How do I give an AI agent SSH access with the least damage?](https://valakes.com/learn/ai-agents-on-servers/): Give the agent its own account on the server, with no sudo, no password in the prompt and nothing secret in its reach. Let it read first, allow root for one exact command at most, make every change wait for a person, and keep a record of what ran.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I fix "REMOTE HOST IDENTIFICATION HAS CHANGED" in SSH?](https://valakes.com/learn/host-key-changed/): The server now shows a different host key than the one saved in your known_hosts file. Check the new fingerprint with the server's owner, remove the old entry with ssh-keygen -R, and connect again.
- [How does Codex connect to a server over SSH?](https://valakes.com/learn/codex-ssh/): Codex, in the ChatGPT desktop app, finds hosts in your ~/.ssh/config, resolves them with OpenSSH and starts its remote app server over SSH. Its documentation shows a key and never mentions a password, so move a host that asks for one to a key first.
