Skip to content

SSH to Ubuntu (Passwordless + sudo)

First PublishedByAtif Alam

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.

  • A key pair on the Mac. Prefer a dedicated host key (for example ~/.ssh/id_ed25519 and ~/.ssh/id_ed25519.pub). Create one under Create a New Key Pair if needed. Do not assume the Git-only id_ed25519_git name from that page.
  • The remote login as user@host. Ubuntu cloud images often use ubuntu; Debian cloud images often use debian (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-server on both Ubuntu and Debian).

If the private key has a passphrase, load it first (Load the Key in ssh-agent):

Terminal window
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Apple’s OpenSSH often has no ssh-copy-id. Install it with Homebrew, or use the manual steps below.

Terminal window
brew install ssh-copy-id
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host

Enter the account password when prompted. The tool appends the public key to ~/.ssh/authorized_keys on the remote side and sets common modes.

From the Mac, show the public key line (one line):

Terminal window
cat ~/.ssh/id_ed25519.pub

On the remote host (password SSH or console), as the login user:

Terminal window
mkdir -p ~/.ssh # create .ssh if missing
chmod 700 ~/.ssh # only this user may enter .ssh
chmod 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 key
chmod 600 ~/.ssh/authorized_keys # only this user may read authorized_keys

Paste 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:

Terminal window
chown -R "$USER:$USER" ~/.ssh

OpenSSH ignores keys when modes are too open:

PathMode
Home directory (~)Not group/world-writable (chmod go-w ~)
~/.ssh700
~/.ssh/authorized_keys600

From the Mac:

Terminal window
ssh -i ~/.ssh/id_ed25519 user@host

You 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:

Terminal window
ssh -v -i ~/.ssh/id_ed25519 user@host

Confirm key login works before you turn off password SSH on the server.

Host lab-ubuntu
HostName 10.0.0.21
User ubuntu
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes

Set User to debian (or your account) on a Debian host.

Then:

Terminal window
ssh lab-ubuntu

IdentitiesOnly yes stops SSH from offering every key in the agent (avoids Too many authentication failures). Same pattern as Multiple Git Hosts with ~/.ssh/config.

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:

Terminal window
groups
sudo -l
sudo whoami

If sudo whoami prints root, you are done for interactive privilege. Skip to Passwordless sudo only if automation needs it.

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:

Terminal window
sudo usermod -aG sudo youruser

Start a new SSH session so the group applies. Verify:

Terminal window
groups
sudo -v
sudo whoami

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:

Terminal window
sudo visudo -f /etc/sudoers.d/youruser

Put one line in that file:

youruser ALL=(ALL) NOPASSWD: ALL

Save, then:

Terminal window
sudo chmod 0440 /etc/sudoers.d/youruser
sudo -n whoami

sudo -n must print root with no password prompt.

SymptomThings 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 passwordKey not in authorized_keys; wrong user; server still not offering pubkey — use ssh -v
ssh-copy-id: command not foundbrew install ssh-copy-id, or use manual authorized_keys
user is not in the sudoers fileAdd to sudo group (common on minimal Debian); new SSH session after usermod
groups still missing sudoGroup changes apply on next login — disconnect and SSH again
sudo: a password is required with -nNOPASSWD drop-in missing, wrong username, or file not 0440 / not under /etc/sudoers.d/
  1. Have a public key on the Mac.
  2. Install it with ssh-copy-id or by appending to ~/.ssh/authorized_keys, then fix modes (go-w on ~, 700 / 600 on .ssh / authorized_keys).
  3. Confirm ssh works without an account password.
  4. Confirm or add the sudo group for interactive root.
  5. Optionally add a /etc/sudoers.d/ NOPASSWD drop-in for automation.