Contents
- Why replace direct root SSH access with a named account?
- Which permissions and authentication settings are independent?
- What should you inspect before making changes?
- How do you create the administrator account safely?
- How do you install the public key without replacing existing access?
- How do you prove that both key login and sudo work?
- How do you restrict root SSH login in the right configuration file?
- How do you apply the change and verify recovery remains possible?
- Which shortcuts cause lockouts or weaken the setup?
- How does CH-J Tools help with accounts and SSH access?
- Frequently asked questions
Why replace direct root SSH access with a named account?
You have inherited a Linux server where everyone connects as root, or a provider has supplied only an initial administrator login. You want a personal account with an SSH key and working sudo before restricting the old route. The difficult part is keeping a usable way back while changing the access you are currently using.
A separate account makes routine work less likely to run with full privileges accidentally. Sudo associates privileged command requests with the calling account, which helps investigation when each person has their own login. It does not create a complete recording of everything done inside a root shell, and an administrator with unrestricted sudo still has full control.
Keep your existing administrator session open until every fresh-login test passes. Confirm that the provider or hypervisor console is accessible and that you have credentials for it. An open SSH window alone is not a recovery plan if the connection drops.
Which permissions and authentication settings are independent?
Separate three decisions: whether the account exists and can start a session, whether SSH accepts its credentials, and whether sudo permits the required commands. A working SSH key does not grant administrator privileges. Adding someone to an administrator group does not install a key or bypass an SSH access restriction.
- An SSH private key stays on your workstation. Only its public counterpart belongs in the server account's authorized_keys file.
- The account password can remain necessary for sudo even when SSH accepts only keys. A private-key passphrase unlocks the local key and is a different secret.
- PermitRootLogin controls direct SSH sessions as root. It does not remove root, disable sudo, or prevent root access through an approved console.
- PasswordAuthentication and keyboard-interactive authentication are separate SSH methods. A password prompt can still come through PAM-backed keyboard-interactive authentication.
The worked commands below target Ubuntu and Debian with OpenSSH and systemd. On RHEL-family systems, account creation conventions differ, administrator policy commonly uses wheel rather than sudo, and the SSH service is sshd.service. Confirm the local sudoers policy instead of assuming a group name guarantees access.
What should you inspect before making changes?
Run these checks in the existing privileged server session. Here admin1 is the new account and user is your existing working administrator login. Replace both names where appropriate. If getent already finds admin1, inspect its owner, home and purpose before reusing it; the account-creation steps below assume it does not exist.
whoami
getent passwd admin1
sudo -l
sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T | grep -E "^(permitrootlogin|pubkeyauthentication|authorizedkeysfile|passwordauthentication|kbdinteractiveauthentication|authenticationmethods) "
sudo grep -nE "^[[:space:]]*(Include|Match|AllowUsers|AllowGroups|DenyUsers|DenyGroups)" /etc/ssh/sshd_configNo output from sshd -t normally means its configuration and key checks passed; confirm its successful exit status. The grep command examines only the main file, so also inspect files reached through Include. Check existing allow and deny rules before adding an account. A restrictive AllowUsers or AllowGroups rule can reject it even with the right key.
Back up the main SSH configuration and every included file you intend to edit to a root-only location outside the included directory. Record their original paths and the active service name. If the service uses a custom configuration path, use that path for all tests rather than testing the unused default file.
How do you create the administrator account safely?
For a new local account on Ubuntu or Debian, run the following from the existing session. If you are already root and sudo is unavailable, omit the sudo prefix. The interactive adduser command creates the home directory and asks you to set the account password; choose a strong password for subsequent sudo authentication.
sudo adduser admin1
sudo usermod -aG sudo admin1
getent passwd admin1
id admin1The -a in usermod -aG appends supplementary membership. Omitting it can replace existing supplementary groups and remove access the account already needs. Verify the home directory reported by getent; the following key-installation example assumes /home/admin1 and a primary group also named admin1.
Fresh sessions pick up the updated group membership. Do not judge the result from a shell opened before the change. If sudo is not installed or the policy does not grant this group the necessary rights, resolve that through your existing administrator access before proceeding. Use visudo for policy edits, since it checks syntax; do not append an unreviewed rule just to make an error disappear.
How do you install the public key without replacing existing access?
On your workstation, confirm the public-key file exists and belongs to the private key you intend to use. With an OpenSSH client in a POSIX shell on Linux or macOS, upload only that public file through your existing login. Substitute your actual key filename and verified server address; keep normal host-key checking enabled.
ssh-keygen -lf ~/.ssh/id_ed25519.pub
scp ~/.ssh/id_ed25519.pub user@server.example:admin1.pubBack in the existing server session, compare the uploaded public-key fingerprint with the workstation result. For the newly created account only, verify that authorized_keys does not already exist before using install, which replaces a destination file. Stop if the check fails. For an existing account, inspect and preserve its current entries and add the new key on a separate line instead.
Never upload the private key. Replacing an existing authorized_keys file can revoke other administrators or automation, so use the guarded installation below only for the new account.
ssh-keygen -lf ~/admin1.pub
sudo install -d -m 700 -o admin1 -g admin1 /home/admin1/.ssh
if sudo test ! -e /home/admin1/.ssh/authorized_keys; then
sudo install -m 600 -o admin1 -g admin1 ~/admin1.pub /home/admin1/.ssh/authorized_keys
fi
sudo ls -ld /home/admin1 /home/admin1/.ssh /home/admin1/.ssh/authorized_keysThe home directory must not be writable by other users. Keep .ssh owned by admin1 with mode 700 and authorized_keys with mode 600. If the effective AuthorizedKeysFile points elsewhere, install the key at that configured location instead. Fix specific ownership or permission problems rather than disabling StrictModes.
How do you prove that both key login and sudo work?
Open a second terminal on the workstation and make a fresh connection. The options below prevent reuse of a multiplexed connection, select the intended key and prevent fallback to password or keyboard-interactive authentication. A local private-key passphrase prompt is expected if your key is encrypted.
ssh -o ControlMaster=no -o ControlPath=none -o IdentitiesOnly=yes -o PreferredAuthentications=publickey -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -i ~/.ssh/id_ed25519 admin1@server.exampleInside that new server session, check your identity and permissions, clear any cached sudo credential, authenticate again, and run a harmless privileged command. With the usual sudo policy, enter admin1's account password, not the root password or the key passphrase. The last command should print 0 when full administrator access is intended.
whoami
id
sudo -l
sudo -k
sudo -v
sudo id -uIf the login fails, use ssh -v with the same client options and inspect the server journal, for example sudo journalctl -u ssh.service -n 50 --no-pager on Ubuntu or Debian. An offered key is not proof of an accepted key. Resolve the failure now, leaving root policy unchanged.
How do you restrict root SSH login in the right configuration file?
After the independent login and sudo tests succeed, choose the root policy. PermitRootLogin no rejects direct root SSH login. PermitRootLogin prohibit-password disables password and keyboard-interactive authentication for root while allowing public-key authentication subject to other rules. Use no when personal accounts and sudo are your intended administration route.
- Check for backup jobs or other approved processes that still connect as root. Migrate or explicitly review them before removing their route.
- Confirm that /etc/ssh/sshd_config includes /etc/ssh/sshd_config.d/*.conf in global context. If it does not, edit the applicable global setting in the main file instead.
- Inspect existing snippets and back up any file you will change. If 00-local-access.conf already exists, review it rather than replacing it.
- On a server with the confirmed Include, use sudoedit /etc/ssh/sshd_config.d/00-local-access.conf and put the following directive there.
PermitRootLogin noFor most directives the first value read wins; wildcard includes are read in lexical order. A later file is not automatically an override. Match rules also require connection-specific checking. Use the effective output below for root with your actual client address; 198.51.100.25 is only an example. Add host, laddr and lport parameters when your Match rules use those criteria.
sudo /usr/sbin/sshd -t
sudo /usr/sbin/sshd -T -C user=root,addr=198.51.100.25 | grep "^permitrootlogin "Expect permitrootlogin no for the connection you are restricting. These commands test files on disk, not the configuration currently loaded by the running listener. If syntax validation fails or the effective result is different, correct the configuration before reloading anything.
How do you apply the change and verify recovery remains possible?
On Ubuntu and Debian with the standard systemd OpenSSH service, validate again and reload only after success. On RHEL-family systems substitute sshd.service for ssh.service. Check the command result and service status before trying another connection.
sudo /usr/sbin/sshd -t && sudo systemctl reload ssh.service
sudo systemctl status ssh.service --no-pager- From the workstation, repeat the fresh admin1 key-only connection command. In that session repeat sudo -k, sudo -v and sudo id -u.
- Check the root effective policy for each relevant Match context. Where an authorized root key exists, confirm a fresh root connection is rejected; failure without a valid credential alone proves little.
- If the new administrator connection fails, use the retained privileged session or console to restore the backed-up files and undo only the new snippet you created. Validate, reload and retest.
- Close the original session only after the new route and required automation work. Keep console recovery instructions available for future changes.
Which shortcuts cause lockouts or weaken the setup?
Treat disabling password-based SSH as a separate change after every necessary account has a tested key. For a policy that excludes both password and keyboard-interactive login, review PasswordAuthentication no and KbdInteractiveAuthentication no together. Do not apply the latter blindly where keyboard-interactive provides a required second factor. Check AuthenticationMethods for dependencies before changing available methods.
- Do not lock an account password merely to disable SSH passwords. Account-lock behaviour depends on the platform and authentication stack, and you may also lose usable sudo authentication.
- Do not grant unrestricted NOPASSWD just to avoid a prompt. A stolen login key may then be sufficient for immediate administrator control without another secret.
- Do not share one account or private key among administrators. Individual accounts and keys make access removal more precise; document ownership and review membership regularly.
- Do not assume deleting a key ends existing sessions. When revoking access, review active sessions, sudo rights and other credentials separately under your incident procedure.
- Do not change the SSH port, firewall, root policy and authentication methods at once. Apply one reviewed change, validate it and test a new connection before continuing.
How does CH-J Tools help with accounts and SSH access?
The Users module in CH-J Tools manages accounts, groups, SSH keys and SSH access over an active SSH session. It never changes sudoers or sets NOPASSWD. It reports the actual sudo state from sudo -l, and without sudo the module is read-only.
The root account cannot be deleted. Allowing root login with a password requires an extra confirmation, with Key only as the recommended choice. An SSH access change is verified using sshd -T and a new SSH login. If verification fails or the change would lock you out, the previous configuration is restored automatically.
Frequently asked questions
Does PermitRootLogin no disable sudo?
No. It rejects direct SSH login as root. A permitted personal account can still log in and use sudo according to the server's sudo policy.
Why does sudo ask for a password after key login?
SSH and sudo authenticate separately. Under the usual sudo policy you provide your account password. The passphrase protecting your SSH private key does not replace it.
Why does a new sshd_config.d file have no effect?
Check that the main file includes it, that its filename matches the Include pattern, and that an earlier directive or applicable Match rule does not determine another value. Then validate and reload the correct service.
Can I test the new account with su instead of SSH?
Not completely. A local account switch does not exercise SSH key acceptance, network access restrictions or the running listener. Use a separate fresh SSH connection and test sudo inside it.
Should I delete root's authorized_keys immediately?
First migrate approved root-based jobs and verify the new administrator route. PermitRootLogin no provides the SSH restriction without deleting keys. Key removal and cleanup should be a separate reviewed change.
Detailed documentation
OpenSSH: sshd (configuration testing) →
OpenSSH: ssh_config (authentication and connection sharing) →