LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 05-29-2017, 08:21 AM   #1
Kefijoo
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:
Quote:
Originally Posted by gsr_kashyap View Post
ssh login using a script
 
Old 05-29-2017, 08:43 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Welcome.

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"

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

Code:
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
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Guess the actual question is: how to configure /etc/sudoers
Here is an example:
Code:
teddy ALL=NOPASSWD: /sbin/halt "", /sbin/reboot "", /bin/umount /dev/cdrom
user teddy might execute the listed commands without password; e.gm 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
Kefijoo
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.
 
  


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 AM.

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