Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-29-2017, 08:21 AM   #1
LQ Newbie
Registered: May 2017
Posts: 5

Rep: Reputation: Disabled
SSH script with SSH keys and sudo privileges

I have a additional questions regarding SSH keys:
If you connect using SSH with SSH keys and run commands which require sudo privileges, how do you do this? With a password, you would run the sudo command and enter a pwd for more privileges, but how to do this in a script?

This question is related to the following thread:
Originally Posted by gsr_kashyap View Post
ssh login using a script
Old 05-29-2017, 08:43 AM   #2
Senior Member
Registered: Apr 2005
Distribution: Ubuntu, Devuan, OpenBSD
Posts: 2,372
Blog Entries: 3

Rep: Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049Reputation: 1049

You'll have to work through it in several steps, first manually then in a more automated fashion. You'll probably want to make a separate key for this activity, then add command="sudo something in the public key which you've placed in authorized_keys on the server. For that, see the manual page for sshd in the section on "authorized_keys file format"

man sshd
When that works manually, modify /etc/sudoers so that the command with its specific parameters (and only those parameters) can be run with no password. For that see the manual page for sudoers

man sudoers
And if you need more background, ask here. But also spend the time to go through the book sudo Mastery by Michael W Lucas or else his presentation:

The idea is to use sudo with the least amount of privilege possible to get the job done.
2 members found this post helpful.
Old 05-29-2017, 09:42 AM   #3
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,240

Rep: Reputation: 981Reputation: 981Reputation: 981Reputation: 981Reputation: 981Reputation: 981Reputation: 981Reputation: 981
Guess the actual question is: how to configure /etc/sudoers
Here is an example:
teddy ALL=NOPASSWD: /sbin/halt "", /sbin/reboot "", /bin/umount /dev/cdrom
user teddy might execute the listed commands without password; sudo /sbin/halt (resulting /sbin/halt as root)

Last edited by NevemTeve; 05-29-2017 at 09:44 AM.
2 members found this post helpful.
Old 06-09-2017, 02:27 AM   #4
LQ Newbie
Registered: May 2017
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you both, this helped me a lot. I wasn't sure if I had to modify sudoers, but it seems the way to go.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script to push ssh keys to more than one server. vwtech Programming 6 02-15-2014 07:24 PM
Perl script to SSH into a server, and sudo jhyland87 Linux - Software 1 10-01-2010 02:10 PM
BASH - question about using sudo in SSH script xmichielx Programming 7 09-03-2010 03:08 AM
SSH host keys are not being read correctly from .ssh/known_hosts. bartonski Linux - Software 3 10-29-2009 04:40 PM
runremote script for passwordless ssh keys + LTSP shishirkotkar Linux - Networking 3 04-12-2008 12:07 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration