SSH
SSH (Secure Shell) provides secure access to your Morph Cloud instances for interactive sessions, command execution, and file transfers. Choose between a direct connection authenticated by one of your user-owned managed SSH public keys, SDK/CLI access, or a separately scoped generated instance credential.
Direct SSH Access
First, add and verify a managed SSH public key in account settings. Then connect with the matching private key on your computer:
ssh -o IdentitiesOnly=yes \
-i ~/.ssh/morph_personal \
morphvm_abc123@ssh.cloud.morph.so
Use the instance ID as the SSH user. On the first connection, verify the Morph SSH gateway host-key fingerprint through a trusted channel before accepting it. Never suppress host-key verification with StrictHostKeyChecking=no.
The public key in account settings is user-owned: it can authorize direct SSH to your instances and instances in organizations where you are currently a member. Morph stores only its public half. It is different from the generated private/public key pair and password for one instance.
Share SSH access (no API key)
If you need to let someone SSH to a specific instance without making their public key part of your user account or giving them your Morph API key, see Share SSH access (no API key). That flow uses a secret instance access bundle, not a managed account public key.
SSH into Instance
You can establish an SSH connection to your running instance for interactive shell access or to execute scripts.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
instance.wait_until_ready() # Ensure instance is ready for SSH
with instance.ssh() as ssh_client:
# Run a command
result = ssh_client.run("ls -l /home")
print(f"Stdout:\n{result.stdout}")
# Start an interactive shell (for interactive sessions, not recommended in scripts)
# ssh_client.interactive_shell()
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function sshAccess() {
const instance_id = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
await instance.waitUntilReady(); // Ensure instance is ready for SSH
const sshClient = await instance.ssh();
try {
// Run a command
const result = await sshClient.execCommand("ls -l /home");
console.log(`Stdout:\n${result.stdout}`);
// For interactive shell in TypeScript, consider using a library that wraps NodeSSH to provide a more shell-like experience if needed for your application.
// NodeSSH's `interactiveShell` is primarily for basic interaction.
} finally {
sshClient.dispose(); // Important to close the SSH connection
}
}
sshAccess();
# SSH directly into the instance
morphcloud instance ssh morphvm_abc123
# Execute a command directly via SSH without interactive login
morphcloud instance ssh morphvm_acb123 "ls -la /tmp"
Interactive vs Non-Interactive Sessions
SSH supports both interactive and non-interactive sessions:
Interactive Sessions
Interactive sessions provide a full shell environment where you can run commands interactively.
- Python
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
with instance.ssh() as ssh_client:
# Start an interactive shell
ssh_client.interactive_shell()
# Start an interactive SSH session
morphcloud instance ssh morphvm_abc123
Non-Interactive Sessions
Non-interactive sessions are useful for running specific commands or scripts without user interaction.
- Python
- TypeScript
- CLI
from morphcloud.api import MorphCloudClient
client = MorphCloudClient()
instance_id = "morphvm_abc123" # Replace with a valid instance ID
instance = client.instances.get(instance_id=instance_id)
with instance.ssh() as ssh_client:
# Run a specific command
result = ssh_client.run("uname -a")
print(f"Stdout: {result.stdout}")
# Run a script with multiple commands
commands = [
"cd /tmp",
"mkdir -p test_dir",
"echo 'Hello World' > test_dir/hello.txt",
"cat test_dir/hello.txt"
]
result = ssh_client.run("; ".join(commands))
print(f"Script output: {result.stdout}")
import { MorphCloudClient } from 'morphcloud';
const client = new MorphCloudClient();
async function sshCommands() {
const instance_id = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instance_id: instance_id });
const sshClient = await instance.ssh();
try {
// Run a specific command
const result = await sshClient.execCommand("uname -a");
console.log(`Stdout: ${result.stdout}`);
// Run a script with multiple commands
const commands = [
"cd /tmp",
"mkdir -p test_dir",
"echo 'Hello World' > test_dir/hello.txt",
"cat test_dir/hello.txt"
].join("; ");
const scriptResult = await sshClient.execCommand(commands);
console.log(`Script output: ${scriptResult.stdout}`);
} finally {
sshClient.dispose();
}
}
sshCommands();
# Run a specific command
morphcloud instance ssh morphvm_abc123 "uname -a"
# Run multiple commands
morphcloud instance ssh morphvm_abc123 "cd /tmp; mkdir -p test_dir; echo 'Hello World' > test_dir/hello.txt; cat test_dir/hello.txt"
Port Forwarding
You can forward ports from your instance to your local machine:
- CLI
# Forward remote port 8080 to local port 8080
morphcloud instance port-forward instance_your_instance_id 8080
# Forward remote port 8080 to local port 9000
morphcloud instance port-forward instance_your_instance_id 8080 9000