- How to run SSH command and exit
- Prerequisites:
- Run the different types of SSH Commands:
- Run the single command:
- Run multiple commands:
- Run multiple commands with pipe:
- Run command to execute the script:
- Run `sudo` command:
- Terminate from the SSH:
- Conclusion:
- About the author
- Fahmida Yesmin
- SSH: Execute Remote Command or Script – Linux
- SSH: Execute Remote Command
- Examples
- SSH: Run Multiple Remote Commands
- Examples
- SSH: Run Bash Script on Remote Server
- Example
- 23 Replies to “SSH: Execute Remote Command or Script – Linux”
How to run SSH command and exit
The users need to connect with the remote host by using SSH (Secure Shell) to run different commands for multiple purposes remotely. It helps the user do their task more easily. Many administrative tasks can be done remotely by the user with some simple steps that save the time and effort of the user. The user may require to run a single or multiple SSH commands remotely and terminate the connection safely from the remote host. How the SSH command can be run in the remote host and exit have shown in this tutorial.
Prerequisites:
Before starting the steps of this tutorial, the following steps will be required to complete.
Enable the SSH service on Ubuntu if it is not enabled before.
Generate the SSH Key pairs to execute the commands in the remote server. Run the following command to create the public key and the private key. The private key will be stored in the remote server, and the public keys will be stored in the client securely.
Run the following command to open the sshd_config file using nano editor to add some necessary configurations.
Add the following lines in the file to enable the root login and password-based authentication.
Run the following command to restart the SSH service.
Run the different types of SSH Commands:
You can check the tasks of the SSH commands by using the remote server or the local server. Two user accounts of Ubuntu have been used here to run the SSH commands in the local server. One user account has been used as a client, and another user account has been used as a server in this tutorial to check the way of executing the command in the server machine from the client machine using SSH.
Run the single command:
Run the following command to execute the `ls` command in the remote host using the hostname. Here, ‘yesmin@fahmida-VirtualBox’ has worked as a client machine, and ‘fahmida@fahmida-VirtualBox’ has worked as a server machine.
The following output shows the list of all files and folders of the current directory of the remote host. Here, the username of the remote host is ‘fahmida.’
Run the following command to execute the `pwd` command in the remote host by using the IP address of the remote host. Here, ‘yesmin@fahmida-VirtualBox’ has worked as a client machine, and ‘fahmida@10.0.2.15’ has worked as a server machine.
The following output shows the path of the current working directory of the remote host where the username of the remote host is ‘fahmida.’
Run multiple commands:
Run the following command to execute three commands, `date,` `pwd,` and `df,` using a single SSH command in the remote host by using the hostname. Here, ‘yesmin@fahmida-VirtualBox’ has worked as a client machine, and ‘fahmida@10.0.2.15’ has worked as a server machine.
The following output shows the output of the three commands after executing in the remote host where the username of the remote host is ‘fahmida.’
Run multiple commands with pipe:
Create a text file named fruits.txt with the following content in the remote host used in the next SSH command.
Run the following SSH command from the client’s terminal to check the content of the fruits.txt file that exists in the server.
Run the following SSH command from the client’s terminal to check the word ‘Mango’ exists or not in the fruits.txt file.
Run the following SSH command from the client’s terminal to check the word ‘Jackfruit’ exists or not in the fruits.txt file.
The following output will appear after executing the above three SSH commands. The following output shows that ‘Mango’ exists in the file and ‘Jackfruit’ does not exist.
Run command to execute the script:
Executing any bash script of the remote host using the SSH command has been shown in this part of the tutorial. Create a bash file named read.sh with the following script in the remote host. The script will take an input value from the user and print the input value by combining it with the other string after execution.
read -p «Do you like Mango? » ans
Run the following SSH command to check the content of the read.sh file that is located in the remote host.
The following output will appear if the file exists in the remote host.
Run the following SSH command to connect with the remote host using the IP address.
Run the following command to execute read.sh file after login to the remote host.
The following output will appear after executing the above commands. The bash file has been executed two times here with two different values.
Run `sudo` command:
Executing the `sudo` command after logging into the remote host using the SSH command has been shown in this part of the tutorial.
Run the following SSH command to log in to the remote host using the IP address of the remote host.
Run the following command to install the Python in the remote host after the successful login. It will ask for the password of the username of the remote host to start the installation process.
The following output will appear after executing the above commands.
Terminate from the SSH:
Run the following command to terminate the connection from the remote host.
Conclusion:
The ways to execute single and multiple commands, the commands with pipe, the command to run a particular bash file, and the use of the `sudo` command in the remote host have been shown in this tutorial. The reader will run the SSH command in the remote host after reading this tutorial properly.
About the author
Fahmida Yesmin
I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.
SSH: Execute Remote Command or Script – Linux
This is quite a common task for Linux system administrators, when it is needed to execute some command or a local Bash script from a one Linux workstation or a server on another remote Linux machine over SSH.
In this article you will find the examples of how to execute a remote command, multiple commands or a Bash script over SSH between remote Linux hosts and get back the output (result).
This information will be especially useful for ones, who want to create a Bash script that will be hosted locally on a one Linux machine but would be executed remotely on the other hosts over SSH.
Cool Tip: Connect to a remote SSH server without typing a password! Configure a passwordless authentication! Only 3 easy steps! Read more →
SSH: Execute Remote Command
Execute a remote command on a host over SSH:
Examples
Get the uptime of the remote server:
SSH: Run Multiple Remote Commands
Much more often it is required to send multiple commands on a remote server, for example, to collect some data for inventory and get back the result.
There are a lot of different ways of how it can be done, but i will show the most popular of them.
Run multiple command on a remote host over SSH:
$ ssh USER@HOST 'COMMAND1; COMMAND2; COMMAND3'
$ ssh USER@HOST 'COMMAND1 | COMMAND2 | COMMAND3'
Cool Tip: SSH login is too slow? This can be fixed easily! Get rid of delay during authentication! Read more →
Examples
Get the uptime and the disk usage:
$ ssh root@192.168.1.1 'uptime; df -h'
Get the memory usage and the load average:
$ ssh root@192.168.1.1 'free -m | cat /proc/loadavg'
Show the kernel version, number of CPUs and the total RAM:
SSH: Run Bash Script on Remote Server
The equally common situation, when there is some Bash script on a Linux machine and it needs to connect from it over SSH to another Linux machine and run this script there.
The idea is to connect to a remote Linux server over SSH, let the script do the required operations and return back to local, without need not to upload this script to a remote server.
Certainly this can be done and moreover quite easily.
Cool Tip: Want to ROCK? Start a GUI (graphical) application on a remote Linux workstation over SSH! Read more →
Example
Execute the local script.sh on the remote server:
23 Replies to “SSH: Execute Remote Command or Script – Linux”
Hi,
How about executing a command remotely and making sure that it will be killed once i kill my ssh session. or getting of PID of either SSH or the actual command on remote-node via a script.
Something like this I want to achieve:
Example: collecting pcap on remote addr and storing it locally.
$ ssh -t -f "tcpdump -w -" > /tmp/tcp.pcap
$ ssh ADDRESS 'echo "rootpass" | sudo -Sv && bash -s' < BASH_FILE
How do create a scrip to run commands on multiple remote machines?
When the first one gets executed, the response returned to STOUT, and the next one is not executed until I ^C the previous command.
I tried using ‘exit’ command and ‘&’ , neither is working.
Hello everyone,
I am new to linux and trying to learn it. I have task to complete “Get last 3 login details of list of linux machine with date and time.”
Is there any way to achive it? Thanks in advance
You should change these examples to use double quotes – I got tripped up putting variables in these single quotes and took me a while to realise bash treats it as a string…
como puedo hacer que solo me pida una vez la contraseña de mi servidor? hice mi escript y como realizo varias tareas me pide varias veces la contraseña
Can someone help me write a shell script to shutdown a Ubuntu computer?
I’m going to have it run on a Mac, so that it will ssh into the Ubuntu and shut it down. I need it to open Application “Terminal” then ssh name@123.45.67.89, then give it the password.
Then I need it to issue command “sudo poweroff”, and give it the password again.
I know how to do this manually by opening Mac’s Terminal. I just type in “ssh name@123.456.78.9, it asks for the password, I type it in, and it’s connected. Then I just type in “sudo poweroff” and it asks for the password again, I type it in, and bam, it shuts down the Ubunt computer.
The problem is, I need to automate this to do it at a specific time of day. On the Mac, there is what is called “Automator”, and you can set up ICalendar Events to run an “Automator Workflow” with a Shell Script. I just don’t know how to write the Shell Script to do what I can do manually? Any help is appreciated greatly!
I solved this. Here’s what works:
tell application “Terminal”
activate
do script (“ssh test@192.168.1.10“)
delay 6
do script “password” in front window
delay 7
do script “sudo poweroff” in front window
delay 5
do script “password” in front window
end tell
Why when I get a script from somewhere by curl & then pipe to bash to run it on my local server, some commands in a bash functions failed to execute, no error, no code error btw..just the called functions don’t execute.. How do I know my code is ok, because when I output it as a file from curl, I chmod and run it like ./..scriptfile.sh.. Code run fine.. Any idea and how do I resolve this?
This was very helpful for me. I am very new to this and just ordered a VPS yesterday and these guides are really helping me work my way around. I appreciate this simple resource you’ve put together!
how to run a script with sudo previllages on remote server: ssh -tt rundeck@$Batch_Host ‘ bash -s’ < /home/abhishekc/dem.sh i am getting permission denied