What is SSH?
SSH (Secure Shell) is a protocol that provides secure access to remote systems over an encrypted network. It allows users to:
Log in to remote systems.
Execute commands remotely.
Transfer files securely.
Step-by-Step Guide to Enable SSH Between Two Linux Machines
Prerequisites
Two Linux machines with SSH installed (most distributions have SSH pre-installed).
Access to root or a user with sudo privileges on both machines.
1. Check SSH Installation
Verify that SSH is installed on both machines:
sudo systemctl status sshd
If not installed, install it using:
For Ubuntu/Debian
sudo apt install openssh-server
For RHEL/CentOS
sudo yum install openssh-server
2. Start and Enable SSH Service
Ensure the SSH service is active and enabled to start on boot:
sudo systemctl start sshd
sudo systemctl enable sshd
3. Configure SSH (Optional)
Modify the SSH configuration file for additional security or customization:
sudo nano /etc/ssh/sshd_config
Key settings to check or modify:
Port: Default is 22. You can change it to another port for security.
PermitRootLogin: Set to no to prevent root login.
PasswordAuthentication: Set to no to enforce key-based authentication.
After making changes, restart the SSH service:
sudo systemctl restart sshd
4. Generate SSH Key Pair
Generate a public-private key pair on the source machine:
ssh-keygen -t rsa -b 4096
The default location is ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key).
5. Share the Public Key
Copy the public key (id_rsa.pub) from the source machine to the remote machine:
ssh-copy-id user@remote_machine
Alternatively, manually append the public key to the ~/.ssh/authorized_keys file on the remote machine:
cat ~/.ssh/id_rsa.pub | ssh user@remote_machine "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
6. Test SSH Connectivity
Attempt to log in without a password:
ssh user@remote_machine
1. SCP (Secure Copy Protocol)
SCP is a simple command-line tool for securely transferring files between Linux systems over SSH. It encrypts data in transit.
Syntax
scp [options] source destination
Examples
Copy a file from local to remote server:
scp file.txt user@remote:/path/to/destination/
Copy a file from remote to local:
scp user@remote:/path/to/file.txt /local/destination/
Copy a directory recursively:
scp -r /local/directory/ user@remote:/path/to/destination/
Important Options
-r Recursively copy directories.
-C Enable compression for faster transfer of large files.
-v Verbose mode for debugging.
-p Preserve the permission and ownership
2. PSCP (PuTTY SCP)
PSCP is SCP's counterpart on Windows systems, provided by the PuTTY suite.
Setup
- Download PuTTY and ensure pscp.exe is in your PATH.
Syntax
pscp [options] source destination
Examples
Copy a file from local to remote server:
pscp file.txt user@remote:/path/to/destination/
Copy a file from remote to local:
pscp user@remote:/path/to/file.txt C:\local\destination\
Specify SSH port:
pscp -P 2222 file.txt user@remote:/path/to/destination/
Important Options
-r Recursively copy directories.
-q Quiet mode.
-C Enable compression.
-v Verbose/debug mode.
-p Preserve the permission and ownership
3. Rsync
Rsync is more advanced than SCP, allowing incremental transfers, bandwidth control, and more.
Syntax
rsync [options] source destination
Examples
Copy a file from local to remote server:
rsync file.txt user@remote:/path/to/destination/
Copy a directory recursively:
rsync -r /local/directory/ user@remote:/path/to/destination/
Sync files while preserving permissions and timestamps:
rsync -av /local/directory/ user@remote:/path/to/destination/
Use SSH for transfer:
rsync -e ssh file.txt user@remote:/path/to/destination/
Transfer only modified files:
rsync -u /local/directory/ user@remote:/path/to/destination/
Important Options
-r Recursively copy directories.
-a Archive mode: preserve permissions, timestamps, etc.
-z Enable compression for faster transfers.
-e Specify SSH as the transport protocol.
--progress Show detailed progress during transfer.
--bwlimit=<kB/s> Limit bandwidth usage.
-u Update: skip files that are newer on the destination.
Permissions and Metadata
SCP/PSCP: Permissions are preserved unless overridden (e.g., umask on the target).
Rsync: Use -a to preserve all metadata (ownership, permissions, timestamps).
Conclusion
By enabling SSH between Linux machines and mastering SCP, PSCP, and Rsync commands, you can securely transfer files and manage systems effectively. Understanding these tools not only streamlines administrative tasks but also ensures security in your operations.
Top comments (1)
Simplify Your Workflow with SSH and Secure File Transfer Tools