0

I am a beginner at scripting, but I created a text file called text.dat which contains only usernames in the file location /home/daniel/text.dat and I’m trying to run a script(called addusers.sh) which displays the names from that text file and will create a new user account on the system for each new user account specified in the text.dat file.

For each user account that is created, append a line entry to the /home/daniel/logs/useraccounts.log file. The line entry will contain the following information:

added | < user account name > | < day and time of creation >
. This is what I have so far:

 #!/bin/bash 
echo “cat text.dat” 
newuser= grep /home/daniel/text.dat 
sudo adduser $newuser
1
  • Are you adding these users interactively, (ie answering the prompts for user info), or non-interactively?
    – bertieb
    Feb 19 '17 at 3:17
0

How can I add users to a Linux system, as listed in a file?

Something along the lines of the following should do if you want the process to be fully automated (run via eg sudo ./addusers.sh):

#!/bin/bash
# addusers.sh - Add users to system and output a log of same
# run as root or via sudo

usersfile=/home/daniel/text.dat
logfile=/home/daniel/logs/useraccounts.log

while read user; do
   echo "Adding $user"
   adduser --gecos "" --disabled-password $user
   echo "added | $user | `date` |" >> $logfile
done < $usersfile

With options (from the adduser manpage):

--disabled-password

Like --disabled-login, but logins are still possible (for example using SSH RSA keys) but not using password authentication.

(--disabled-login would work too I guess)

--gecos GECOS

Set the gecos field for the new entry generated. adduser will not ask for finger information if this option is given.

You can leave these options to adduser off if you want to specify a password and/or user info.

You can also specify how you want the day and time of creation specified via options to date, eg

$ date "+%Y-%m-%d %H%M"
2017-02-19 0330
2
  • thanks so much for your answer, mostly everything went through but when I ran the script it said ./addusers.sh: line 7: /home/daniel/logs/useraccounts.log:Permission denied adding user account. I also added sudo in front of adduser in the script. Do you know why that would be? Feb 20 '17 at 1:14
  • @DanielWilson it sounds like the line before done < $usersfile is causing a problem; though it works in my (minimal) example. Have you checked the logs file/directory exists and has sensible permissions?
    – bertieb
    Feb 20 '17 at 10:18
0

The event you want to log is already being logged by Linux. Take a look at /var/log/auth.log and try to filter or parse it according to your particular need instead of trying to create your own log file.

Here's a sampling of the log in question:

Feb 19 10:23:47 localhost sudo:    larssend : TTY=pts/1 ; PWD=/home/larssend ; USER=root ; COMMAND=/usr/sbin/useradd -m -s /bin/bash -U tst
Feb 19 10:23:47 localhost sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Feb 19 10:23:47 localhost useradd[1661]: new group: name=tst, GID=1001
Feb 19 10:23:47 localhost useradd[1661]: new user: name=tst, UID=1001, GID=1001, home=/home/tst, shell=/bin/bash
Feb 19 10:23:48 localhost sudo: pam_unix(sudo:session): session closed for user root
Feb 19 10:23:50 localhost sudo:    larssend : TTY=pts/1 ; PWD=/home/larssend ; USER=root ; COMMAND=/usr/sbin/userdel -r tst
Feb 19 10:23:50 localhost sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Feb 19 10:23:50 localhost userdel[1667]: delete user 'tst'
Feb 19 10:23:50 localhost userdel[1667]: removed group 'tst' owned by 'tst'
Feb 19 10:23:50 localhost userdel[1667]: removed shadow group 'tst' owned by 'tst'
Feb 19 10:23:50 localhost sudo: pam_unix(sudo:session): session closed for user root

As you can see, it has pretty much everything you might need to know about user account creation and deletion, including who performed the creation/deletion by way of sudo logging. Note that if the creator didn't use sudo to call useradd or userdel, you may have to perform some extra work to figure out who performed the creation/deletion.

2
  • Please read the question again carefully. Your answer does not answer the original question. OP is trying to create new accounts himself not log existing creations.
    – DavidPostill
    Feb 19 '17 at 12:55
  • @DavidPostill: "For each user account that is created, append a line entry to the /home/daniel/logs/useraccounts.log file." I was advising against exactly that.
    – Larssend
    Feb 20 '17 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.