Securely Transferring Files Between Systems
When a host is running the sshd
service, it can also facilitate secure file transfers between systems. There are several commands available for this purpose:
- scp: For copying files.
- rsync: For synchronizing files.
- sftp: For transferring files using an FTP-like interface over SSH.
Using scp
to Securely Copy Files
The scp
(secure copy) command functions similarly to the cp
(copy) command for local files, but it also supports remote hosts. This allows you to copy files and directories to and from remote systems.
- Basic Syntax: To copy a file to a remote host, you would use the command:
scp /local/path/to/file user@remotehost:/remote/path/to/file
Example: Copying the/etc/hosts
file to the/tmp
directory onserver2
:scp /etc/hosts server2:/tmp
- Copying as Another User: To copy a file to your home directory on
server2
as theroot
user: scp root@server2:/etc/passwd ~
- Copying Directories: Use the
-r
option to copy an entire directory structure: scp -r /local/dir user@remotehost:/remote/dir
Example: Copying the/etc
directory fromserver2
to the/tmp
directory:scp -r server2:/etc /tmp
- Specifying Non-Default SSH Port: To connect to a non-default SSH port, use the
-P
option (note the uppercaseP
forscp
, whilessh
uses lowercase-p
): scp -P port_number /local/path/to/file user@remotehost:/remote/path/to/file
Using sftp
to Securely Transfer Files
The sftp
(SSH File Transfer Protocol) command provides an FTP-like interface for secure file transfers over SSH.
- Starting an
sftp
Session: - Open an
sftp
session to a remote server running thesshd
service: sftp user@remotehost
- Typical
sftp
Commands:put
to upload a file from your local system to the remote server.get
to download a file from the remote server to your local system.
sftp
session: sftp> put /local/path/to/file /remote/path/to/file
sftp> get /remote/path/to/file /local/path/to/file
- Local Directory: The local directory context is important. When you
put
a file, it is taken from the current local directory. When youget
a file, it is stored in the current local directory.
- Local Directory: The local directory context is important. When you
Using rsync
for File Synchronization
The rsync
command is a powerful tool for synchronizing files and directories between systems. It provides various options to control what is synchronized and how.
By leveraging these commands, you can securely and efficiently transfer files between systems, ensuring data integrity and security.
The rsync
command leverages SSH to synchronize files between a remote directory and a local directory. The primary advantage of synchronization is that only the differences between files are transferred, making the process efficient. For instance, if a 100-MiB file has only a few changed blocks since the last sync, only those changes will be transferred. This method is known as delta synchronization.
Common rsync
Options
Here are some frequently used rsync
options:
Option | Description |
---|---|
-r | Synchronizes the entire directory tree |
-l | Copies symbolic links as symbolic links |
-p | Preserves permissions |
-n | Performs a dry run, not actually synchronizing anything |
-a | Uses archive mode, ensuring that entire subdirectory trees and all file properties are synchronized |
-A | Uses archive mode and synchronizes Access Control Lists (ACLs) |
-X | Synchronizes SELinux context as well |
Using SFTP to Manage Files on a Remote Server
Add a Hostname: From a sudo shell, add a line to match the IP address of server2
to the hostname server2
.
Open an SFTP Session: From a terminal, type:
sftp student@server2
This opens an SFTP prompt onserver
List Files: Typels
to see the files in the current working directory on the remote server how Remote Directory: Typepwd
to display the current directory on the remote server Show Local Directory: Typelpwd
to display your local current directory Change Local Directory: Typelcd /tmp
to change the local directory to/tmp
Upload a File: Typeput /etc/hosts
to upload the/etc/hosts
file fromserver1
to the home directory of thestudent
user onserver2
Close SFTP Session: Typeexit
to close the SFTP session.
Configuring Key-Based Authentication for SSH
For enhanced security, especially when SSH is used over the Internet, it’s advisable to use public/private key authentication instead of passwords. This method is generally enabled by default due to its increased security.
Setting Up Key-Based Authentication
Generate Key Pair: On the client machine, generate a public/private key pair using:
ssh-keygen
Accept the default filename (~/.ssh/id_rsa
) and press Enter twice when prompted for a passphrase if you prefer not to use one.
Copy Public Key to Server: Use ssh-copy-id
to transfer the public key to the remote server:
ssh-copy-id user@server2
You will be prompted for the remote user’s password one last time.
Verify Key-Based Authentication: Test the setup by logging into the remote server:
ssh user@server2
You should be able to log in without entering a password.
Important Considerations
- The public key is stored in the
~/.ssh/authorized_keys
file on the server. - Multiple users can have their keys in the
authorized_keys
file; ensure not to overwrite this file to avoid disrupting other users’ access.
By following these steps, you can securely and efficiently transfer files between systems, manage remote files with SFTP, and enhance your SSH security with key-based authentication.
Revision: Transferring Files
Using scp
(Secure Copy Protocol)
scp
is used to securely copy files between hosts on a network.
Copy a file from the local system to a remote system:
scp /path/to/local/file username@remote_host:/path/to/remote/directory
Example:
scp /home/user/file.txt user@192.168.1.100:/home/user/
- Copy a file from a remote system to the local system:
scp username@remote_host:/path/to/remote/file /path/to/local/directory
Example:
scp user@192.168.1.100:/home/user/file.txt /home/user/
- Copy a directory recursively from the local system to a remote system:
scp -r /path/to/local/directory username@remote_host:/path/to/remote/directory
Example:
scp -r /home/user/mydir user@192.168.1.100:/home/user/
Using rsync
(Remote Sync)
rsync
is used for efficiently transferring and synchronizing files between systems.
- Synchronize a file from the local system to a remote system:
rsync -av /path/to/local/file username@remote_host:/path/to/remote/directory
Example:
rsync -av /home/user/file.txt user@192.168.1.100:/home/user/
- Synchronize a file from a remote system to the local system:
rsync -av username@remote_host:/path/to/remote/file /path/to/local/directory
Example:
rsync -av user@192.168.1.100:/home/user/file.txt /home/user/
- Synchronize a directory from the local system to a remote system:
rsync -av /path/to/local/directory username@remote_host:/path/to/remote/directory
Example:
rsync -av /home/user/mydir/ user@192.168.1.100:/home/user/mydir/
Using sftp
(SSH File Transfer Protocol)
sftp
is an interactive file transfer program, similar to ftp
, but uses SSH for security.
- Start an
sftp
session:
sftp username@remote_host
Example:
sftp user@192.168.1.100
- Use
put
command to upload a file from local to remote system:
sftp> put /path/to/local/file /path/to/remote/directory
Example:
sftp> put /home/user/file.txt /home/user/
- Use
get
command to download a file from remote to local system:
sftp> get /path/to/remote/file /path/to/local/directory
Example:
sftp> get /home/user/file.txt /home/user/
- Use
put -r
command to upload a directory recursively from local to remote system:
sftp> put -r /path/to/local/directory /path/to/remote/directory
Example:
sftp> put -r /home/user/mydir /home/user/
These commands cover various scenarios of file transfer which are essential for general system administration tasks.