Last active
September 4, 2023 13:34
-
-
Save mrioan/16ec5ca46212447b5c9d8302cbce3e37 to your computer and use it in GitHub Desktop.
Script to create users in an Asus router (OpenWRT) to login via SSH for instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
echo -e "\nUsage: create_user.sh <username> <password>" | |
USERNAME=$1 | |
PASSWORD=$2 | |
echo "Creating user ${USERNAME}" | |
if [ ! -d "/home/${USERNAME}" ] | |
then | |
echo "Directory /home/${USERNAME} does not exist. Let's continue normally." | |
mkdir /home/${USERNAME} | |
chown ${USERNAME} /home/${USERNAME} | |
echo "${USERNAME}:x:200:200:${USERNAME}:/home/${USERNAME}:/bin/sh" >> /jffs/configs/passwd.add | |
echo "${USERNAME}:@ZZZZ@:0:0:99999:7:0:0:" >> /jffs/configs/shadow.add | |
echo "${USERNAME}:x:200:" >> /jffs/configs/group.add | |
echo "${USERNAME}:*:200:" >> /jffs/configs/gshadow.add | |
echo "${USERNAME}:x:200:200:${USERNAME}:/home/${USERNAME}/:/bin/sh" >> /etc/passwd | |
echo "mkdir /home/${USERNAME}" >> /jffs/configs/users_to_add_on_every_reboot.sh | |
echo "chown ${USERNAME} /home/${USERNAME}" >> /jffs/configs/users_to_add_on_every_reboot.sh | |
else | |
echo "Directory /home/${USERNAME} already exists. Let's only update the password." | |
sed -i.bak "/${USERNAME}:/d" /jffs/configs/shadow.add | |
echo "${USERNAME}:@ZZZZ@:0:0:99999:7:0:0:" >> /jffs/configs/shadow.add | |
fi | |
echo "${USERNAME}:$PASSWORD" | sudo chpasswd | |
ENCRYPTED_PASSWORD=`cat /etc/passwd | grep -Po "${USERNAME}:\K(.*)(?=:200:200:)" | head -n 1` | |
#echo "New encrypted password $ENCRYPTED_PASSWORD" | |
sed -i "s/${USERNAME}:@ZZZZ@/${USERNAME}:${ENCRYPTED_PASSWORD}/g" /jffs/configs/shadow.add | |
if [ $? -eq 0 ]; then | |
echo -e "All done\n" | |
else | |
echo -e "- IMPORTANT ERROR. PLEASE READ - User action is required: The last command (sed) failed since it is likely that the generated encrypted password has a '/'. You must either a) re-run this script (the time-based seed will create a different password), or b) copy the encrypted password from /etc/passwd and paste it into /jffs/configs/shadow.add replacing @ZZZZ@\n" | |
fi |
Enabling SUDO (root)
echo 'root:x:0:0:root:/root:/bin/sh' >> /jffs/configs/passwd.add
opkg install sudo
chmod u+w /opt/etc/sudoers
Now, we need to paste a text line inside sudoers
file. So, first:
vi /opt/etc/sudoers
Now, paste this line inside User alias specification section
admin ALL=(ALL:ALL) ALL
Now you can run a command with sudo
Great, works now. Thanks a million!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to fix