SSH Command

Mastering SCP and SSH Linux Commands

Ready to start learning? Individual Plans →Team Plans →

One wrong port flag, one missing -r, or one misunderstood SCP behavior can turn a simple file transfer into a support ticket. This guide shows how to use the command patterns for SSH and SCP correctly so you can log in securely, move files without exposing data in transit, and troubleshoot the most common failures fast.

Quick Answer

The Linux command for secure remote login is SSH, and the command for secure file transfer over SSH transport is SCP. SSH is best for interactive remote sessions and one-off commands, while SCP is best for quick copies of logs, configs, scripts, and backups. On current OpenSSH releases, SCP may use SFTP mode by default, which changes some legacy behaviors.

Quick Procedure

  1. Verify the remote host, username, and port before you connect.
  2. Use ssh user@host to open a secure remote shell or run a single command.
  3. Use scp source target to copy a file between local and remote systems.
  4. Add -r when copying directories and -P for custom SCP ports.
  5. Use SSH keys instead of passwords for repeated access and automation.
  6. Check transfer results with SSH, then confirm paths, permissions, and service behavior.
  7. Use scp -v or ssh -v when troubleshooting connection failures.
Primary useSecure remote access and secure file transfer as of November 2026
SSH syntaxssh [options] user@host as of November 2026
SCP syntaxscp [options] source target as of November 2026
Common SSH port flag-p as of November 2026
Common SCP port flag-P as of November 2026
Directory copy flag-r as of November 2026
Legacy SCP compatibility flag-O as of November 2026

Admins use these Linux commands every day for log checks, patching, backup restores, config deployment, and incident response. SSH is the secure remote shell and command-execution tool, while SCP is the secure copy tool that rides on SSH transport. If you know the difference, you avoid the two most common mistakes: using the wrong tool for the job and troubleshooting the wrong layer.

Understanding SSH and SCP in the Linux Ecosystem

SSH is a secure remote access protocol and command-line tool for opening encrypted shell sessions or running a single command on another machine. SCP is a secure file-copy command that uses SSH transport to move files between systems. That distinction matters because SSH is the shell, while SCP is the courier.

Think of SSH as the tool you use when you need to work on the remote machine directly. Think of SCP as the tool you use when you need to move something to or from that machine. If you need to check a service, SSH is the right command. If you need to send a config file, SCP is the right command.

Both tools encrypt traffic in transit, which protects usernames, passwords, commands, and file contents from being read on the network. That makes them foundational for Remote Access, Security, and routine Deployment work. In practice, they show up in service restarts, log reviews, configuration changes, and backup retrievals.

SSH is for control. SCP is for movement. Mixing those roles wastes time and makes troubleshooting harder.

OpenSSH is the standard implementation on most Linux systems, and its man pages are still worth reading before production use. The official OpenSSH project documents the current behavior, including modern SCP semantics and option differences. For reference, start with the project documentation at OpenSSH and the Linux man pages available on your system with man ssh and man scp.

Note

Current OpenSSH releases may use SFTP mode underneath scp by default. That means some older examples online can be outdated even when the syntax still looks familiar.

How Does the SSH Command Work?

The SSH command follows the pattern ssh [options] user@host. The user part tells the remote system which account to use, and the host part identifies the server by hostname or IP address. If you leave off the remote command, SSH opens an interactive shell.

A simple login looks like this:

ssh admin@192.0.2.10

That command opens a remote session as admin on the host at 192.0.2.10. If you want to run a single command without staying in the shell, place the command at the end:

ssh admin@192.0.2.10 df -h

This is useful when you need a quick answer and do not want to manually log in, run the command, and exit. For example, you can check disk usage, verify a service, or read a log line from a remote server in one step. That saves time during maintenance windows and incident response.

Common SSH options that matter most

The most-used SSH options are the ones that affect connectivity and authentication. The port flag is lowercase -p, and the identity file flag is -i. Those two are easy to mix up under pressure.

  • -p sets a custom SSH port.
  • -i points to a private key file.
  • -v enables verbose output for troubleshooting.

