Progress: 0/62 (0%)

💻 Remote Login and Command Execution

Remote Login and Command Execution

With SSH, you don't always need a full interactive session. You can run commands or even entire scripts on a remote machine directly from your local terminal. This is a powerful feature for automation and remote system management.

Running Commands Remotely with SSH

You can run a single command on a remote machine like this:

ssh alice@192.168.1.10 'ls -l /home/alice'

This logs into 192.168.1.10 as alice, runs ls -l /home/alice, then exits. The output appears right in your local terminal.

Running Scripts Remotely

You can also execute entire scripts on a remote server:

ssh alice@192.168.1.10 'bash -s' < local_script.sh

The < local_script.sh sends your local script over SSH and executes it remotely. This is useful for automating updates, backups, or deployments.

Example: Automatically back up logs every night:

ssh alice@192.168.1.10 'tar -czf /home/alice/logs_backup.tar.gz /var/log/'

Practical Use Cases

Here are some common real-world scenarios:

1. Check disk space remotely:

ssh alice@192.168.1.10 'df -h'

2. Restart a service remotely:

ssh alice@192.168.1.10 'sudo systemctl restart apache2'

3. Automate updates on multiple servers:

for server in 192.168.1.10 192.168.1.11 192.168.1.12; do ssh alice@$server 'sudo apt update && sudo apt upgrade -y' done

Real-life analogy

For running commands remotely: It's like sending a remote-controlled robot to your friend's house to check if the lights are on, then the robot comes back with the report. For multiple servers: It's like being the conductor of an orchestra: you don't need to touch every instrument yourself — you just give commands and the instruments (servers) play along. With this, you now know how to log in securely using SSH, set up passwordless login with keys, transfer files using scp or sftp, and run commands or scripts remotely. This is basically the foundation for remote system management, server automation, and secure network administration.