How to Use SCP Command to Transfer Files Securely

Apache-Servers

An SCP (secure copy) is a command-line utility that allows you to securely copy files and directories between two locations.

With scp, you can copy a file or directory:

  • From your local system to a remote system.
  • From a remote system to your local system.
  • Between two remote systems from your local system.

When transferring data with scp, both the files and password are encrypted so that anyone snooping on the traffic doesn’t get anything sensitive.

In this tutorial, we will show you how to use the scp command through practical examples and detailed explanations of the most common scp options.

SCP Command Syntax

Before going into how to use the scp command, let’s start by reviewing the basic syntax.

The scp command syntax take the following form:

scp [OPTION] [user@]SRC_HOST:]file1 [user@]DEST_HOST:]file2

Copy

  • OPTION – scp options such as cipher, ssh configuration, ssh port, limit, recursive copy …etc.
  • [user@]SRC_HOST:]file1 – Source file.
  • [user@]DEST_HOST:]file2 – Destination file

Local files should be specified using an absolute or relative path, while remote file names should include a user and host specification.

scp provides a number of options that control every aspect of its behavior. The most widely used options are:

  • -P – Specifies the remote host ssh port.
  • -p – Preserves files modification and access times.
  • -q – Use this option if you want to suppress the progress meter and non-error messages.
  • -C – This option forces scp to compresses the data as it is sent to the destination machine.
  • -r – This option tells scp to copy directories recursively.

Before you Begin

The scp command relies on ssh for data transfer, so it requires an ssh key or password to authenticate on the remote systems.

The colon (:) is how scp distinguish between local and remote locations.

To be able to copy files, you must have at least read permissions on the source file and write permission on the target system.

Be careful when copying files that share the same name and location on both systems, scp will overwrite files without warning.

When transferring large files, it is recommended to run the scp command inside a screen or tmux session.

Copy a Local File to a Remote System with the scp Command

To copy a file from a local to a remote system run the following command:

scp file.txt [email protected]:/remote/directory

Where file.txt is the name of the file we want to copy, remote_username is the user on the remote server, 10.10.0.2 is the server IP address. The /remote/directory is the path to the directory you want to copy the file to. If you don’t specify a remote directory, the file will be copied to the remote user home directory.

You will be prompted to enter the user password, and the transfer process will start.

[email protected]'s password: file.txt 100% 0 0.0KB/s 00:00

Omitting the filename from the destination location copies the file with the original name. If you want to save the file under a different name, you need to specify the new file name:

scp file.txt [email protected]:/remote/directory/newfilename.txt

If SSH on the remote host is listening on a port other than the default 22 then you can specify the port using the -P argument:

scp -P 2322 file.txt [email protected]:/remote/directory

The command to copy a directory is much like as when copying files. The only difference is that you need to use the -r flag for recursive.

To copy a directory from a local to remote system, use the -r option:

scp -r /local/directory [email protected]:/remote/directory

Copy a Remote File to a Local System using the scp Command

To copy a file from a remote to a local system, use the remote location as a source and local location as the destination.

For example to copy a file named file.txt from a remote server with IP 10.10.0.2 run the following command:

scp [email protected]:/remote/file.txt /local/directory

If you haven’t set a passwordless SSH login to the remote machine, you will be asked to enter the user password.

Copy a File Between Two Remote Systems using the scp Command

Unlike rsync , when using scp you don’t have to log in to one of the servers to transfer files from one to another remote machine.

The following command will copy the file /files/file.txt from the remote host host1.com to the directory /files on the remote host host2.com.

scp [email protected]:/files/file.txt [email protected]:/files

You will be prompted to enter the passwords for both remote accounts. The data will be transfer directly from one remote host to the other.

To route the traffic through the machine on which the command is issued, use the -3 option:

scp -3 [email protected]:/files/file.txt [email protected]:/files

HOW TO INSTALL PHP 8 ON UBUNTU 20.04

php

PHP is one of the most widely used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP.

PHP 8.0 is the latest major release of the PHP language. It introduces several breaking changes, performance improvements, and lots of new features such as named arguments, JIT compiler, union types, match expression, and more.

This article will show you how to install PHP 8 on Ubuntu 20.04 and integrate it with Nginx and Apache.

At the time of writing, the default Ubuntu 20.04 repositories include PHP 7.4 version. We’ll install PHP from the ondrej/php PPA repository.

Before upgrading to or installing PHP 8, make sure that your applications support it.

The same steps apply for Ubuntu 18.04 and all Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS.

Enabling PHP Repository

Ondřej Surý, a Debian developer, maintains a repository that includes multiple PHP versions. To enable the repository , run:

sudo apt install software-properties-commonsudo add-apt-repository ppa:ondrej/php

Once the PPA is enabled, you can install PHP 8.

Installing PHP 8.0 with Apache

If you’re using Apache as a web server, you can run PHP as an Apache module or PHP-FPM.

Install PHP as Apache Module

Installing PHP as an Apache module is a straightforward task:

sudo apt updatesudo apt install php8.0 libapache2-mod-php8.0

Once the packages are installed, restart Apache for the PHP module to get loaded:

sudo systemctl restart apache2

Configure Apache with PHP-FPM

Php-FPM is a FastCGI process manager for PHP. Run the following command to install the necessary packages:

sudo apt updatesudo apt install php8.0-fpm libapache2-mod-fcgid

By default PHP-FPM is not enabled in Apache. To enable it, run:

sudo a2enmod proxy_fcgi setenvifsudo a2enconf php8.0-fpm

To activate the changes, restart Apache:

systemctl restart apache2

Installing PHP 8.0 with Nginx

Nginx doesn’t have built-in support for processing PHP files. We’ll use PHP-FPM (“fastCGI process manager”) to handle the PHP files.

Run the following commands to install PHP and PHP FPM packages:

sudo apt updatesudo apt install php8.0-fpm

Once the installation is completed, the FPM service will start automatically. To check the status of the service, run

systemctl status php8.0-fpm
● php8.0-fpm.service - The PHP 8.0 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php8.0-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-12-03 16:10:47 UTC; 6s ago

You can now edit the Nginx server block and add the following lines so that Nginx can process PHP files:

server { # . . . other code location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.0-fpm.sock; } }

Do not forget to restart the Nginx service so that the new configuration takes effect:

sudo systemctl restart nginx

Installing PHP extensions

PHP extensions are compiled libraries that extend the core functionality of PHP. Extensions are available as packages and can be easily installed with apt :

sudo apt install php8.0-[extname]

For example, to install MySQL and GD extensions, you would run the following command:

sudo apt install php8.0-mysql php8.0-gd

After installing a new PHP extension, do not forget to restart Apache or PHP FPM service, depending on your setup.

Testing PHP Processing

To test whether the web server is configured properly for PHP processing, create a new file named info.php inside the /var/www/html directory with the following code:

/var/www/html/info.php

<?php phpinfo();

Save the file, open your browser, and visit: http://your_server_ip/info.php

You’ll see information about your PHP configuration.