SSH to Ubuntu (Passwordless + sudo)
Operator guide: use an SSH key on macOS to log into a remote Ubuntu or Debian account without a password, then run privileged commands with sudo. The commands are the same on both; only the default account name and whether that account already has sudo may differ. Key creation and ssh-agent live on OpenSSH Client Keys. This page is host login and privilege — not GitHub or GitLab.
Prerequisites
Section titled “Prerequisites”- A key pair on the Mac. Prefer a dedicated host key (for example
~/.ssh/id_ed25519and~/.ssh/id_ed25519.pub). Create one under Create a New Key Pair if needed. Do not assume the Git-onlyid_ed25519_gitname from that page. - The remote login as
user@host. Ubuntu cloud images often useubuntu; Debian cloud images often usedebian(or whatever account you created). Use the real account name. - A bootstrap path still open: password SSH once, or a hypervisor/cloud console. If cloud-init or Terraform already injected your public key, skip Install the Public Key and go to Verify Passwordless Login.
- OpenSSH server on the guest (
openssh-serveron both Ubuntu and Debian).
If the private key has a passphrase, load it first (Load the Key in ssh-agent):
ssh-add --apple-use-keychain ~/.ssh/id_ed25519Install the Public Key
Section titled “Install the Public Key”Apple’s OpenSSH often has no ssh-copy-id. Install it with Homebrew, or use the manual steps below.
Option A: ssh-copy-id
Section titled “Option A: ssh-copy-id”brew install ssh-copy-idssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostEnter the account password when prompted. The tool appends the public key to ~/.ssh/authorized_keys on the remote side and sets common modes.
Option B: Manual authorized_keys
Section titled “Option B: Manual authorized_keys”From the Mac, show the public key line (one line):
cat ~/.ssh/id_ed25519.pubOn the remote host (password SSH or console), as the login user:
mkdir -p ~/.ssh # create .ssh if missingchmod 700 ~/.ssh # only this user may enter .sshchmod go-w ~ # home must not be group/world-writable (sshd rejects keys otherwise)echo 'PASTE_THE_FULL_PUBKEY_LINE_HERE' >> ~/.ssh/authorized_keys # append your public keychmod 600 ~/.ssh/authorized_keys # only this user may read authorized_keysPaste only the public .pub line. Never put the private key on the server.
Ownership must be that login user (not root), unless the account is literally root:
chown -R "$USER:$USER" ~/.sshRequired Remote Permissions
Section titled “Required Remote Permissions”OpenSSH ignores keys when modes are too open:
| Path | Mode |
|---|---|
Home directory (~) | Not group/world-writable (chmod go-w ~) |
~/.ssh | 700 |
~/.ssh/authorized_keys | 600 |
Verify Passwordless Login
Section titled “Verify Passwordless Login”From the Mac:
ssh -i ~/.ssh/id_ed25519 user@hostYou should land in a shell with no password prompt (a key passphrase via the agent is fine). If it still asks for the account password, run with verbose output and fix the key or modes:
ssh -v -i ~/.ssh/id_ed25519 user@hostConfirm key login works before you turn off password SSH on the server.
Optional Host Entry in ~/.ssh/config
Section titled “Optional Host Entry in ~/.ssh/config”Host lab-ubuntu HostName 10.0.0.21 User ubuntu IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yesSet User to debian (or your account) on a Debian host.
Then:
ssh lab-ubuntuIdentitiesOnly yes stops SSH from offering every key in the agent (avoids Too many authentication failures). Same pattern as Multiple Git Hosts with ~/.ssh/config.
Grant sudo on the Remote User
Section titled “Grant sudo on the Remote User”Check First
Section titled “Check First”Both Ubuntu and Debian use the sudo group for interactive sudo. Many Ubuntu cloud images already put ubuntu in that group. Some minimal Debian installs leave the first user out until you add them. On the remote host:
groupssudo -lsudo whoamiIf sudo whoami prints root, you are done for interactive privilege. Skip to Passwordless sudo only if automation needs it.
Add the User to the sudo Group
Section titled “Add the User to the sudo Group”As an existing admin or root (same command on Ubuntu and Debian). Replace youruser with the real login name (for example alice) — not the literal word USER:
sudo usermod -aG sudo youruserStart a new SSH session so the group applies. Verify:
groupssudo -vsudo whoamiPasswordless sudo (Optional)
Section titled “Passwordless sudo (Optional)”Use this for automation (for example Ansible become) when you do not want a password prompt. Prefer a narrow command list when the job is fixed; the example below is full NOPASSWD.
NOPASSWD: ALL lets this account run any command as root without a password. Treat the SSH private key as root access.
As a user who already has sudo. Again replace youruser with the real login name:
sudo visudo -f /etc/sudoers.d/youruserPut one line in that file:
youruser ALL=(ALL) NOPASSWD: ALLSave, then:
sudo chmod 0440 /etc/sudoers.d/yourusersudo -n whoamisudo -n must print root with no password prompt.
Troubleshooting
Section titled “Troubleshooting”| Symptom | Things to check |
|---|---|
Permission denied (publickey) | Correct .pub on the server? Right IdentityFile / -i? ssh-add -l shows the key? Modes on ~, ~/.ssh, authorized_keys? |
| Still prompted for account password | Key not in authorized_keys; wrong user; server still not offering pubkey — use ssh -v |
ssh-copy-id: command not found | brew install ssh-copy-id, or use manual authorized_keys |
user is not in the sudoers file | Add to sudo group (common on minimal Debian); new SSH session after usermod |
groups still missing sudo | Group changes apply on next login — disconnect and SSH again |
sudo: a password is required with -n | NOPASSWD drop-in missing, wrong username, or file not 0440 / not under /etc/sudoers.d/ |
Summary
Section titled “Summary”- Have a public key on the Mac.
- Install it with
ssh-copy-idor by appending to~/.ssh/authorized_keys, then fix modes (go-won~,700/600on.ssh/authorized_keys). - Confirm
sshworks without an account password. - Confirm or add the
sudogroup for interactive root. - Optionally add a
/etc/sudoers.d/NOPASSWD drop-in for automation.