Example with a nonstandard port and identity file:

ssh -p 2222 -i ~/.ssh/id_ed25519 admin@server01

The official Cisco® and Microsoft® documentation ecosystems both stress the same operational idea: know the endpoint, know the credentials, and verify the transport before assuming the application is broken. For SSH usage details, OpenSSH man pages remain the best authoritative source, and Microsoft Learn documents the same remote-access principles in its administrative guidance at Microsoft Learn.

How Does the SCP Command Work?

The SCP command follows the pattern scp [options] source target. SCP decides whether each path is local or remote by the way you write it. A remote path usually includes a username, host, and colon before the file path.

Here is a local-to-remote copy:

scp report.txt admin@192.0.2.10:/tmp/

Here is a remote-to-local copy:

scp admin@192.0.2.10:/var/log/syslog ./

Here is a remote-to-remote copy:

scp admin@192.0.2.10:/tmp/report.txt admin@192.0.2.20:/tmp/

SCP works well when you need a quick one-off transfer. It is commonly used for logs, config backups, deployment assets, and small scripts. It is not a synchronization tool, so it is a poor fit for repeated mirror-style operations where file deltas matter more than simple copying.

The official guidance from Red Hat also emphasizes using the right file-transfer method for the task, especially when security and operational consistency matter. That advice is practical: if you need browsing or interactive file handling, SFTP may be a better fit; if you need repeated incremental updates, rsync is usually the better operational choice.

What the source and target really mean

SCP is positional. The first path is the source, and the second path is the destination. If you reverse them, you may copy in the wrong direction or get a permission error that looks unrelated. Absolute paths are usually the safest choice in admin workflows because they remove ambiguity.

For example, this is clearer than a relative path when moving a backup into a known directory:

scp backup.tar.gz admin@server01:/var/backups/

That clarity matters when scripts, shells, or aliases change your working directory unexpectedly.

Prerequisites

Before you start using SSH and SCP in production-like workflows, make sure these basics are in place.

  • A Linux workstation or server with OpenSSH installed.
  • A reachable remote host with SSH service enabled.
  • A valid remote username with permission to log in.
  • A password or SSH key pair for authentication.
  • Basic familiarity with paths, file permissions, and terminal usage.
  • Permission to read or write the target directories on the remote system.

If you are working in a hardened environment, confirm the listening port, firewall rules, and host key policy before you begin. That small check prevents a lot of false troubleshooting later.

How Do You Authenticate with SSH and SCP?

Authentication is the process of proving you are allowed to connect. In SSH and SCP workflows, you will usually see password-based login or key-based login. Passwords are common in small or temporary setups, but keys are the preferred method for repeatable administration.

Password authentication still appears in labs and simple environments because it is easy to set up. The downside is obvious: passwords are easier to reuse, easier to guess, and harder to automate safely. For day-to-day admin work, SSH keys reduce password exposure and make scripted access much more reliable.

Generate and deploy SSH keys

Use ssh-keygen to create a key pair, then copy the public key to the remote host with ssh-copy-id. A typical flow looks like this:

ssh-keygen -t ed25519

ssh-copy-id admin@server01

The first command creates the key pair, and the second installs the public key on the remote server. After that, SSH and SCP can often authenticate without prompting for a password. That is especially useful for routine administration, automation, and low-friction troubleshooting.

Use the identity file option when needed

The -i option tells SSH which private key file to use. That matters when you have multiple key pairs, when a team uses separate keys for different environments, or when a service account uses a dedicated identity file.

scp -i ~/.ssh/prod_ed25519 app.conf admin@server01:/etc/app/

That command uses the specified key for the transfer. It is also a good habit when you need to make your authentication path explicit instead of relying on defaults.

Using SSH for Remote Command Execution

Remote command execution means running a command on the remote host without opening an interactive shell. This is one of the most useful SSH features for admins because it cuts out the manual middle step of logging in, typing the command, and logging out. A single SSH line can collect the information you need and return it immediately.

Examples:

ssh admin@server01 uptime

ssh admin@server01 systemctl status nginx

