on a remote system from your local system itself without having to log in to that remote system, here is how to do it.
Execute Commands On Remote Linux Systems Via SSH
The typical way to run a command or script on a remote system over SSH from the local system is:
Allow me to show you some examples.
Run a single command on remote systems via SSH
Let us say you want to find Kernel details of your remote Linux system. To do so, simply, run:
Here,
- sk is the username of my remote system,
- 192.168.225.22 is the IP address of the remote system,
- And “uname -a” is the command that I want to run on the remote system from my local system.
Sample output:
See? I haven’t actually logged-in to the remote system, but executed the uname command on the remote system over SSH and displayed the output in my local system’s Terminal.
You can also specify the command in quotes like below.
Or,
If you have changed default port of SSH protocol, just mention it using -p parameter like below.
Run multiple commands on remote systems via SSH
You can also run multiple commands on the remote by specifying them within quotes like below.
Or,
The above commands will display the Kernel version and distribution details of my Ubuntu server.
Sample output:
As one one of our reader mentioned in the comment section below, you should specify multiple commands in quotes. If you don’t use quotes, the first command will execute on the remote system and second command will be evaluated on local machine only. The whole command in quotes will be processed remotely as intended.
Note: Know difference between “&&” and “;” operators between commands:
The “&&” operator executes the second command only if the first command was successful.
Example:
In the above case, the second command (sudo apt-get upgrade) will execute if the first command was successful. Otherwise, it won’t run.
The “;” operator executes the second command even if the first command was successful or fail.
Example:
In the above case, the second command (sudo apt-get upgrade) will execute even if the first command is failed.
Run commands with “sudo” privileges on remote systems via SSH
Some commands requires “sudo” privileges to run. For instance, the following command will install Vim on my remote system.
Sample output:
Did you notice? I have used -t flag in the above command. We need to mention this -tflag to force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services.
Also, I have entered password twice. The first time I entered the password of the remote user to access the remote system over SSH from my local system and the second password is required to give sudo permission to the remote user to install application (i.e. apache2 in this case) on the remote system.
Similarly, we can run any command or script on a remote system over SSH from the local system.
Run local scripts on remote systems via SSH
Let us a create a simple script on our local system to display all the available information about your remote system’s distribution name, package management and base details etc.
Add the following lines:
Press ESC key and type :wq to save and quit the file.
Now run this script on your remote system over SSH using command:
Sample output:
If you don’t specify ‘bash -s’ in the above command, you will get the details of the remote system but Pseudo-terminal will not be allocated.
Save output from remote system to the local system
This can be useful if you want to share the output of a command that you run on the remote system over SSH with your support team or colleague.
The following command will run “du -ah” on your remote system over SSH and save the output in diskusage.txt file in your local system.
You can then analyze the disk usage details by viewing the diskusage.txt file using catcommand or text viewers.
Configure SSH Key-based authentication to avoid password typing
If you run commands on remote systems often, you may want to configure SSH key-based authentication to skip password typing every time. More details can be found in the following link.
Comments
Post a Comment