Comments on: How to Execute Linux Commands on Remote System over SSH https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/ Tue, 10 Oct 2023 11:00:29 +0000 hourly 1 By: Rajavamsidhar https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-59655 Tue, 10 Oct 2023 11:00:29 +0000 https://www.linuxtechi.com/?p=7553#comment-59655 In reply to Pradeep Kumar.

Can we do below
I can run a script on remote a server with command: ssh user1@ip-of-server “command”

That works fine but I need to execute a command with a different user (user2).
If I do it manually – these would be the steps:
1) SSH as user1 to a server
2) sudo su – user2
3) execute command (now as user2)

]]>
By: Walter https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-48124 Wed, 18 Jan 2023 20:22:44 +0000 https://www.linuxtechi.com/?p=7553#comment-48124 I did it and it work perfectly.

No need to sshpass. I use the ssh private key generated instead a password. Something like:
ssh -i localpath/to/the/ssh_private_key remoteuser@remoteip command

Now, I was wondered: if a can use a ssh-copy-id to copy a key to the remote server, how can a remove the key from the remote server?

]]>
By: Pradeep Kumar https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-22756 Wed, 03 Feb 2021 03:06:22 +0000 https://www.linuxtechi.com/?p=7553#comment-22756 In reply to Ram Paravastu.

I Would recommend set the password file for webapp user and make ssh session with this user only and then execute the commands.
You can try one more option, create a small script, write the commands in that script that you want to execute as webapp user and call this script over the ssh session as shown below:

sshpass -f passwordfile ssh myself@abc.com ‘bash -s’ < 'script.sh'

]]>
By: Ram Paravastu https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-22755 Tue, 02 Feb 2021 21:39:57 +0000 https://www.linuxtechi.com/?p=7553#comment-22755 I looking for a solution for the below scenario:
I have setup a password file for my user.
I am using sshpass -f passwordfile ssh myself@abc.com
The above is working fine.
From the same command line I would like to switch user to app user webapp, like su – webapp
the webapp password I want to pass thru local file.
In essence sshpass -f passwordfile ssh myself@abc.com su – webapp
after executing the above command I would be logged in as webapp@abc.com
How to achieve it?
Kindly Help.

]]>
By: Julien https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-21051 Fri, 17 Jul 2020 14:37:48 +0000 https://www.linuxtechi.com/?p=7553#comment-21051 Hello,
I have the same issue as Santosh. I’d like to remotely mount a partition as root.
Is it possible ?
Thanks

]]>
By: Iñaki https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-18752 Wed, 12 Feb 2020 16:41:20 +0000 https://www.linuxtechi.com/?p=7553#comment-18752 In reply to ericm.

Your are completely right. “ssh ” will execute at . In other words, it will SSH to , then ONCE THERE it will execute whatever entails, just as if we where at (in the home dir of the user we are SSHing as). Any command we issue will be looked for at , not locally.

]]>
By: Santosh https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-17621 Wed, 04 Dec 2019 05:23:08 +0000 https://www.linuxtechi.com/?p=7553#comment-17621 How to execute sudo or root command remotely like password reset?

]]>
By: Ken https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-16609 Fri, 01 Nov 2019 20:22:06 +0000 https://www.linuxtechi.com/?p=7553#comment-16609 it looks like sshpass still will NOT work the first time when the local hostname has not been saved to the Known_hosts file in the remote host under .ssh directory. Does anyone know how to get along that?

]]>
By: Fuseteam https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-13618 Tue, 20 Aug 2019 14:47:18 +0000 https://www.linuxtechi.com/?p=7553#comment-13618 uh ssh-pass looks like a bad idea, or perhaps an incorrect usage?
the way you presented it the actual password or the name of the password file or the name of the env variable will be logged in the command history !!!
ssh via private and public key is way more secure as long as you keep your privatekey……..private

]]>
By: ericm https://www.linuxtechi.com/execute-linux-commands-remote-system-over-ssh/#comment-6004 Thu, 27 Sep 2018 21:36:52 +0000 https://www.linuxtechi.com/?p=7553#comment-6004 Hi Pradeep,

the quote below seems to imply system-info.sh is a local script, which is executed on a remote system (192.168.10.10) However, I have found that this only work if the script is on the remote system. In other words, ssh can execute a script that is on the remote server. Is this also your experience or else can you clarify?

We just have to provide absolute path of local script to SSH command.
$ ssh linuxtechi@192.168.10.10 ./system-info.sh

]]>