How to SSH into a server as sudo from application

I am using Sequel Pro to connect to a remote database. Normally, when I SSH in, I have to run sudo su before I can access the database. So when I put in my normal SSH credentials into Sequel Pro, it comes back with an error that I don't have the rights to access the DB server (just like I get an error when I try to connect from the command line without sudo su).

So is there a way to sudo connect to a remote server?


Solution 1:

Please! Don't allow root logins via SSH!

Instead, edit your sudoers file to allow the command(s) you need to execute as the user you're logging in as--this is precisely why sudo exists! Then simply pre-pend the word 'sudo' to your remote command.

Example /etc/sudoers file:

myuser ALL=(root) NOPASSWD: /path/to/my/command --my --argument --list

Then your SSH command:

ssh myuser@remotehost "sudo /path/to/my/command --my --argument --list"

man sudoers and man ssh will provide additional details.

Solution 2:

Perhaps just enable remote logins as root, set a root password and then ssh as 'root'. This is generally discouraged as it is considered insecure to allow root logins over ssh. There are however various steps you can take to improve security about allowing this kind of login:

  1. Only allow root logins that use both passwords and key authentication
  2. Only allow root logins from certain known/trusted IP addresses
  3. Change the root username to something unique (not totally sure how easily this can be done)

This ServerFault question has a few more ssh security suggestions.

A much more 'secure' solution is to create a remote system account that only has permission to do the things you need to do. This obviously requires some reconfiguration on the machine you're logging in to and you don't say if that's an option for you.