Skip to main content

SSH

SSH (Secure Shell) provides secure access to your Morph Cloud instances for interactive sessions, command execution, and file transfers.

Direct SSH Access

You can directly SSH into any MorphVM instance using standard SSH clients with your public key:

ssh <morphvm>@ssh.cloud.morph.so

This method works just like SSHing into any other VM. Your SSH public key must be registered with Morph Cloud first. This is the same as using the SDK or CLI methods described below, but gives you the flexibility of using your preferred SSH client directly.

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