0

I'm new to Ubuntu server and I'm having a little trouble with a project.

I need to add a user account, add a comment, create and set the home directory, add primary group to user, and set the password for the user.

I have no problem doing these individually, but for my project I need to use a single command to create the user with the properties listed.

Can anybody help me with this?

Zanna
  • 69,223
  • 56
  • 216
  • 327
Daniel E
  • 13
  • 2
  • Are the home dir, grp name to the same as the username or are they to be different? – George Udosen Nov 30 '17 at 01:14
  • Same as username (isatssh) home directory should be /home/isatssh, group name should be isatssh – Daniel E Nov 30 '17 at 01:20
  • Have a look [here](https://stackoverflow.com/questions/2150882/how-to-automatically-add-user-account-and-password-with-a-bash-script) and [here](https://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html) – derHugo Nov 30 '17 at 07:07

2 Answers2

3

Assuming that by "a comment" you are referring to an entry in the GECOS field, you can do this using the newusers command. From man newusers:

NAME
       newusers - update and create new users in batch

SYNOPSIS
       newusers [options] [file]

DESCRIPTION
       The newusers command reads a file (or the standard input by default)
       and uses this information to update a set of existing users or to
       create new users. Each line is in the same format as the standard
       password file (see passwd(5)) with the exceptions explained below:

       pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell

Ex.

$ sudo newusers << EOF
bob:12$dta%:::comment:/home/bob:/bin/bash
EOF
[sudo] password for steeldriver: 

Checking

$ getent passwd bob
bob:x:1002:1002:comment:/home/bob:/bin/bash
$ ls -ld /home/bob
drwxr-xr-x 2 bob bob 4096 Nov 29 20:25 /home/bob
steeldriver
  • 131,985
  • 21
  • 239
  • 326
0

You may also have a look at man useradd.

sudo useradd -c 'this is Bob' -d /home/bob -g users -m -p 'aXjeklexjklrewj' bob

(NB: you have to give here the encrypted password)

muclux
  • 5,034
  • 4
  • 20
  • 43