Operations 3 min read

Using Paramiko to Establish SSH Connections and Switch to Root Privileges

This tutorial explains how to install the Python Paramiko library, create an SSH connection to a remote server using password or key authentication, and then elevate to root privileges with sudo, while highlighting additional capabilities of the module.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Using Paramiko to Establish SSH Connections and Switch to Root Privileges

Paramiko is a Python module that implements the SSH2 protocol, enabling encrypted and authenticated remote connections on platforms such as Linux, Solaris, BSD, macOS, and Windows.

The article demonstrates how to use Paramiko to establish an SSH connection and subsequently obtain root privileges on a server.

Step 1: Install the Paramiko module pip3 install paramiko

Step 2: Login with username and password – after installing the library, create a paramiko.SSHClient() , set missing host key policy, and call client.connect(hostname, port=22, username='your_user', password='your_password') to open the session.

Step 3: Login with an SSH key – generate or provide a private key file and use the pkey argument in client.connect() (e.g., client.connect(hostname, username='user', pkey=paramiko.RSAKey.from_private_key_file('id_rsa')) ).

Step 4: Switch to root privileges – because most servers disable direct root login, after a normal login execute sudo su - (or sudo -i ) via client.exec_command() to become the root user.

Conclusion: Paramiko is a powerful SSH library; beyond the basic connection and privilege escalation shown, it supports SFTP file transfers, concurrent operations with threading, configuration management via configparser , and process title customization with setproctitle . The examples provided are a starting point for more advanced automation.

backendPythonoperationsSSHParamikoremote accessroot privilege
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.