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.
- 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 instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
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 instanceId = "morphvm_abc123"; // Replace with a valid instance ID
const instance = await client.instances.get({ instanceId: instanceId });
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