ssh admin@server01 tail -n 20 /var/log/auth.log

These are ideal for quick checks during incident response. You can confirm a service is running, inspect disk usage, or verify whether a log contains an error without leaving your workstation. The less time you spend navigating remote shells, the faster you can diagnose the problem.

Watch your quoting carefully

Quoting matters when your remote command contains pipes, semicolons, variables, or nested quotes. The local shell processes one layer, and the remote shell processes another. If you get the quoting wrong, the command may run locally instead of remotely, or it may fail with a syntax error.

Example:

ssh admin@server01 'grep -i error /var/log/nginx/error.log | tail -n 5'

Use single quotes around the remote command when possible. That keeps the local shell from interpreting special characters too early.

How to Copy Files with SCP Correctly

File transfer with SCP is straightforward once you understand direction and path formatting. The most common use cases are local-to-remote, remote-to-local, and remote-to-remote transfers. Each one uses the same command structure but a different path pattern.

Local to remote:

scp ./backup.sql admin@server01:/var/backups/

Remote to local:

scp admin@server01:/var/log/app.log ./

Remote to remote:

scp admin@server01:/tmp/package.tar.gz admin@server02:/tmp/

This is especially useful when you need to move a file between two servers without staging it on your laptop first. That can save time and reduce unnecessary hops, especially in restricted admin networks.

Use absolute destinations in admin workflows

Absolute paths reduce mistakes. If you are copying into a service directory, a backup directory, or a config location, being explicit prevents accidental placement in the wrong folder. That is important when a service expects a file in one exact location and nowhere else.

For example, copying a web config backup into a predictable location is safer when you write:

scp nginx.conf.backup admin@server01:/etc/nginx/

Rather than relying on a relative path that changes with the current working directory.

How Do You Copy Directories with SCP?

The -r option tells SCP to copy directories recursively. Without it, SCP expects a single file and will fail or skip the directory tree. That is one of the most common reasons a transfer seems to “partially work” when the real issue is that the command was not recursive.

Example:

scp -r ./site-backup admin@server01:/var/backups/

That copies the entire site-backup directory and its nested contents. This is useful for application bundles, config trees, and backup folders where multiple files matter together. If you omit -r, the directory itself will not transfer the way you expect.

Directory copies often include files with different permissions and ownership requirements. SCP will move the data, but it does not magically make the destination usable by a service. After the copy, verify the ownership and permissions where the application expects them.

A good habit is to check the destination with SSH after the transfer:

ssh admin@server01 'ls -l /var/backups/site-backup'

That quick check confirms that the directory exists and the contents arrived intact.

What Is the Difference Between -p and -P?

SSH commonly uses lowercase -p for the port, while SCP uses uppercase -P. This is one of the most common command-line mistakes people make when moving between the two tools. The difference is small, but the behavior is not.

SSH example:

ssh -p 2222 admin@server01

SCP example:

scp -P 2222 report.txt admin@server01:/tmp/

If you use the wrong case, the command may fail in a confusing way. Sometimes it looks like authentication failed, when the real issue is that the client never reached the correct port. That is why port problems should be one of the first things you check, not the last.

Custom ports are common in hardened environments, jump-host setups, and multi-service systems where SSH is not listening on 22. Confirm the configured port before you dig deeper into keys, permissions, or firewall rules.

Warning

Do not assume that an authentication error means your password or key is wrong. A bad port, blocked firewall, or unreachable host can produce the same symptom from the user’s point of view.

What Changed in Modern SCP Behavior?

Modern SCP behavior on current OpenSSH versions may use SFTP mode by default instead of the older legacy SCP protocol semantics. That matters because older tutorials often describe SCP as if it still behaves exactly as it did years ago. In many environments, it no longer does.

This change affects compatibility, quoting behavior, and how certain edge cases are handled. For most users, the default behavior is fine and may even be safer. But if you are dealing with legacy servers, unusual path parsing, or scripts written against older expectations, you need to know that behavior can differ.

If you need legacy SCP semantics, use scp -O. That flag forces the older behavior when compatibility requires it. It is a good option to know, especially if a transfer works in one environment but not another and the explanation seems to contradict old documentation.

