Linux scp remote server

Classic SysAdmin: How to Securely Transfer Files Between Servers with scp

This is a classic article written by Swapnil Bhartiya from the Linux.com archives. For more great SysAdmin tips and techniques check out our free intro to Linux course.

If you run a live or home server, moving files between local machines or two remote machines is a basic requirement. There are many ways to achieve that. In this article, we talk about scp (secure copy command) that encrypts the transferred file and password so no one can snoop. With scp you don’t have to start an FTP session or log into the system.

The scp tool relies on SSH (Secure Shell) to transfer files, so all you need is the username and password for the source and target systems. Another advantage is that with SCP you can move files between two remote servers, from your local machine in addition to transferring data between local and remote machines. In that case you need usernames and passwords for both servers. Unlike Rsync, you don’t have to log into any of the servers to transfer data from one machine to another.

This tutorial is aimed at new Linux users, so I will keep things as simple as possible. Let’s get started.

Copy a single file from the local machine to a remote machine:

The scp command needs a source and destination to copy files from one location to another location. This is the pattern that we use:

scp localmachine/path_to_the_file username@server_ip:/path_to_remote_directory

In the following example I am copying a local file from my macOS system to my Linux server (Mac OS, being a UNIX operating system has native support for all UNIX/Linux tools).

scp /Volumes/MacDrive/Distros/fedora.iso swapnil@10.0.0.75:/media/prim_5/media_server/

Here, ‘swapnil’ is the user on the server and 10.0.0.75 is the server IP. It will ask you to provide the password for that user, and then copy the file securely.

I can do the same from my local Linux machine:

scp /home/swapnil/Downloads/fedora.iso swapnil@10.0.0.75:/media/prim_5/media_server/

If you are running Windows 10, then you can use Ubuntu bash on Windows to copy files from the Windows system to Linux server:

scp /mnt/c/Users/swapnil/Downloads/fedora.iso swapnil@10.0.0.75:/media/prim_5/ media_server/

Copy a local directory to a remote server:

If you want to copy the entire local directory to the server, then you can add the -r flag to the command:

scp -r localmachine/path_to_the_directory username@server_ip:/path_to_remote_directory/

Make sure that the source directory doesn’t have a forward slash at the end of the path, at the same time the destination path *must* have a forward slash.

Читайте также:  Перейти в корневую папку linux

Copy all files in a local directory to a remote directory

What if you only want to copy all the files inside a local directory to a remote directory? It’s simply, just add a forward slash and * at the end of source directory and give the path of destination directory. Don’t forget to add the -r flag to the command:

