Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

A Smart Bash Script to Reset Remote User Passwords — With Automatic Sudo Mode Detection

I had a requirement to reset some user password on Linux remotely, below is the simple script which helps to reset password locally, it uses simple passwd command. Save below file as reset_password.sh.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/bin/bash

if [ -z "$1" ]; then
    echo "Usage: $0 <username>"
    exit 1
fi

USER="$1"
NEW_PASS=$(openssl rand -base64 16 | sed -E 's/(.)\1+/\1/g')
echo "$NEW_PASS" | sudo passwd --stdin "$USER"

if [ $? -eq 0 ]; then
    echo "Password for user '$USER' has been reset successfully."
    echo "New Password: $NEW_PASS"
else
    echo "Failed to reset the password for user '$USER'."
    exit 2
fi

I wanted to use above shell script remotely, to execute it, this script file will be copied on remote Linux server and then it will execute the script remotely. This script requires sshpass application to be preinstalled. Next make changes on the line 8 and 9 providing remote server ssh username and password. This script requires 2 parameters provided while execution as shown example below.

./main-script.sh <remote_ip> <target_user_password_need_to_reset>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash

if [ $# -ne 2 ]; then
    echo "Usage: $0 <remote_ip> <target_user>"
    exit 1
fi

SSH_PASS="xxxxxxxxxx"
SSH_USER="unixsupport"
REMOTE_IP="$1"
TARGET_USER="$2"

# Copy script to remote host
sshpass -p "$SSH_PASS" scp -o StrictHostKeyChecking=no \
    /root/sftp/reset_password.sh "$SSH_USER@$REMOTE_IP:/tmp"

# Detect if sudo requires password
SUDO_CHECK=$(sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no "$SSH_USER@$REMOTE_IP" "sudo -n true" 2>&1)

# If sudo requires password
if echo "$SUDO_CHECK" | grep -q "a password is required"; then
    echo "🔐 sudo requires password — using sudo -S"
    sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no \
        "$SSH_USER@$REMOTE_IP" "echo \"$SSH_PASS\" | sudo -S /tmp/reset_password.sh $TARGET_USER"

# If sudo does NOT require password
else
    echo "✅ sudo does NOT require password — running directly"
    sshpass -p "$SSH_PASS" ssh -o StrictHostKeyChecking=no \
        "$SSH_USER@$REMOTE_IP" "sudo /tmp/reset_password.sh $TARGET_USER"
fi

Useful Articles
Getting started Ansible AWX tower for IT automation run first playbook
Ansible for VMwary Using vmware_vm_inventory dynamic inventory plugin
Ansible selectattr The error was TemplateRuntimeError no test named 'equalto'
ansible create an array with set_fact
Ansible get information from esxi advanced settings nested dictionary with unique keynames
Install Ansible AWX Tower on Ubuntu Linux
Ansible AWX installation error Cannot have both the docker-py and docker python modules
Ansible AWX installation error docker-compose run --rm --service-ports task awx-manage migrate --no-input
docker: Got permission denied while trying to connect to the Docker daemon socket
Ansible AWX Tower create Manual SCM (Source Control Credential Type) project
Reset Ansible AWX Tower admin password
Install Ansible AWX on Microsoft Windows OS
Step by Step Install Ansible on Ubuntu OS
Install Ansible AWX Tower on Ubuntu Linux OS
Ansible AWX Tower Github inventory integration | Github inventory source

Go Back

Comment

Blog Search

Page Views

13869636

Archive

Follow me on Blogarama