The OpenSSH project documents this behavior directly, and you should treat that documentation as the final word for current clients. For a standards-minded perspective on secure transfer practices, see the official OpenSSH ecosystem guidance and the broader OpenSSH project information at OpenSSH.

When Should You Use SSH, SCP, SFTP, or Rsync?

Tool selection matters more than most admins admit. SSH, SCP, SFTP, and rsync solve related but different problems. If you choose the right one, the task is faster and safer. If you choose the wrong one, you create unnecessary complexity.

  • SSH is best for interactive work and single remote commands.
  • SCP is best for quick secure file copies.
  • SFTP is best for interactive file browsing and manual transfers.
  • rsync is best for repeated synchronization and incremental updates.
Task Best tool and why
Check a service status SSH because you want a remote command, not a file transfer
Copy one config file SCP because it is quick and direct
Browse remote files interactively SFTP because directory navigation is easier
Keep two directories in sync rsync because incremental copying is more efficient

Here is the simple rule: use SSH when you need control, SCP when you need movement, SFTP when you need browsing, and rsync when you need synchronization. That decision framework saves time and avoids using a file-copy command for a synchronization job it was never meant to handle.

How Can SSH Configuration Shortcuts Improve Your Workflow?

~/.ssh/config is a per-user SSH configuration file that lets you define shortcuts for hosts, usernames, ports, and identity files. This is one of the best productivity upgrades for sysadmins because it reduces typing and standardizes how you connect. It also reduces the chance of mistakes during urgent troubleshooting.

A simple alias might look like this:

Host prod-web
HostName 192.0.2.10
User admin
Port 2222
IdentityFile ~/.ssh/prod_ed25519

With that in place, you can connect using:

ssh prod-web

And copy files using:

scp app.conf prod-web:/etc/app/

That makes everyday work easier, but it also improves consistency. Team members can use the same naming pattern for the same server roles, which reduces confusion during handoffs and incidents. Named hosts are easier to read than a string of IPs and flags.

For admins in DevOps and operations roles, this is not a cosmetic improvement. It is a practical way to cut errors and speed up repetitive tasks.

How Do You Troubleshoot SSH and SCP Failures?

Troubleshooting SSH and SCP works best when you check the problem in a logical order: network reachability, authentication, path accuracy, and permissions. That sequence prevents wasted effort. A command failure is often caused by the layer below the one you first notice.

Start with verbose output

Use ssh -v to see what the client is doing. Verbose mode can reveal the port in use, which key is being tried, whether the host key is accepted, and where authentication breaks down. The same idea applies to SCP with scp -v, which is often especially useful when transfer behavior is unclear.

Common symptoms and likely causes:

  • Connection timed out usually points to network, firewall, or host reachability problems.
  • Permission denied often means wrong username, wrong key, or insufficient server-side access.
  • Host key verification failed means the known host entry changed or needs review.
  • No such file or directory usually means the source or destination path is wrong.

Host key prompts are there for a reason. They help detect man-in-the-middle risk and should not be blindly ignored. If a host key changes unexpectedly, verify the reason before accepting a new one.

The U.S. National Institute of Standards and Technology (NIST) recommends strong access control and secure remote administration practices in its security guidance. For broader secure configuration context, see NIST and the NIST Computer Security Resource Center at NIST CSRC.

What Security Best Practices Should You Follow?

Security best practices for SSH and SCP are mostly about reducing attack surface and reducing operator mistakes. Encrypted transport helps protect data in transit, but good operational hygiene matters just as much. A secure command still becomes a risk if private keys are left exposed or files are copied to the wrong location.

  • Prefer SSH keys over passwords for routine access.
  • Verify host keys before trusting a new connection.
  • Use least privilege for the account you connect with.
  • Protect private keys with strict file permissions.
  • Avoid unnecessary root access when a normal admin account will do.

If you are transferring sensitive configs or backups, remember that the copy itself may be secure while the destination permissions are not. A file copied securely can still be dangerous if it lands somewhere world-readable. That is why SSH and SCP should be paired with permissions checks, not used as a substitute for them.

