Create a Non-Root User on Debian Linux
The recommended way to create a non-root user on a server is passwordless: the account has no usable password at all, you log in with an SSH key, and sudo is granted without a password prompt. There is no password to guess, phish, or reuse, and nothing to type when you sudo.
Do all of this while logged in as root (or via sudo), and keep that root session open until you have verified the new login works.
Step-by-step instructions
-
Create the user with no password.
sudo adduser --disabled-password --gecos "" username--disabled-passwordcreates the account and home directory but never sets a password, so password logins are impossible while key-based logins still work.--gecos ""skips the interactive "Full Name / Room Number" questions. -
Install your public key for the new user. You'll need a key pair on your local machine — see Set Up SSH Key Authentication if you don't have one yet.
Because the account has no password,
ssh-copy-idcan't authenticate yet, so push the key through your existing root session. Run this from your local machine:ssh root@server "install -d -m 700 -o username -g username /home/username/.ssh && \cat >> /home/username/.ssh/authorized_keys && \chown username:username /home/username/.ssh/authorized_keys && \chmod 600 /home/username/.ssh/authorized_keys" < ~/.ssh/id_ed25519.pubOr, if you're already sitting at a root shell on the server, paste the key in by hand:
install -d -m 700 -o username -g username /home/username/.sshnano /home/username/.ssh/authorized_keys # paste the .pub line, one key per linechown username:username /home/username/.ssh/authorized_keyschmod 600 /home/username/.ssh/authorized_keysSSH is strict about permissions:
.sshmust be700,authorized_keysmust be600, and both must be owned by the user — otherwise the key is silently ignored. -
Grant passwordless sudo. A user with no password can't answer a
sudopassword prompt, so the sudo rule has to beNOPASSWD. Put it in its own file under/etc/sudoers.d/rather than editing/etc/sudoers:echo "username ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/usernamesudo chmod 0440 /etc/sudoers.d/usernamesudo visudo -cf /etc/sudoers.d/usernameThe last command validates the syntax and must print
parsed OK. A broken sudoers file can lock you out ofsudoentirely, so never skip it. The filename must not contain a dot or end in~, or sudo will ignore the file.Adding the user to the
sudogroup is not needed with this rule, but is harmless if you prefer the group for consistency:sudo usermod -aG sudo username -
Verify the login from a second terminal, while the root session stays open:
ssh username@serversudo whoamisudo whoamishould printrootwith no prompt. -
Turn off password authentication for SSH (recommended). Once key login works, stop the server from accepting passwords at all. On Debian 12 and newer, drop a file into
/etc/ssh/sshd_config.d/:sudo tee /etc/ssh/sshd_config.d/99-hardening.conf <<'EOF'PasswordAuthentication noKbdInteractiveAuthentication noPermitRootLogin prohibit-passwordEOFsudo sshd -t && sudo systemctl restart sshsshd -ttests the config before the restart.PermitRootLogin prohibit-passwordkeeps root reachable by key (useful for recovery) while blocking root password logins; usenoif you want root SSH shut off completely — but only after confirming your non-root user works.On Debian 13,
sshdis socket-activated; if a config change doesn't seem to take effect, restart the socket instead:sudo systemctl restart ssh.socket
Key considerations
- "SSH cert" vs. SSH key: this guide uses ordinary public-key authentication — a key pair, with the public half in
authorized_keys. True SSH certificates are keys signed by an SSH certificate authority (ssh-keygen -s), which is worth the extra machinery when you're managing many users or hosts, but is overkill for a single server. - Keep a recovery path. A passwordless account can't log in at a physical or virtual console, and can't be reached with
su - usernamefrom another non-root account. Make sure you retain root console access (Proxmox console, cloud provider serial console, hypervisor terminal) before disabling password logins. - Passwordless sudo is a deliberate trade-off. It means anyone holding your private key has instant root. That's an acceptable trade for a single-admin homelab or personal server; on a shared or higher-risk machine, set a strong password with
sudo passwd usernameand drop theNOPASSWD:prefix instead. adduservs.useradd: both exist, butadduseris preferred on Debian as it is an interactive wrapper that sets up home directories and default configurations automatically, whereasuseraddis a lower-level, non-interactive tool more suited to scripting.- Docker: if the user needs to manage Docker without root, see this Reddit discussion.
Sources
- https://manpages.debian.org/stable/adduser/adduser.8.en.html
- https://manpages.debian.org/stable/openssh-server/sshd_config.5.en.html
- https://manpages.debian.org/stable/sudo/sudoers.5.en.html
- https://www.youtube.com/watch?v=LbJK48gvXcA
- https://www.youtube.com/watch?v=GlCDsvMyv9Y
- https://www.webhi.com/how-to/how-to-add-and-delete-users-on-centos-ubuntu-debian-redhat/
- https://www.reddit.com/r/debian/comments/1nm1j75/linux_debian_13_how_to_grant_a_user_permissions/
- https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-debian-8
- https://jumpcloud.com/blog/how-to-create-sudo-users-debian
- https://serverfault.com/questions/295113/run-script-from-ssh-as-non-root-linux
- https://unix.stackexchange.com/questions/182180/why-is-the-home-directory-not-created-when-i-create-a-new-user