Skip to main content

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.

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()

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.

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()

Non-Interactive Sessions

Non-interactive sessions are useful for running specific commands or scripts without user interaction.

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}")

Port Forwarding

You can forward ports from your instance to your local machine:

# 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