Logging In as a root user to a remote machine using SSH

Posted on

Logging In as a root user to a remote machine using SSH – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, networking, ssh, automation, linux-kernel.

My problem is similar to this one , I am trying to automate the portion where I can access the remote machine and run a command as super user. It works fine till it logs-in ,
like from here :-

$ ssh-keygen -t rsa -b 2048


$ ssh-copy-id id@server
id@server's password:
$ ssh id@server

The overhead for me is that I can ask user for remote machine’s password only once (while generating ssh keys), and after that while running a command as super user it shouldn’t ask for password again i.e It shouldn’t need a command like below:-

sudo su

Is there any way I can do it? because I tried saving the password as variable and then substituting the value like :-

echo -n "Enter password > "
read passwd
sshpass -p $username ssh -o StrictHostKeyChecking=no $username@$server sudo some_application &

It will prompt for the password where I cannot substitute the value of the variable I declared.

Solution :

Have you tried using expect?

Simple login script writen in expect:

set timeout 9
set username [lindex $argv 0]
set password [lindex $argv 1]
set hostname [lindex $argv 2]
log_user 0

if {[llength $argv] == 0} {
  send_user "Usage: scriptname username 'password' hostnamen"
  exit 1

send_user "n#####n# $hostnamen#####n"

spawn ssh -q -o StrictHostKeyChecking=no $username@$hostname

expect {
  timeout { send_user "nFailed to get password promptn"; exit 1 }
  eof { send_user "nSSH failure for $hostnamen"; exit 1 }

send "$passwordr"

expect {
  timeout { send_user "nLogin failed. Password incorrect.n"; exit 1}
  "*$ "

send_user "nPassword is correctn"
send "exitr"

Example of use:

./script_name user_foo pass_bar hostname.com

It would only try to log you in and give any error message in case something went wrong and, if not, it would say “Password is correct” and then exit the session.

Some of the parameters will deppend on the system you are conecting to. Also, using expect means you know what the output will be when logging in and executing commands.

This tutorial will cover your needs

Did you actually copy your pub key to root’s .ssh/authorized_keys file?
I always do “chmod -R go= /root/.ssh” afterwards, also check ownership.

If you’ve got selinux running, you’ll have to “restorecon” on the authorized_keys file and the .ssh dir too.

And if you want to ssh in as root, you need to check /etc/ssh/sshd_config and reload sshd.

Leave a Reply

Your email address will not be published. Required fields are marked *