Secure transport is only half the job. The other half is making sure the right person can read the file after it arrives.

These practices align with common guidance from the Cybersecurity and Infrastructure Security Agency on secure administration and access hardening. They also match the operational logic used in regulated environments where auditability and least privilege are mandatory.

What Is the Command Used to View Tasks from the Command Line Interface?

The command used to view tasks from the command line interface depends on what you mean by “tasks,” but in Linux administration the most common answer is to use process and service inspection commands such as ps, top, or systemctl status. If you are checking scheduled work, you may use crontab -l. If you are checking Windows-like scheduled tasks on a Linux host, you would usually inspect cron, systemd timers, or the relevant service manager instead of one single universal command.

That question comes up in SSH workflows because remote command execution is often used to inspect what a system is doing right now. For example, an admin might run ssh admin@server01 top -b -n 1 to capture a snapshot of running processes, or ssh admin@server01 systemctl list-units --type=service to review active services. The command you use should match the “task” you actually want to inspect.

If your goal is operational visibility, SSH is the transport and the Linux command is the inspector. That is exactly why remote command execution is so valuable in troubleshooting and maintenance.

Real-World Admin Workflow Example

A realistic SSH and SCP workflow starts with verification, moves to transfer, and ends with validation. Suppose you need to copy a config backup to a remote Ubuntu server, then confirm the service can read it. That is a normal maintenance task, and it is where SSH and SCP work best together.

  1. Connect and verify the target. Start with SSH to confirm the destination host is reachable and the account is valid.

    ssh admin@server01

    Once connected, confirm the directory exists and you have write access. A quick pwd or ls -ld /etc/app prevents surprises later.

  2. Transfer the file with SCP. Copy the config backup to the correct path using an absolute destination.

    scp app.conf.bak admin@server01:/etc/app/

    If the file is inside a directory tree, add -r. If the server uses a nondefault port, remember -P for SCP, not -p.

  3. Validate permissions and ownership. After the copy, SSH back in and inspect the file.

    ssh admin@server01 'ls -l /etc/app/app.conf.bak'

    Check whether the service user can read the file. A successful transfer is not enough if the application still cannot access the content.

  4. Confirm service behavior. Use a remote command to verify the app still starts or reloads cleanly.

    ssh admin@server01 'systemctl restart app && systemctl status app --no-pager'

    This step confirms the copied file works in context, not just on disk.

  5. Review logs if needed. If the service fails, inspect the log immediately while the change is still fresh.

    ssh admin@server01 'journalctl -u app -n 50 --no-pager'

    That is faster and more reliable than guessing based on the file transfer alone.

This workflow reduces downtime because it keeps the entire change path short and verifiable. You connect, move the file, validate access, and confirm the service outcome without jumping between tools or guessing which step failed.

Key Takeaway

  • SSH is the command for secure remote login and remote command execution.
  • SCP is the command for secure file transfer over SSH transport.
  • -p is the SSH port flag, while -P is the SCP port flag.
  • -r is required for directory copies with SCP.
  • Current OpenSSH releases may use SFTP mode under SCP by default, so legacy behavior is not guaranteed without -O.

Conclusion

SSH and SCP are foundational Linux commands for secure administration, but they solve different problems. SSH gives you secure remote control, and SCP gives you secure file transfer. If you remember that split, most of the confusion disappears.

The practical takeaways are simple. Use the correct syntax, watch the port flag case, choose key-based authentication when possible, add -r for directories, and verify results after the transfer. If something fails, check connectivity first, then authentication, then the path, then permissions.

Modern OpenSSH behavior also matters. SCP may no longer behave exactly like the older protocol many admins learned years ago, and knowing when to use -O can save time in mixed environments. Match the tool to the job, and your remote-access workflow will stay faster, safer, and easier to support.

If you want to go deeper, practice these commands in a lab and verify the output yourself. That hands-on repetition is what turns SSH and SCP from memorized syntax into reliable admin habits.

