Thursday, February 02, 2023

Write a script to login to Ubuntu server via SSH and reset the root password

 Here is a script in Bash that logs in to an Ubuntu server via SSH and resets the root password:


#!/bin/bash


# Variables for the remote server login

user=<username>

server=<server_ip_or_hostname>


# Connect to the remote server using SSH

ssh $user@$server << EOF

    # Switch to the root user

    sudo su


    # Reset the root password

    echo "root:<new_password>" | chpasswd

EOF


Replace the placeholders <username>, <server_ip_or_hostname>, and <new_password> with the appropriate values for your setup. Then, save the script to a file and make it executable with the chmod +x command. You can then run the script from the terminal.


Note that this script assumes that you have SSH access to the server with a user account that has sudo privileges. Also, be careful when using this script, as it resets the root password, which is a critical operation. Make sure to test the script in a safe environment before using it in production.