27.06.2025

How to Copy Files and Run Commands via SSH on Windows and Linux

Connecting to a server via SSH is one of the most essential and widely used methods for managing Unix-like (*nix) servers remotely. Often, administrators need to upload files to a remote server or download files from it, and SSH provides a secure and reliable way to perform these tasks when no other direct access methods are available. Fortunately, SSH not only allows command execution but also supports secure file transfer capabilities.

On Linux and other Unix-based systems, the scp (secure copy) command is commonly used to transfer files over an encrypted SSH connection, ensuring data confidentiality and integrity during transfer. For Windows users, this functionality is available through pscp.exe, a command-line utility included with the popular PuTTY SSH client. Both tools enable efficient and secure file transfers between your local machine and the remote server, making SSH a versatile solution for server management and file operations.

Using the Linux OS

Execute the following commands:

scp [modifier] [source] [target_directory]

If a remote server serves as the source or target directory, execute the following commands:

[user]@[server]:[file_path]

After running the command, you need to enter the password for the remote server account.

To sum up, you can copy the local file named: /home/user/file.tgz to the root user home directory of the remote server 123.123.123.123 executing the following command:

scp /home/user/file.tgz root@123.123.123.123:/root

To download the same file from a remote server:

scp root@123.123.123.123:/root/file.tgz /home/user

You can copy several files at once. To do this, you must add them as the source. Separate them by a space – the last parameter will be considered as the target directory. For example, to upload file1 files.tgz and file2.tgz from the local directory to the remote server you have to execute the command:

scp file1.tgz file2.tgz root@123.123.123.123:/root

To copy the directory, you will need to use the –r command modifier. Copy the local directory /home/user/dir to the remote server:

scp-r /home/user/dir root@123.123.123.123:/root

When the SSH server is running on a non-default port, we need to get the help of the -P option. If you need to use port 10022:

scp-P 10022 /home/user/file.tgz root@123.123.123.123:/root

To find out what other modifiers you can use, simply execute scp without parameters and read the help.

Using the Windows OS

When using the Windows operating system and Putty as a client, the command formatting remains the same. However, we will change the name of the executable file. We need to use a syntax for specifying paths to Windows files and directories when mentioning the source or target directories. Run the command line (cmd.exe) or PowerShell, open the directory with the pscp .exe file and enter the command:

pscp.exe C: Tempfile.tgz root@123.123.123.123:/root

If you run it from another folder, you will need to specify the full path to pscp.exe. If any of the paths contain spaces, we need to use double quotes — "file path”:

“C:Program FilesPuttypscp.exe” C:Tempfile.tgz root@123.123.123.123:/root

Simply execute pscp (similarly to scp) without parameters and read the help on the modifiers as well as the command syntax information.

Running commands on a remote server using the SSH connection

SSH protocol supports both running interactive sessions and regular commands and scripts on a remote server.

Using the Linux OS

Command syntax:

ssh [user]@[server] ‘[command]’

Enter the password of the specified user and get the command output in the SSH console, if any.

For example, we can find information about the operating system installed on the remote server:

ssh root@123.123.123.123 ‘uname -a’

To run multiple commands using a single connection, you can use the “;” character as a separator. Check the network settings and active network connections on the remote server:

ssh root@123.123.123.123 ‘ifconfig; netstat -anp tcp’

If you need to run a local script file on a remote server: run the command interpreter (in the script execution mode). It can be bash with the-s key and pass the script file to it for the standard input. Take a look at the example below:

ssh root@123.123.123.123 ‘bash -s’ < /home/user/myscript.sh

The local file: /home/user/myscript.sh will be executed on a remote server.

Running the SSH command without parameters allows you to see a brief syntax reference and a list of additional modifiers that allow you to extend the command's functionality.

Using the Windows OS

To connect to the remote server, we will use the Putty client with its executable plink.exe. File, if our computer supports Windows. Use the command line (cmd.exe) or PowerShell to work with this file.

To run the command on a remote server, use the following syntax:

plink.exe [server] - ssh-l [user] “[command]”

Check the network interfaces configuration:

plink.exe 123.123.123.123 -ssh-l root “ifconfig”

plink.exe allows you to use “;” as a separator to run multiple commands, likewise the SSH command in Linux:

plink.exe 123.123.123.123 -ssh-l root “ifconfig; netstat-anp tcp”

You can run commands from a local file using an additional key -m:

plink.exe 123.123.123.123 -ssh-l root-m “C:Tempmyscript.sh”

By running the plink.exe command without parameters, you can find a brief syntax reference and a list of additional command modifiers.