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
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
grep command in Unix/Linux The grep command in Unix/Linux is a powerful tool used for searching and manipulating text patterns within files. Its name is derived from the ed (editor) command g/re/p (globally search for a regular expression and print matching lines), which reflects its core functionality. grep is widely used by
7 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
Sed Command in Linux/Unix With Examples The SED command is one of the most powerful commands used during the process of text processing in Linux/Unix operating systems. The SED command is typically invoked for executing operations such as replace and search, text manipulation, and stream editing.With SED, you can manipulate text files wit
9 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
How to Find the Wi-Fi Password Using CMD in Windows Forgotten your Wi-Fi password? Need to connect a new device but canât recall the complex string of characters? You donât have to scramble for that sticky note or reset your router just yet! Hidden within Windows is a powerful, built-in tool that lets you retrieve your Wi-Fi password quickly and secu
8 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
How to Install PIP on Windows PIP stands for "Preferred Installer Program" or "Pip Installs Packages" and is a standard package manager for Python that enables users to install and manage additional libraries and dependencies not included in the standard Python library. To use PIP, you must install Python on your Windows machine
6 min read