Using SSH keys for authentication is far more secure than using passwords. Paramiko supports this by loading a private key from a file and passing it to the connect() method. You must first ensure your public key is in the ~/.ssh/authorized_keys file on the remote server.
This script connects to a server using a private key file.
import paramiko
import os
# --- Connection Details ---
hostname = 'your_server_ip_or_hostname'
username = 'your_username'
# Path to your private key file (e.g., id_rsa, id_ed25519)
private_key_path = os.path.expanduser('~/.ssh/id_rsa')
# --- Load Private Key ---
try:
private_key = paramiko.RSAKey.from_private_key_file(private_key_path)
# For other key types, use:
# private_key = paramiko.Ed25519Key.from_private_key_file(private_key_path)
# private_key = paramiko.ECDSAKey.from_private_key_file(private_key_path)
except paramiko.PasswordRequiredException:
key_password = getpass.getpass('Enter password for private key: ')
private_key = paramiko.RSAKey.from_private_key_file(private_key_path, password=key_password)
except Exception as e:
print(f"❌ Failed to load private key: {e}")
exit()
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
print(f"Connecting to {hostname} using SSH key...")
# --- Connect using the private key object ---
client.connect(
hostname=hostname,
username=username,
pkey=private_key
)
print("✅ Connection successful!")
# Execute a simple command to verify
stdin, stdout, stderr = client.exec_command('whoami')
print(f"Authenticated as: {stdout.read().decode('utf-8').strip()}")
except Exception as e:
print(f"❌ An error occurred: {e}")
finally:
client.close()Generate a new SSH key pair on your local machine using ssh-keygen -t ed25519. Copy the public key (~/.ssh/id_ed25519.pub) to your test server's ~/.ssh/authorized_keys file. Modify today's script to use this new key for authentication.