I am trying to automate setting up a user on centos as part of my script. Here is what i am doing:-
/usr/sbin/useradd my-user
/usr/bin/passwd my-user
You will be be prompted to enter password for the my-user user
Now is there a way to skip the second command so that there is no manual prompt, so that i can have something in the script to auto create my-user on centos without needing to manually enter the password
You can change password programmatically, using:
echo -e "xyz\nxyz\n" | /usr/bin/passwd my-user
You can use chpasswd command with echo. Here is a simple example
/bin/echo my-user:password | /usr/sbin/chpasswd
Related
I am new to Linux. I want to switch to root user from a script file without prompting for password. I am writing this line
echo 'password' | sudo -S su
above runs nicely but does not change user to root user. I have seen that -S does not change user so i tried -i as well but then it prompts for password. Please help
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.
Dears,
I want script to change root password with a predefined password for Solaris 10,11, AIX, Linux without prompting to enter password.
#!/bin/sh
echo -e "password\npassword" | passwd
Just run this as root.
However, there are some serious security concerns about keeping your password in plain text so be careful.
Is there any suitable shell script for shutting down or restarting a Linux machine? I have tried a shell script for shutdown, but when I enter sudo shutdown it will ask for the password. How we can enter the password using the script?
Another, in my opinion cleaner approach:
Create a new file in /etc/sudoers.d/ with content:
%users ALL=NOPASSWD: /sbin/shutdown
%users ALL=NOPASSWD: /sbin/reboot
This causes sudo to not ask for the password, if any user of group "users" tries to execute a shutdown or reboot. Of course you can also specify another group, maybe a newly created group for finer control of reboot permissions.
More information about the other possible settings for sudo can be found in the Manpage.
Yes, use the -S switch which reads the password from STDIN:
$echo <password> | sudo -S <command>
So to shut down the machine, your command would be like this (just replace <password> with your password):
$echo <password> | sudo -S poweroff
Exposing your password is generally bad idea search for something that can protect / hide it. In the past I've used Jenkins plugins to do this while executing the scripts regularly.
if you really want to achieve it, you should write a script containing the shutdown command; make root be its owner, then set the SUID bit with the chmod command and give to it executable permission for everybody. When executed, the owner of the script would become root and no password should be asked.
I want to change the user in one command line with su
echo password | su user
But this is not working.
Is there a other possibility?
best wishes
First of all: Ask yourself whether it is really necessary for a script to know the password. sudo and SE-Linux usually give you plenty of options to avoid entering passwords.
This being said, su opens its controlling terminal to read the password, not stdin. You can use sudo with the -S option to read the password from stdin.
From sudo's manual:
-S
The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character.
Do the folowing ..
passwd root
it will ask you for a new password
put it and then you log as administrator and do what you want