Creating new users through Shell Script

Asked

Viewed 881 times

0

I’m trying to create a shell script to add new users to the GNU/Linux system

But without entering native commands like adduser or useradd in the script, it would be something in the nail.

I have something almost fine(ready!), but I need to share ideas about what can be done.

If you’ve ever tried, or done anything like that, let me know.

  • 4

    Hello Diego, try to describe better what you want to do, that’s why you’re getting the negatives. Another thing, do not need the pendant in the title, here just mark the question as answered.

  • @Intruder the "pending" was not he who entered :) The question was actually closed as pending, see the yellow box (or click edit and see the title field)

  • @Diegohenrique [pending] is automatic system, I did not deny, but closed because I did not understand at first what exactly does not work and how it should work. Maybe you didn’t understand it yourself and so it was unclear, but I’ll try to read and test

  • @Diego Henrique, can edit your question and add what you have so far?

  • I believe you will answer yes, by the way, improve the title, make it clear. ;)

  • Now it’s clearer yes. ;)

Show 1 more comment

1 answer

2


Anyone running a GNU/Linux distro can already try it, or for those who don’t have it, use some Livecd or USB linux platform system.


Code

#!/bin/sh
#
# Por - Diego Henrique "<[email protected]>" 
#
# (c) 2016 Programa - Adicionar novos usuários na base do sistema GNU/Linux
#

ADD_UID=0; ADD_GID=0

for X in `cat /etc/passwd | cut -d ':' -f3`;
do
if [ $X > $ADD_UID ]; then
  ADD_UID=$X
fi
done
ADD_UID=$(($ADD_UID + 1))

for Y in `cat /etc/group | cut -d ':' -f3`;
do
if [ $Y > $ADD_GID ]; then
  ADD_GID=$Y
fi
done

ADD_GID=$(($ADD_GID + 1))


if [ $(id -u) != "0" ]
then

echo -ne "Se você é usuário de um sistema Linux, há arquivos que podem ser bloqueados a você.\nNo caso de arquivos e processos ligados ao funcionamento do sistema, seu proprietário natural é o usuário root. Isso significa que só ele é que pode alterá-los." 
exit 0

elif [ -z $2 ]
then

echo "Use: $0 opções usuário"
exit 0

elif [ "$(cat /etc/passwd | grep -i $1 | wc -l)" = "1" ]
then

echo "Usuário '$1' e seu criador '$2' já existente. Tente outro novamente."
exit 0

fi

if [ $2 = "" ]
then

echo "Favor, coloque o seu nome verdadeiro para o usuário."

elif [ "$(cat /etc/passwd | grep -i $2 | wc -l)" = "2" ]
then
echo "Usuário '$2' já existente. Tente outro novamente."
exit 0
fi

echo "Inclusão de usuário '$1' em /etc/passwd"

echo "$1::$ADD_UID:$ADD_GID:$2:/home/$1:/bin/sh" >> /etc/passwd

sleep 1

echo "Inclusão de usuário '$1' em /etc/group"

echo "$1:x:$ADD_GID:$1" >> /etc/group

if [ ! -d "/home/$1" ]
then

echo "Inclusão de usuário '$1' em /home/$1"
mkdir /home/$1

else

echo "Falha na Inclusão de usuário '$1'. Verifique e tente manualmente."
exit 1

fi

# Alterar proprietário e grupo de arquivos - comandos chown e chgrp

chown -R $1 /home/$1

chgrp -R $1 /home/$1

sleep 1

echo "Pronto! Novo usuário criado com sucesso."

Example


# ./script.sh dhg diego henrique guilherme

Explanation

This little script takes two arguments followed by the user name to create, the "dhg" was used to illustrate the example, along with the true name of its creator "diego Henrique Guilherme" thus forming a new user on the penguin system.

Look how it turned out:

  • /etc/passwd


dhl::502:128:diego henrique guilherme:/home/dhl:/bin/sh


  • /etc/group


dhl:x:114:dhl


Important! - The content available here is so basic than complete. Therefore, it is up to anyone who wishes to make use of it to make appropriate adaptations in accordance with their particular wish.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.