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
  • 70,465

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
  • 136,215
  • 21
  • 243
  • 336
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,154