How to execute commands remotely using SSH in Linux?
Last Updated :
02 Jun, 2022
Many times users need to work in remote systems. For which they have to log in to the remote server, execute certain commands and come out of that session. Is it possible to perform all these actions locally? Yes, it's possible using ssh client. In this article, we will see different ways of running remote commands locally using ssh.
Pre-requisite
- Any Linux distribution.
- Install OpenSSH and enable SSH service.
- Generate SSH key pairs to execute remote commands from the local server to avoid entering passwords.
- Require commands to be executed with root access or sudo privilege.
1. Single command execution
Let us execute single command 'date' to fetch from the remote machine,
2. Command redirection
It's possible to redirect the output of a command executed on the remote server to the local machine using the redirection operator(>),
3. Multi-command execution
To execute multiple commands, each command needs to be separated using a semicolon(;) to be enclosed within a single quote or double quote,
4. Multi-command execution using pipe
Using an unnamed pipe, let us try to see when the root user last logged onto the remote server,
5. Script execution
Remote command execution is not limited to commands, it's possible to execute even a script located in a remote server provided the script exists. We have to provide the absolute path of the script in the remote machine.
Here, a script is written in a remote server(10.21.42.166) to print the name of the operating system using 'uname' command,
Now let us run it,
The script doesn't have to execute permission, we got a permission denied error. After changing the permission, the script ran successfully.
6. Command execution with privileges
At times, we may need to execute commands with elevated privileges. The test user in the remote server doesn't have to write permission in the /etc/ directory.
We have used the ‘-t‘ option with the SSH command, allowing pseudo-terminal allocation. as sudo command requires an interactive terminal hence this option is necessary.
The file got created on a remote server,
Thus we have a good understanding of remote command execution for single, multi-commands, using pipe, using redirection, script execution, and running the command with privilege which would definitely help for easy remote access, any kind of automation, etc.
Similar Reads
How to use SSH to connect to a remote server in Linux | ssh Command Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It's like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you're sending a letter, and instead of sending it openly, you put it in a magic envelope th
8 min read
How to Make Script Executable in Linux | chmod Command In Unix operating systems, the chmod command is used to change the access mode of a file. The name is an abbreviation of change mode. Which states that every file and directory has a set of permissions that control the permissions like who can read, write or execute the file. In this the permissions
7 min read
How to Access All Users in Linux Using Different Commands? Linux allows multiple users with their own custom setting and configuration to work together on the same system, even at the same time. It can even allow a particular user to access several sessions from different locations in order to work on the system. Below is a list of different commands to acc
4 min read
How to Run Remote Command Execution on Powershell? From the Command Line Interface on Windows, the Command Prompt Application first comes to your mind. However, another great Command Line Interface is Windows Terminal i.e. Windows Powershell which can be also useful. If you want to perform Remote Command Execution on some other Remote Computers, the
5 min read
How to Connect to Amazon Linux Instance Using SSH? Pre-requisites: AWS, SSH TOOLS AWS stands for Amazon Web Services, and it is a cloud computing platform provided by Amazon. AWS provides a range of cloud-based services, including computing power, storage, databases, analytics, machine learning, and many other resources that businesses and individua
3 min read
How to change the default SSH port in Linux SSH (Secure Shell) is a network protocol used to securely connect to the remote server where the data between the server and client is transferred in an encrypted format. In the world of Linux system administration and security, one essential practice is changing the default SSH port. This article w
7 min read
How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp
10 min read
How to Create SSH Tunneling or Port Forwarding in Linux? SSH is a secure shell standard client utility for Linux. It is used to establish secure connections to remote (or even local) ssh servers. But some programs are not designed flexible enough to be processed by ssh trivial way: the program can work with local connections only or some related network a
6 min read
How to Compare Local and Remote Files in Linux In this article, we will discuss how to compare or differentiate between local and remote files in Linux. Programmers and writers often want to know the difference between two files or two copies of the same file when writing program files or regular text files. The discrepancy between the contents
3 min read
How to configure SSH Client in Linux ? At times we may need to access multiple remote systems with different IP addresses/hostnames, usernames, non-standard-ports with various command-line options. One way is to create a bash alias for each remote connection. But we have an easy and much better solution to this problem. OpenSSH allows to
4 min read