Can Claude Code SSH into a server?
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.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
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:
$ 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:
$ 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:
$ 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:
$ 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 explains. Put a key on the host instead; the SSH keys article 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:
$ 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 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 describes. If a task needs root, Claude Code and 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 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. For a server with a database, see also the article on agents and production databases.
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 and on the Claude Code page.
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?
References
- Claude Code, overview and install code.claude.com
- Claude Code, tools reference, the Bash tool code.claude.com
- Claude Code, configure permissions code.claude.com
- Claude Code, connect tools with MCP code.claude.com
- Claude Code, security code.claude.com
- Claude Code, sandboxing code.claude.com
- Claude Code, authentication code.claude.com
- Claude Code, troubleshoot installation and login code.claude.com
- Claude Code, the desktop app, SSH sessions code.claude.com
- ssh(1) on Ubuntu 24.04, the exit status of ssh manpages.ubuntu.com
- ssh_config(5) on Ubuntu 24.04, BatchMode and StrictHostKeyChecking manpages.ubuntu.com
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.