Tag: SSH

  • How to move files around with scp

    Need to transfer files securely between computers? scp (Secure Copy) is a simple yet powerful command-line tool that lets you move files over an SSH connection with ease.

    Download a Remote File

    scp -i conn.pem azureuser@ip:/home/azureuser/output.gz output.gz

    Upload a File to Remote

    scp output.gz -i conn.pem azureuser@ip:/home/azureuser/output.gz

    Notice how the order of the files in the command determines whether you’re uploading or downloading.

    scp also offers several useful options to enhance your file transfers:

    -C – Compresses files or directories during transfer for faster copying.

    -P – Specifies the SSH port if it’s not the default port 22.

    -r – Recursively copies directories and their contents.

    -p – Preserves the original access and modification times of the files.

  • How to execute a command(s) or a script via SSH

    Imagine this: you’re sipping coffee at your desk, and you suddenly need to check the status of a remote server. Do you really want to fire up a full-blown remote desktop or wrestle with clunky web dashboards? No way. With SSH, you can execute commands remotely—fast, simple, and scriptable. If you’re not using this technique yet, you’re missing out on one of the best productivity hacks in the sysadmin and developer toolkit.

    Running a Single Command Over SSH

    Want to check the uptime of a remote machine? Just send the command directly and get the output instantly:

    ssh [email protected] 'uptime'

    Tip: The command inside single quotes runs on the remote host, and its output comes right back to your terminal. This is perfect for quick checks or automation scripts.

    Executing Multiple Commands

    Sometimes, you need to run a sequence of commands. You don’t have to SSH in and type them one by one. Use a here document for multi-command execution:

    ssh [email protected] << EOF
    COMMAND1
    COMMAND2
    COMMAND3
    EOF

    Gotcha: Make sure your EOF delimiter is at the start of the line—no spaces! Also, remember that environment variables and shell settings may differ on the remote host.

    Running a Local Script Remotely

    Have a script on your local machine that you want to run remotely? You don’t need to copy it over first. Just stream it to the remote shell:

    ssh [email protected] 'bash -s' < myscript.sh

    Pro Tip: This pipes your local myscript.sh directly to bash on the remote machine. If your script needs arguments, you can pass them after bash -s like this:

    ssh [email protected] 'bash -s' -- arg1 arg2 < myscript.sh

    Best Practices and Pitfalls

    • Use SSH keys for authentication—never hardcode passwords in scripts.
    • Quote your commands properly to avoid shell interpretation issues.
    • Test locally before running destructive commands remotely. A misplaced rm -rf can ruin your day.
    • Check exit codes if you’re automating deployments. SSH will return the exit status of the remote command.

    Why This Matters

    SSH command execution is a game-changer for deployment, automation, and troubleshooting. It’s fast, scriptable, and—when used wisely—secure. So next time you need to automate a remote task, skip the manual steps and use these SSH tricks. Your future self will thank you.