How-to

Create a User on Linux, the Right Way

useradd is on every Linux system and does exactly what its flags say. That makes it the right tool for scripts and for service accounts.

What is the safest useradd command for a person?

Spell out every important setting, because defaults differ between distributions:

useradd -m -s /bin/bash -c "Ines Duarte, developer" -G developers ines
passwd ines
id ines

-m creates the home folder; -s sets the login shell; -c stores a comment so you know who the account belongs to; -G adds extra groups. Without -m and -s, Debian's useradd creates no home folder and uses /bin/sh, while Rocky Linux and AlmaLinux create a home and use bash. Explicit flags give the same result everywhere. Debian also offers adduser, a friendly interactive wrapper, compared with useradd elsewhere in these guides.

Where do defaults come from?

SourceControls
/etc/login.defsUID and GID ranges, home creation on Enterprise Linux, password aging defaults
/etc/default/useraddDefault shell, home base, inactive days, skeleton folder
/etc/skel/Files copied into every new home folder

Show the effective defaults with useradd -D. Put shared shell settings, a standard .bashrc or an editor config into /etc/skel, and every new account starts with them.

How do I set a fixed UID?

Use -u. Matching UIDs matters when files move between machines, for example with rsync between a web box and a backup box, or when container volumes expect a specific number:

useradd -m -u 2101 -U -s /bin/bash builder

-U creates a group with the same name, and with most defaults the same number. Check that the UID is free first with getent passwd 2101. Keeping a small table of UIDs for your team across all machines saves ownership headaches later.

How do I create a service account?

A service account runs a program, never logs in and needs no password:

useradd --system --home-dir /var/lib/imgproxy --create-home --shell /usr/sbin/nologin --comment "image proxy" imgproxy

--system picks a UID below 1000 and skips password aging. On Rocky Linux and AlmaLinux, the nologin shell lives at /sbin/nologin; both paths work there because /sbin points to /usr/sbin. Then run the program as that user, for example with User=imgproxy in its systemd unit. If a process is compromised, it only has the rights of this one account.

For services shipped with their own unit files, DynamicUser=yes in systemd is an alternative: systemd creates a temporary user each time the service starts.

How do I make a temporary account?

Add an expiry date when creating it:

useradd -m -s /bin/bash -e 2027-01-31 -c "Audit contractor" audit-ext

After that date the account cannot log in by any method, including SSH keys. Extend or remove expiry later with chage -E. Lifecycle tasks such as locking and deleting are covered in Linux user management.

How should the new user log in?

With an SSH key. A password set with passwd is still useful for sudo prompts, but SSH logins should use keys once password authentication is switched off. Install the key with correct ownership:

install -d -o ines -g ines -m u=rwx,go= /home/ines/.ssh
printf '%s\n' 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM4... ines@mbp' | install -m 600 -o ines -g ines /dev/stdin /home/ines/.ssh/authorized_keys

On SELinux systems add restorecon -Rv /home/ines/.ssh. Key creation is explained in Log in with an SSH key; granting admin rights in Create a sudo user on Debian and Sudo user on CentOS Stream, Rocky, Alma.

What about FreeBSD?

FreeBSD 14 uses pw instead of useradd, plus an interactive adduser script:

pw useradd -n ines -m -s /bin/sh -c "Ines Duarte" -G wheel
passwd ines

Membership in wheel allows su to root on FreeBSD; for sudo, install the package and enable the wheel rule in /usr/local/etc/sudoers.

How do I confirm everything is right?

getent passwd ines
id ines
ls -ld /home/ines

The first line shows UID, home and shell; the second lists groups; the third shows the home folder's owner and mode. Five seconds of checking avoids a confusing first login.

Keep Reading

Linux user management

The full life of an account on a server: create, group, change, expire, lock, audit and delete, with the files behind each step.

Open

Create a sudo user on Debian

Add an admin account on Debian 13 or 12, give it sudo rights, install your SSH key and retire daily root logins safely.

Open

Log in with an SSH key

Create an ed25519 key, install it on your machine through the panel or ssh-copy-id, and log in without typing the root password.

Open

FreeBSD

FreeBSD 14 with ZFS in the base system and a famously solid network stack, on x86 and Arm.

Open

FAQ

Quick Answers

Use useradd -m, ideally with -s /bin/bash for the shell, then set a password with passwd.

Debian does not create homes by default with useradd. Add -m, or use adduser.

Use useradd --system with --shell /usr/sbin/nologin for service accounts.

Pass -u with the number, after checking it is free with getent passwd.

Yes. Add -e with a date such as 2027-01-31 when creating it, or use chage -E later.