sudo auth via ssh-agent

One of the nicest things about writing a book is that your tech reviewers tell you completely new but cool stuff about your topic. While I was writing the OpenSSH book, one of the more advanced reviewers mentioned that you could use your SSH agent as an authentication source for sudo via pam_ssh_agent_auth.

I have dozens of servers. They all have a central password provider (LDAP). They’re all secured, but I can’t guarantee that a script kiddie cannot crack them. This means I can’t truly trust my trusted servers. I really want to reduce how often I send my password onto a server. But I also need to require additional authentication for superuser activities, so using NOPASSWD in sudoers isn’t a real solution. By passing the sudo authentication back to my SSH agent, I reduce the number of times I must give my password to my hopefully-but-not-100%-certain-secure servers. I can also disable password access to sudo, so that even if someone steals my password, they can’t use it. (Yes, someone could possibly hijack my SSH agent socket, but that requires a level of skill beyond most script kiddies and raises the skill required for APT.)

My sample platform is FreeBSD-9/i386, but this should work on any OS that supports PAM. OpenBSD doesn’t, but other BSDs and most Linuxes do.

pam_ssh_agent_auth is in security/pam_ssh_agent_auth in ports and pkgsrc. There are no build-time configuration knobs and no dependencies, so I used the package.

While that installs, look at your sudoers file. sudo defaults to purging your environment variables, but if you’re going to use your SSH agent with sudo, you must retain $SSH_AUTH_SOCK. I find it’s useful to retain a few other SSH environment variables, for sftp if nothing else.

Newer versions of sudo cache the fact that you’ve recently entered your password, and let you run multiple sudo commands in quick succession without entering your password. This behavior is fine in most environments if you’re actually typing your password, but as sudo will now query a piece of software for your authentication credentials, this behavior is unnecessary. (Also, this caching will drive you totally bonkers when you’re trying to verify and debug your configuration.) Disable this with the timestamp_timeout option.

To permit the SSH environment and set the timestamp timeout, add the following line to sudoers:

Defaults env_keep += "SSH_AUTH_SOCK",timestamp_timeout=0

You can add other environment variables, of course, so this won’t conflict with my earlier post on sftp versus sudo.

Now tell sudo to use the new module, via PAM. Find sudo’s PAM configuration: on FreeBSD, it’s /usr/local/etc/pam.d/sudo. Here’s my sudo PAM configuration:

auth sufficient /usr/local/lib/ file=~/.ssh/authorized_keys
auth required
account include system
session required

By default, sudo uses the system authentication. I removed that. I also removed the password management entry. Instead, I first try to authenticate via If that succeeds, sudo works. If not, the auth attempt fails.

Now try it. Fire up your SSH agent and load your key. SSH to the server with agent forwarding (-A), then ask sudo what you may run.

$ sudo -l
Matching Defaults entries for mwlucas on this host:

Runas and Command-specific defaults for mwlucas:

User mwlucas may run the following commands on this host:

Now get rid of your SSH agent and try again.

$ unsetenv SSH_AUTH_SOCK
$ sudo -l
Sorry, try again.
Sorry, try again.
Sorry, try again.
sudo: 3 incorrect password attempts

The interesting thing here is that while you’re asked for a password, you never get a chance to enter one. Sudo immediately rejects you three times. Your average script kiddie will have a screaming seizure of frustration.

The downside to this setup is that you cannot use passwords for sudo on the console. You must become root if you’re sitting in front of the machine. I’m sure there’s a way around this, but I’m insufficiently clever to come up with it.

Using the SSH agent for sudo authentication changes your security profile. All of the arguments against using SSH agents are still valid. But if you’ve made the choice to use an SSH agent, why not use it to the fullest? And as this is built on PAM, any program built with PAM can use the SSH agent for authentication.

13 Replies to “sudo auth via ssh-agent”

  1. For OpenBSD systems, check the -a flag for sudo and login.conf to accomplish the same thing.

  2. Peter, if you don’t trust a server, you should NEVER forward your agent to it. I was a FreeBSD committer, and didn’t forward my agent to FreeBSD machines. Anyone who wants more details, look at my book “SSH Mastery” for a discussion of this topic.

  3. It occurred to me today that it could be possible to authorize sudo based on the ssh-agent, and I found your blog entry detailling how to do it. <3 when I find other people have already materialized ideas I have :).

    I'll leave here a somehow related question, as I see you seem to have a high experience in ssh management in distributed environments:

    I find SSH very handy to manage access to Unix accounts across many servers. It can be handled centrally quite easily using shell scripts to distribute authorized_keys files to a root folder on the remote servers, enforcing an updated configuration that the users can't change. This is good. Actually, in a customer we succeded to manage about 8000 (personas,machine,Unix_Account) triplets with (Personas, Groups) membership and (Group,(Machine,Unix_Account)) associations.

    Yet something I haven't been able to overcome is that people still can create their ssh private key without selecting any passphrase. So, there's no way to enforce passphrase, nor passphrase aging policy, which is really a lacking feature of this setup IMHO.

    The only thing I can come with would be private keys aging, which could be managed centrally by actually aging and expiring the public keys in the central repository… But still it's not the same as enforcing passphrase policies, and IMHO passphrase is a critical point for the security in this context.

    What do you think?

  4. Great tip, but I think it needs corrections to be more secure,

    For auth you should use pubkey-file which user cannot modify, like this:

    auth sufficient /usr/local/lib/ file=/root/.ssh/sudo-authorized_keys

    or else an intruder can impersonate you with his own SSH_AUTH_SOCK and modified ~/.ssh/authorized_keys file…

Comments are closed.