CompTIA®, Cisco®, Microsoft®, and OpenSSH are referenced for educational context; their names are trademarks or service marks of their respective owners.

[ FAQ ]

Frequently Asked Questions.

What is the difference between SSH and SCP commands in Linux?

SSH (Secure Shell) and SCP (Secure Copy Protocol) are both essential Linux commands used for secure data operations over a network, but they serve different purposes.

SSH is primarily used to establish a secure and encrypted interactive shell session with a remote machine, allowing users to execute commands and manage remote systems safely. Conversely, SCP is designed specifically for securely transferring files between local and remote hosts using the SSH protocol as its transport layer.

While SSH handles command execution and remote login, SCP focuses on copying files securely without exposing data in transit. Understanding this distinction helps in using each command appropriately depending on whether you need remote system access or secure file transfer.

How do I correctly use the SCP command to copy directories?

To copy directories using SCP, you must include the recursive flag -r. This option tells SCP to copy all files and subdirectories within the specified directory recursively.

The general syntax is: scp -r /path/to/local/dir user@remote_host:/path/to/remote/dir. Omitting the -r flag when copying directories results in errors or incomplete transfers because SCP treats the directory as a regular file.

Remember that SCP operates over SSH, so you need proper permissions and network access to the remote host. Using the recursive flag ensures that entire directory trees are copied securely without missing any content.

What are common causes of SCP file transfer failures and how can I troubleshoot them?

SCP file transfer failures often occur due to misconfigured command options, network issues, or permission problems on the remote host.

Common issues include using incorrect port flags (such as specifying the wrong port with -P), forgetting the recursive -r flag for directories, or lacking proper SSH authentication credentials. Additionally, network firewalls or blocked ports can prevent SCP from establishing a connection.

To troubleshoot, verify that the SSH service is running on the remote host, confirm the correct port number, ensure you have the right user permissions, and test connectivity with a simple SSH login before attempting SCP. Adding verbose output with -v can also help diagnose underlying problems.

Can I transfer files securely without exposing data in transit using SCP?

Yes, SCP transfers files securely by leveraging the SSH protocol, which encrypts all data transmitted between the local and remote hosts. This encryption prevents unauthorized interception or eavesdropping during file transfer.

Unlike traditional FTP or unsecured copy methods, SCP ensures confidentiality and integrity of your files in transit. This makes SCP a preferred choice for sensitive data transfers over untrusted networks.

However, it is crucial to use strong authentication methods such as key-based SSH authentication and avoid using plain-text passwords to maintain the highest security standards during SCP transfers.

What is the correct way to specify a non-default SSH port with SCP?

When using SCP to connect to a remote host on a non-default SSH port (not port 22), you must specify the port number with the uppercase -P option. This is a common point of confusion since SSH itself uses the lowercase -p flag for port specification.

The correct SCP syntax is: scp -P 2222 file.txt user@remote_host:/destination/path, where 2222 is the custom SSH port. Using the lowercase -p flag instead will not change the port and can cause connection failures.

Always double-check the port flag case sensitivity when troubleshooting SCP connection issues related to port numbers to avoid unnecessary support tickets.

Related Articles

Ready to start learning? Individual Plans →Team Plans →
Discover More, Learn More
Traceroute: Your Comprehensive Guide to Mapping Network Paths Discover how to use traceroute to diagnose network issues, visualize data paths,… Navigating Through Linux GUIs: A Comparative Guide to Graphical User Interfaces Discover how to choose the ideal Linux graphical user interface to enhance… Top 10 Cisco Commands : A Cheatsheet For Network Administrators Discover the top 10 Cisco commands to troubleshoot network issues quickly and… Adding a Drive to a ZFS System Discover proven strategies to safely expand your ZFS pool, prevent data loss,… SELinux for Enhanced Security: A Deep Dive into Mandatory Access Control Discover how SELinux enhances Linux security by enforcing strict access controls that… Kali Linux : The Complete Ethical Hacker's Toolbox Learn how Kali Linux streamlines security testing with a purpose-built platform that…
FREE COURSE OFFERS