Script Linux pentru adaugare user si parola

Configurare noua (How To)

Situatie

Vrem sa adaugam un user local de linux si sa ii asignam o parola .

Solutie

Pasi de urmat

Deschidem terminalul si scriem urmatoarea comanda :

pico adaugareuser.sh

si introducem urmatorul script :

#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
	read -p "Enter username : " username
	read -s -p "Enter password : " password
	egrep "^$username" /etc/passwd >/dev/null
	if [ $? -eq 0 ]; then
		echo "$username exists!"
		exit 1
	else
		pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
		useradd -m -p $pass $username
		[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
	fi
else
	echo "Only root may add a user to the system"
	exit 2
fi

Salvam fisierul, apoi rulam comanda:

chmod +x adaugareuser.sh

executam scriptul in felul urmator:

./adaugareuser.sh

 

Dupa adaugare cu succes al utilizatorul va aparea :

Tip solutie

Permanent

Voteaza

(22 din 38 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?