scp -r localmachine/path_to_the_directory/* username@server_ip:/path_to_remote_directory/

Copying files from remote server to local machine

If you want to make a copy of a single file, a directory or all files on the server to the local machine, just follow the same example above, just exchange the place of source and destination.

Copy a single file:

scp username@server_ip:/path_to_remote_directory local_machine/path_to_the_file

Copy a remote directory to a local machine:

scp -r username@server_ip:/path_to_remote_directory local-machine/path_to_the_directory/

Make sure that the source directory doesn’t have a forward slash at the end of the path, at the same time the destination path *must* have a forward slash.

Copy all files in a remote directory to a local directory:

scp -r username@server_ip:/path_to_remote_directory/* local-machine/path_to_the_directory/

Copy files from one directory of the same server to another directory securely from local machine

Usually I ssh into that machine and then use rsync command to perform the job, but with SCP, I can do it easily without having to log into the remote server.

Copy a single file:

scp username@server_ip:/path_to_the_remote_file username@server_ip:/ path_to_destination_directory/

Copy a directory from one location on remote server to different location on the same server:

scp username@server_ip:/path_to_the_remote_file username@server_ip:/ path_to_destination_directory/

Copy all files in a remote directory to a local directory

scp -r username@server_ip:/path_to_source_directory/* usdername@server_ip:/ path_to_the_destination_directory/

Copy files from one remote server to another remote server from a local machine

Currently I have to ssh into one server in order to use rsync command to copy files to another server. I can use SCP command to move files between two remote servers:

Usually I ssh into that machine and then use rsync command to perform the job, but with SCP, I can do it easily without having to log into the remote server.

Copy a single file:

scp username@server1_ip:/path_to_the_remote_file username@server2_ip:/ path_to_destination_directory/

Copy a directory from one location on a remote server to different location on the same server:

scp username@server1_ip:/path_to_the_remote_file username@server2_ip:/ path_to_destination_directory/

Copy all files in a remote directory to a local directory

scp -r username@server1_ip:/path_to_source_directory/* username@server2_ip:/ path_to_the_destination_directory/

Conclusion

As you can see, once you understand how things work, it will be quite easy to move your files around. That’s what Linux is all about, just invest your time in understanding some basics, then it’s a breeze!

Ready to continue your Linux journey? Check out our free intro to Linux course!

Источник

Читайте также:  Lemp linux mysql php

Secure Copy Protocol (SCP) in Linux: A Guide to File Transfer from Local to Remote Server

Secure Copy Protocol (SCP) in Linux: A Guide to File Transfer from Local to Remote Server

We often need to move files from a local machine to a remote server or vice versa, especially when managing or deploying code on servers. One efficient way to do this is through Secure Copy Protocol (SCP), a tool that’s built into the SSH (Secure Shell) protocol. This guide will walk you through how to use SCP on Linux platforms like Ubuntu, Linux Mint, and Debian, among others.

SSH, or Secure Shell, is a protocol that allows secure access to remote computers.

Understanding SSH and SCP: The Basis for Secure File Transfers

SSH , an acronym for Secure Shell, is a protocol used to access remote computers securely. Built into this SSH ecosystem is the Secure Copy Protocol ( SCP ), an effective tool for transferring files between local and remote computers. Its syntax closely resembles the ‘cp’ (copy) command, but it’s geared towards remote file transfer.

Other file transfer applications, such as SFTP and rsync , also harness the power of SSH to secure their file transfers. These applications open up the possibility of securely copying files from local to remote servers, and vice versa.

Introducing the SCP Command on Linux

The SCP command in Linux is a utility for transferring files between remote computers. It’s included by default in most Linux and Unix distributions, including Linux Ubuntu, Linux Mint, Linux Debian, Arch Linux, etc., and is part of the OpenSSH packages.

SCP is your secure solution for transferring files between a remote location and a host, or from your local computer to a remote server, or even between two remote locations.

Using Linux SCP via SSH: A Guide on How to Copy Files

The difference between the cp command and the scp command is that the latter requires you to specify the remote host’s DNS name or IP address and provide username credentials. Here’s how to use SCP for copying files from your local machine to a remote server, and from a remote server to your local machine.

Linux SCP from Local to Remote: How to Copy a Single File

Copying a single file from your local computer to a remote computer using SCP involves the following syntax:

# From current folder you don't need to specify the file path  scp myfile.txt [email protected]:/remote/folder/  # From any folder from your local computer, write the full local path to the file  scp /full/path/to/myfile.txt [email protected]:/remote/folder/ 

Let’s break down these two examples:

In this first example, we’re using SCP to transfer a file called myfile.txt from the current working directory on our local machine to a folder on the remote server.

  • myfile.txt is the name of the file we want to transfer.
  • username is the username for your account on the remote server.
  • remoteserver is the DNS name or IP address of the remote server.
  • /remote/folder/ is the path of the directory on the remote server where you want to transfer the file.
Читайте также:  Альт линукс vmware tools

By running this command, myfile.txt will be securely copied from your local machine to the specified folder on the remote server.

In the second example, we’re doing the same thing but the file we want to transfer isn’t in our current directory. Instead, it’s located elsewhere on our local machine, and we need to specify its full path.

  • /full/path/to/myfile.txt is the absolute path to the file on your local machine. It provides complete information about the file’s location starting from the root directory.

So, when you run this command, again, myfile.txt will be securely transferred from the specified path on your local machine to the designated folder on the remote server.

Both of these SCP commands offer a simple yet powerful way to perform secure file transfer from a local machine to a remote server using SSH, irrespective of where your file is stored locally.

If the target folder on remote host ( /remote/folder/ ) is not specified, it will copy the file to the remote user’s home directory.

The user specified with username need to exist and have write permission to /remote/folder / in the remote system.

Linux SCP from Remote to Local: Copying a Single File

You can also copy a file from a remote server to your local computer using SCP. The syntax is similar, only that you invert the order of the local and remote information:

 # From remote to current local folder  scp use[email protected]:/remote/folder/remotefile.txt localfile.txt  # From remote to local but specifying the local folder target to save the file  scp [email protected]:/remote/folder/remotefile.txt /path/to/local/folder/localfile.txt 

Using a . (dot) as the copy target (in our example replacing localfile.txt to . ), will copy the remote file to the current working directory using the original filename, in this case: remotefile.txt

Copying Several Files Using SCP

You can copy multiple files from your local machine to a remote server, or vice versa. You can either specify each file as a parameter, or use wildcards:

 # Specify each file  scp myfile.txt /local/folder/myfile2.txt [email protected]:/remote/folder/  # Use a wildcard to copy several files from remote  scp [email protected]:/remote/folder/* . 

Recursively Copy Files and Folders from Local to Remote using SCP

To copy recursively files and folders from local to a remote server, you need to use the option -r .

This is particularly useful when setting up a project structure on a remote server:

Copy files using SCP with PEM or CER credential

If you don’t want to type the remote user password when are copying files, you can use a .pem ou .cer file to inform you secure credential to scp ssh connection.

Note that you still need to write the username on the command:

Final considerations

The SCP command in Linux is one of the most efficient and secure methods for transferring files between local and remote computers connected over a network. Whether you need to SCP from local to remote or perform SSH file copy operations, this guide has you covered.

Always remember to verify your SCP operations to ensure the correct files have been transferred.

Источник

Оцените статью
Adblock
detector