I have a linux (debian based) server which is configured to allow SSH session to the user 'admin', but not the user 'root'. Both these accounts are linked somehow because they share the same password.

During an SSH session as admin, 'sudo' is required to run commands, unless I switch to the user 'root'.

I have some services on which I need to run now and then, or even at system startup. I'm currently using private/public key mechanism to remote execute commands on the server. Some of the commands are manually typed, others are shell scripts that I execute. Currently the server still asks for password when a command has uses sudo.

Question: How can remote execute as user 'admin' without supplying the password? Is it possible to use a private/public key to satisfy sudo? Or perhaps even a way to start shell scripts as the user 'root'?

Is it even possible to avoid having to type the password using sudo? If not, are they other alternatives for situation like mine?


6 Answers 6


you can tell sudo to skip password for some command.

e.g. in /etc/sudoers

archemar  ALL = (www-data) NOPASSWD: /bin/rm -rf /var/www/log/upload.*

this allow me to use

sudo -u www-data /bin/rm -rf /var/www/log/upload.*

as archemar without password.

Note that

sudo -u www-data rm -rf /var/www/log/upload.*

won't work (will ask a password) as rm differ from /bin/rm. (*)

Be sure to edit /etc/sudoers using visudo command.

Once you've reach advanced level, you might whish to have your own sudo files in /etc/sudoers.d.

(*) this change in modern OS (redhat 7.x circa 2022) if rm in your path match /bin/rm in sudoers.conf you might use rm.

  • This might just be what I'm looking for. I will have to put it to test and see.
    – izack
    Oct 3, 2017 at 8:45
  • This was a great input. Thank you so much. Luckily I discovered the dangers of editing that file before actually doing it. For future visitors, you MUST use the 'visudo' tool to edit, and be careful not to change the owner of the file or you risk lockout.
    – izack
    Oct 4, 2017 at 7:24

The most simple way is to provide password from stdin if your sudo supports that (-S key)

ssh -t admin@remotehost "echo <yourpassword> |sudo -S <yourcommand>"
  • 1
    My sudo does support the -S key. Good to know about this. I will keep this as an alternative. because generally I prefer avoiding to type my root password at all. But thanks for the input
    – izack
    Oct 3, 2017 at 8:41
  • 9
    This is bad because the password will be shown in many places (on screen, in process list, in history files) Oct 8, 2017 at 1:12
  • 2
    -1 This is bad because the password is seen by many places May 18, 2019 at 7:15
  • 2
    Maybe inserting the password inside an environment files makes this solution more acceptable. I.e. ssh -t admin@remotehost "echo $YOUR_PASSWORD | sudo -S <yourcommand>", defining the alias YOUR_PASSWORD in the ~/.bashrc file.
    – Andrea
    Apr 9, 2020 at 12:59

If the accounts are linked somehow it makes no sense to allow ssh for one and not the other. Here is what I would do instead:

  • enable ssh for root, allowing only access with ssh keys and not with a password
  • create a new key that will be used only for your specific command
  • put the key and the command you need to execute in authorized_keys of root, so that as soon as a connection is made with this key, the command is launched.

This is secure because in that way the caller can not have a shell nor execute any other command (even if he provides one).

You can see an example here: https://stackoverflow.com/questions/402615/how-to-restrict-ssh-users-to-a-predefined-set-of-commands-after-login with the command= syntax. You can also do the same thing by embedding the command in the certificate if you use certificates instead of keys, or do it globally using the configuration option ForceCommand

See http://larstobi.blogspot.com/2011/01/restrict-ssh-access-to-one-command-but.html for another example (which illustrates that you need to take into account parameters of your command)

  • Hi Patrick. I did some digging and I now believe the accounts aren't linked per-se but rather just share the same password. Changing 1, causes the other to change. But yes, it does not make sense when both share the same password, as getting hold of admin password will let you act as root anyway. Your solution sounds interesting. How do I setup a key that will be used only for specific command or commands? Any example or link to resource will be greatly appreciated.
    – izack
    Oct 9, 2017 at 14:11
  • I edited my answer with more info. Your case about one password for two accounts is strange. Are you sure they are not the same account? The name does not count, you have to compare their UID. Launch command id under both account and compare the result at beginning (uid=...) Oct 9, 2017 at 14:34
  • Thanks for the resource. I checked the uid and they do not match. So definitely no the same account
    – izack
    Oct 10, 2017 at 8:55

Worked for me, using sshpass:

sshpass -p $PASS ssh -t $USER@$HOST "echo \"$PASS\" |sudo -S $COMMAND"

To add to Archemar's answer, sudo asks for the password of the user running sudo, not the user the command will be run as. You say that the 'admin' and 'root' accounts are 'linked' and share the same password. If you have come to this conclusion based on sudo accepting the password for the 'admin' account (and the password sudo accepts changing when the password for 'admin' is changed), this is normal behaviour for sudo.

  • Yes, I came to this conclusion based on sudo accepting the same password for both admin and root user. I never changed the password since I initially set it and it was through an GUI so this link is only simulated by setting the password on both user accounts. I think this was done to allow the installation of packages without implicitly inheriting root permission
    – izack
    Oct 9, 2017 at 14:17

If you are using a MacBook with TouchID, you can configure SSH sudo with Mac TouchID (fingerprint, 2FA)


Code: https://github.com/prbinu/touch2sudo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .