Zenity: HOW TO create a user and change its password? - linux

I'm new to zenity and would like to create a GUI for creating a new user and password. My code goes like this:
#!/bin/bash
zenity --info --text='Welcome to Account Creation Wizard'
zenity --entry --text='What do you want to set your username as?' > username
zenity --password > test
cat username >> username1
cat username >> username1
cat test >> test1
cat test >> test1
useradd -m < username1
passwd < username1 < test1
rm test test1
rm username username1
exit
But it does not work. In the terminal, I get this:
[root#archevaris Desktop]# ./UserCreator.sh
Usage: useradd [options] LOGIN
useradd -D
useradd -D [options]
Options:
--badnames do not check for bad names
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
--btrfs-subvolume-home use BTRFS subvolume for home directory
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change default useradd configuration
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-g, --gid GROUP name or ID of the primary group of the new
account
-G, --groups GROUPS list of supplementary groups of the new
account
-h, --help display this help message and exit
-k, --skel SKEL_DIR use this alternative skeleton directory
-K, --key KEY=VALUE override /etc/login.defs defaults
-l, --no-log-init do not add the user to the lastlog and
faillog databases
-m, --create-home create the user's home directory
-M, --no-create-home do not create the user's home directory
-N, --no-user-group do not create a group with the same name as
the user
-o, --non-unique allow to create users with duplicate
(non-unique) UID
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-R, --root CHROOT_DIR directory to chroot into
-P, --prefix PREFIX_DIR prefix directory where are located the /etc/* files
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as the user
New password: Retype new password: passwd: password updated successfully
[root#archevaris Desktop]#
And what it actually did was change my root password :P (This bash script is kinda dangerous to execute but thankfully I did it in a VM snapshot)
So, how to fix this? What changes should I make in the code?

To get a username and password via zenity, this will work:
export INFO=$(zenity --password --username)
$INFO will be split by a pipe, so if your username is "Fred" and your password is "123abc", $INFO will look like this:
Fred|123abc
From here, just use cut to separate the data:
USERNAME=$(echo $INFO | cut -d"|" -f1)
PASSWORD=$(echo $INFO | cut -d"|" -f2)

Related

How to add Multiple user acccount with check and SSH key using Bash script

Based on my requirement in my dev server, I have Created a Linux function that will create a dev user,(Take the user's first name and create a Linux user )
we have a devs group in our developer server, and now want to create a Linux function for 10 plus developers (user)
as a requirement If any of the '$1' directories exist, it would throw a notice to the executor. our developer will have their own key my plan was not to copy the key. we could pass the key in when this is executed, or have this generate the key and output it on completion.
I have tried this code:
#!/bin/bash
CREATE_USER_NAME="pi"
# Create account
echo "============= now create an account ============="
sudo useradd -s /usr/bin/fish -m $CREATE_USER_NAME
sudo usermod -aG devs $CREATE_USER_NAME
sudo passwd $CREATE_USER_NAME
if getent passwd "$1" > /dev/null 2>&1; then
echo "yes the user exists"
else
echo "No, the user does not exist"
fi
mkdir -p /home/santosh/.ssh
chmod 0700 /home/santosh/.ssh
touch /home/santosh/.ssh/authorized_keys
chmod 600 /home/santosh/.ssh/authorized_keys
sudo mkdir -p /var/www/html/santosh-dev.jove.com/
sudo chown -R santosh /var/www/html/santosh-dev.jove.com/
sudo mkdir -p /var/log/httpd/santosh-dev.jove.com/
sudo chown -R santosh /var/log/httpd/santosh-dev.jove.com/
#
Output I Received:
[santosh#skb Desktop]$ ./user1.sh
============= now create an account =============
[sudo] password for santosh:
useradd: user 'pi' already exists
Changing password for user pi.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
No, the user does not exist
what is the best advice here! I'm unable to figure it what is wrong here? can anyone guide me on it!

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.

Reading and creating Username and passsword from CSV File in ubuntu

line=0
cat userDetails.csv |while IFS="," read -r last first grps
do
line=$((line+1))
if [ $line == 1 ]; then
continue
fi
first=echo "$first"|tr 'A-Z' 'a-z'
last=echo "$last"|tr 'A-Z' 'a-z'
grps=echo "$grps"
for groupName in echo $grps
do
ret=getent group $groupName
if [ -z $ret ]
then
groupadd $groupName
fi
done
passwd=cat /dev/urandom | tr -dc 'a-zA-Z0-9'|head -c10
user1=echo "$last"|tail -c6
user2=echo "$first"|head -c2
grps=echo "$grps"
if [ ! -z $grps]
then
useradd -G $grps $user1$user2 -p $password
else
useradd $user1$user2 -p $password
fi
echo “user id for $first is: $user1$user2, password: $password”
done
This is the code that is reading the CSV file and creating the password, when I run it against the csv File that looks a little like
Smith, John
Doe, Jane
etc.. And the errors coming out are this
./adduser3.sh: line 9: John: command not found
./adduser3.sh: line 10: Smith: command not found
./adduser3.sh: line 11: : command not found
./adduser3.sh: line 14: group: command not found
groupadd: group 'echo' already exists
./adduser3.sh: line 20: /dev/urandom: Permission denied
./adduser3.sh: line 21: Smith: command not found
./adduser3.sh: line 22: John: command not found
./adduser3.sh: line 23: : command not found
useradd: option requires an argument -- 'p'
Usage: useradd [options] LOGIN
useradd -D
useradd -D [options]
Options:
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory of the new account
-D, --defaults print or change default useradd configuration
-e, --expiredate EXPIRE_DATE expiration date of the new account
-f, --inactive INACTIVE password inactivity period of the new account
-g, --gid GROUP name or ID of the primary group of the new
account
-G, --groups GROUPS list of supplementary groups of the new
account
-h, --help display this help message and exit
-k, --skel SKEL_DIR use this alternative skeleton directory
-K, --key KEY=VALUE override /etc/login.defs defaults
-l, --no-log-init do not add the user to the lastlog and
faillog databases
-m, --create-home create the user's home directory
-M, --no-create-home do not create the user's home directory
-N, --no-user-group do not create a group with the same name as
the user
-o, --non-unique allow to create users with duplicate
(non-unique) UID
-p, --password PASSWORD encrypted password of the new account
-r, --system create a system account
-R, --root CHROOT_DIR directory to chroot into
-s, --shell SHELL login shell of the new account
-u, --uid UID user ID of the new account
-U, --user-group create a group with the same name as the user
-Z, --selinux-user SEUSER use a specific SEUSER for the SELinux user mapping
--extrausers Use the extra users database
I cannot figure as to why the dev/urandom permission is messing up with the cat and why it is spitting out the errors it is giving me
The first problem is first=echo "$first" not doing what you think.
To define a variable, you need var=val, with no spaces in val (or you need to quote it). There is also var=val command syntax, where var is defined in a sub-environment created to run command (i.e. temporarily, just for command sake). This is what your command does: first it expands $first to John, then you have the command first=echo John, which defines a variable first with value echo for the duration of the command John. And as the error informs you, there is no command John.
The correct syntax would be first=$(echo $first), which runs the command echo $first and assigns its value to first. Or first="$first", which just assigns the value of first to first. Which makes it very obvious that you are doing something completely unnecessary in the first place.
The rest of the code is peppered with the same kind of error, using echo unnecessarily, wrongly, or both, or failing to quote properly. You should also make sure to indent your code correctly in the future, both for the benefit of the potential answerers, and also for your own sanity when debugging.

Linux script for create the user on remote server

I've been able to create a user on the remote server without a problem with the below command.
ssh -i /home/centos/dummy.pem centos#host_ip 'sudo useradd -s /bin/bash -m testuser'
If I try to create a user on a remote server with shell script, it is throwing the error, passing the host_user, host_ip, and new_user argument like
./test.sh "host_user" "host_ip" "new_user"
#!/bin/bash
#Remote server login user
echo host_user=$1
#Remote server hostname
host_name=$2
#user name
user_name=$3
#user public key
#pub_key=$4
ssh $host_user#$host_name -i /home/centos/dummy.pem 'sudo useradd -s /bin/bash -m $user_name'
Expected:
new user on a remote server.
Actual result:
Usage: useradd [options] LOGIN
useradd -D
useradd -D [options]
Options:
-b, --base-dir BASE_DIR base directory for the home directory of the
new account
-c, --comment COMMENT GECOS field of the new account
-d, --home-dir HOME_DIR home directory
And also tried with "-D" in the script but no luck.

Adding sudo permissions to sudoers for user via shell script

I'm trying to create a post install script for Linux and I want to have the script edit the sudoers file so that users wont need to do sudo visudo and edit it manually.
In the script I have:
if [[ ! `sudo -l -U "$user" 2>&1 | grep "ALL"` ]]; then
su -c "echo '$user ALL=(ALL) ALL' >> /etc/sudoers"
su -c "echo '$user ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers"
fi
the problem with this is that when I sudo whoami after I run the script I get this output:
sudo: >>> /etc/sudoers: syntax error near line 31 <<<
sudo: parse error in /etc/sudoers near line 31
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
How do I do this without ruining my sudoers file?
EDIT:
As requested here is my sudoers file:
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
Mind that it is not possible to do cat /etc/sudoers after the script has run.
EDIT 2:
The solution is to define $user as user=$(whoami)
As the comment at the end of the default sudoers file suggests, you should create a new file in /etc/sudoers.d/.
Doing this from a (Debian) package's postinst seems fishy, though. Where does the value of user come from?
Also, any particular reason this user is not simply added to one of the existing groups, admin or sudoers?
My solution is to have the script ask the user to enter his password and store the value in a variable to be used along with Expect. The script installs Expect if it's not installed and then the script does:
read -p "Please enter your password: " PASSWD
export PASSWD
username=$USER
export username
if [[ ! `sudo -l -U "$USER" 2>&1 | grep "ALL"` ]]; then
expect -c '
spawn "su -c \"cat <<EOF >> /etc/sudoers.d/$env(username)
$env(username) ALL=(ALL:ALL) ALL
$env(username) ALL=(ALL) NOPASSWD:ALL
EOF
\"
"
expect "Password:\r"
send $env(PASSWD)
interact
'
fi
You can edit file /etc/sudoers through "pkexec visudo", after when you will delete bad line, sudo will be work.

Resources