Creating users and assigning passwords on linux - linux

I am creating multiple users (this case 3 users) on a linux bash script using the line
sudo adduser --force-badname CPE_User"$count"
and I am trying to give those users a password that I assign in the script using:
echo "CPE_User"$count":1234" | chpasswd
but the terminal gives me this message :
Allowing use of questionable username.
adduser: The user `CPE_User1' already exists.
Changing password for CPE_User1.
chpasswd: (user CPE_User1) pam_chauthtok() failed, error:
Authentication token manipulation error
chpasswd: (line 1, user CPE_User1) password not changed
What is the problem?

It says it quite clearly: a user with that name already exists. Linux doesn't allow to have more than one user with the same name...

Not sure what the chpasswd problem might be (you are running it as root, right?) If you can't get chpasswd to work, consider using passwd (potentially via an expect script) - that will work for sure.

Related

Missing command prompt user name when a user account is created - Linux - Debian?

I am writing a bash script to create user account with password, that will expire. After creating the user account and then login using su - , I get the prompt, but the user id is missing in the prompt. Also, the tab auto complete is missing. Here is my bash script. Remember, I am using Debian 9 in Windows - WSL2.
#!/bin/bash
# This script creates an account on the local system.
# You will be prompted for the account name and password.
# Ask for the user name.
read -p 'Enter the username to create: ' USER_NAME
# Ask for the real name.
read -p 'Enter the name of the person who this account is for: ' COMMENT
# Ask for the password
read -p 'Enter the password to use for the account: ' PASSWORD
# Create the user
useradd -c "${COMMENT}" -m ${USER_NAME}
# Set the password for the user.
# echo ${PASSWORD} | passwd --stdin ${USER_NAME}
echo "${USER_NAME}:${PASSWORD}" | chpasswd
# Force password change on first login.
passwd -e ${USER_NAME}
After running this, I get a prompt which doesn't has a user-id in it on the left side. Also, the auto completion using tab isn't working. I am a bit surprised, am I doing something wrong here?
Here is what I am seeing.
Add a user with adduser command instead of useradd.
Inscript always user adduser.
tested the same script on the Debian box and it's working fine.

Ansible equivalent of "passwd -l"

I'm trying to lock a user account that I just created with Ansible (it should not be possible to log into this account). I know you can do this using the shell module by running "passwd -l".
Is there a way to do this via the user module, or something similar?
I think that's not possible.
Maybe the following is an option?
- user:
name: someone
shell: /sbin/nologin
I think this is even more secure than using passwd -l as the latter would only disable the password. The user would still be able to login by ssh key authentication.
You can use attribute 'password_lock'
password_lock: yes
^^ This is the equivalent of 'usermod -L'

Automating password into sudo

I'm completely new to the Linux world, but I've been able to complete a few tasks on my own.
Now, I have a task to complete that's driving me crazy.
I need to be able to send the password to sudo, since I can't prompt the user for the password.
What I've been able to find is
echo myPassword | sudo -s
Apparently -s allows sudo to receive the password through command input, and not user input.
I can't render my user so sudo won't ask me for password, since I don't know who will use this script, the script has access to their password and account names, the only thing I need it to be able to automate sudo.
PS: English is not my native language, sorry if I made any mistakes.

Linux user not being able to login (/bin/nologin)

I work on a shared linux enviroment (CentOS), but for some reason one of my logins has been locked.
When I do a cat /etc/passwd | grep "/home", I can find my user:
roaming:x:579:579::/home/roaming:/bin/nologin
I've got root permission but don't know what to do to be able to login again.
What should I do about this 'no login' thing??
The shell for this user is set to a non-existent program in order to prevent user from logging in with interactive shell (ssh, local login). Yet the user can authenticate to do some other stuff like copying files through FTP or SMB.
Just run as a root to put a normal shell back.
chsh roaming /bin/bash
As root, enter
chsh -s /bin/sh roaming

Caching RSA key fingerprint in plink using bash script

I am running an automation script to automate the login and some other commands to be run on a remote target using plink. I used following approach to do a automatic login and saving the RSA key:
user#ubuntu~$ echo -e 'y\n' | plink root#<target ip> -pw <password> "pwd"
This command saves the key when run through command line, but when run using script, is inconsistent in saving the RSA key. Consider username and password being passed as correct, it prompts the error message as the Connection refused, as 'y' is not fetched in the prompt input.
Many times, it will prompt for accepting the key again and again as I have many simultaneous consecutive plink commands used in my script. Ideally, it shouldn't ask for user input more than once. I checked, 'sshhostkeys' file which was not present in ~/.putty folder, which is the cause for prompt for user input each time plink is run.
Has anyone faced this problem earlier? Any fix for this , or any hack/workaround for this?
P.S: Using expect scripts, or manually saving a profile using putty, or manually running the plink command and saving the key for once, is being ruled out (not to be considered).
Got the solution, actually the issue was with permission assigned to the $HOME/.putty directory. The ownership information for the folder was also root, thus when I was trying to run
user#ubuntu~$ echo -e 'y\n' | plink root#<target ip> -pw <password> "pwd"
I was getting the prompt for '(y/n)' repeatedly as the key was not getting saved in .putty folder due to the permission issues. Above command when once run was not able to create file sshhostkeys file due to which it was asking again and again for saving the key, each time it tries to save the key but was not able to save as it didn't have root permission. This issue is resolved by assigning rwx permission for all other (sudo chmod 707 ~/.putty) or other approach can be changing the ownership information to the user running the script by 'chown'.

Resources