SSH hostname command on remote server shows hostname of local server

I run this SSH command from server1 on server2 :

ssh server2 echo pasword | su - root -c "echo this is the `hostname`" 

I expected the result to be host name of server2 but instead I get host name of server1. How can I manipulate the command to get the hostname of server 2 ?

Thanks.

5

2 Answers

Firstly, your command piping is wrong. As it currently stands this is executed in this order:

  1. ssh server2 echo pasword - this goes to Server 2 and logs in and executes the command echo pasword on the remote system. (output would be pasword)

  2. Take the output from #1 ("pasword") and pipe that into this command run locally:

    su - root -c "echo this is the `hostname`"

    ... which echoes your LOCAL hostname, technically (and doesn't do anything with the echoed 'pasword' from the first command). (you don't need 'root' for this though - hostname can be run by anyone on the remote system or your local system, no need for superuser!).

What you need to do is pass the command into SSH and NOT into pipes this way. The way to do this is to use this:

ssh server2 'echo "This is the $(hostname)"'

This will properly send the request to the remote server to get the hostname from that server and execute that on the remote server and tell you the response. You do not need sudo to do this on the remote server.

If you need to provide a password for the SSH session, you should be using ssh-pass instead, and execute this instead:

sshpass -p YourPassword ssh server2 'echo "This is the $(hostname)"'

This is considered the proper approach as it does keyboard-interactive authentication non-interactively and passes the password into the SSH command when prompted for a password. However this is still very insecure, as your password will be in command history or hardcoded in scripts, so you should consider using SSH Key Authentication on your remote servers instead of password authentication where possible.

There are several things here.

First - the command

ssh server2 echo pasword | su - root -c "echo this is the `hostname`"

does the following:

  • runs the command echo pasword on the remote server server2 via ssh

  • and feeds output from this command to the following command, executed on your local computer:

    su - root -c "echo this is the `hostname`"

If you want the entire pipe, starting from echo, to be executed on remote computer, you should do it this way:

ssh server2 'echo pasword | su - root -c "echo this is the `hostname`"'

But this is not going to work, because su requires to be run from a terminal and will just abort with an error message. However, you don't need root right to run the hostname command, so you might just try

ssh server2 echo 'this is the `hostname`'

You must enclose the hostname substitiution in single quotes, otherwise it will be substituted on your local computer before being sent to the remote one, so it will still print your local hostname.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like