- [Instructor] In the previous video,
we looked at listing all the alive machines on a network.
Running Commands on a Remote Host with SSH.
This video will introduce different ways
in which commands can be executed at a remote host.
SSH is an interesting system administration tool
that gives you access to a shell on a remote computer,
which you can use to run commands.
SSH stands for Secure Shell,
as it transfers the network data transfer
over an encrypted tunnel.
SSH doesn't come pre-installed
with all GNU/Linux distributions,
and you may have to install the openssh-server
and openssh-client packages using a package manager.
SSH service runs at default port number 22.
To connect to a remote host with the SSH server running,
use ssh username@remote_host.
In this command,
username is the user that exists at the remote host.
Remote_host can be the domain name or IP address.
For example,
it will interactively ask for a user password,
and upon successful authentication,
it will return the shell for the user.
SSH performs a fingerprint verification to make sure that
we are actually connecting to the remote host we want to.
This is to avoid what is called a man-in-the-middle attack,
where an attacker tries to impersonate another computer.
SSH will, by default, store the fingerprint
for the first time we connect to a server
and verify that it does not change for future connections.
By default, the SSH server runs at port 22,
but certain servers run SSH service at different ports.
In that case, use the minus p port_num
with the SSH command to specify the port.
In order to connect to an SSH server running at port 422,
use...
You can execute commands in the shell
that correspond to the remote host.
However, when using SSH in shell scripts,
we do not want an interactive shell,
as we're required to execute several commands
and display or store their output.
Issuing a password every time is not practical
for an automated script,
hence password-less login
using SSH keys should be configured.
Password-less auto-login with SSH recipe
explains the SSH commands to set this up.
To run a command at the remote host
and display its output on the local shell, use this syntax.
For example, multiple commands can be given
using a semicolon delimiter in between the commands as...
In this example, the commands are executed
at the remote host are,
it can be generalized as...
We can also pass a more complex subshell in the
command sequence by using the brackets subshell operator.
Let's write an SSH-based shell script
that collects the uptime of a list of remote hosts.
Uptime is the time for which the system is powered on,
and the uptime command is used to display this information.
It's assumed that all systems in IP_LIST
have a common user test.
The SSH command can be executed
with several additional options.
Let's go through them.
SSH with compression,
the SSH protocol also supports
data transfer with compression.
This comes in handy when bandwidth is an issue.
Use the minus capital C option with the SSH command
to enable compression.
Redirecting data into standard in
of remote host shell commands.
Sometimes, we need to redirect some data
to standard in of remote shell commands.
Let's see how to do it.
Here's an example.
Echo on the remote host prints
the data received through standard in,
which in turn is passed to standard in from localhost.
Running graphical commands on a remote machine.
If you attempt to use this recipe with a command
that needs to show some sort of GUI to the user,
you'll see an error similar to cannot open display.
This is because the SSH shell is not able
to connect to the X server running on the remote machine.
For this, you need to set
the dollar DISPLAY variable like this.
This will launch the graphical output on the remote machine.
If you want to show the graphical output
on your local machine, use SSH's X11 forwarding option.
This will run the commands on the remote machine,
but will bring the graphical output to your machine.
Okay, awesome.
So we discussed all the ways of running commands
on a remote host with SSH.
In the next video,
we'll see transferring files through the network.