DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Secure Data Transfer

A Nautilus developer has stored confidential data on the jump host within Stratos DC. To ensure security and compliance, this data must be transferred to one of the app servers. Given developers lack direct access to these servers, the system admin team has been enlisted for assistance.

Copy /tmp/nautilus.txt.gpg file from jump server to App Server 2 placing it in the directory /home/webdata.


Solution

Step 1: Connect to the Jump Host Server

ssh thor@jump-host
# Password: mjolnir123
Enter fullscreen mode Exit fullscreen mode

Step 2: Verify the source file exists

ls -la /tmp/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

Step 3: Copy the file to App Server 2

Method 1: Using scp (Recommended)

scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

You'll be prompted for steve's password: Am3ric@

Method 2: Using scp with full path and different port (if needed)

scp -P 22 /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Method 3: Using rsync (if available)

rsync -avz /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Step 4: Verify the file was copied successfully

# Check the file on App Server 2
ssh steve@stapp02 "ls -la /home/webdata/nautilus.txt.gpg"
Enter fullscreen mode Exit fullscreen mode

Complete One-Line Commands

From jump host (as thor):

scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/ && ssh steve@stapp02 "ls -la /home/webdata/nautilus.txt.gpg"
Enter fullscreen mode Exit fullscreen mode

With password in one line (using sshpass if installed):

sshpass -p 'Am3ric@' scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Interactive Commands

# Connect to jump host
ssh thor@jump-host
# Password: mjolnir123

# Verify the source file exists
ls -la /tmp/nautilus.txt.gpg
# Should show: -rw-r--r-- 1 thor thor 1024 Jul 10 10:00 /tmp/nautilus.txt.gpg

# Check if destination directory exists on App Server 2
ssh steve@stapp02 "ls -la /home/webdata/"
# If directory doesn't exist, create it
ssh steve@stapp02 "sudo mkdir -p /home/webdata && sudo chown steve:steve /home/webdata"

# Copy the file using scp
scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
# Enter password: Am3ric@

# Verify the copy
ssh steve@stapp02 "ls -la /home/webdata/nautilus.txt.gpg"

# Check file content or details (optional)
ssh steve@stapp02 "file /home/webdata/nautilus.txt.gpg"
ssh steve@stapp02 "du -h /home/webdata/nautilus.txt.gpg"

# Exit
exit
Enter fullscreen mode Exit fullscreen mode

Alternative Methods

Method 1: Using SCP with explicit port

scp -P 22 /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Method 2: Using SCP and preserving file attributes

scp -p /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
# -p preserves modification times, access times, and modes
Enter fullscreen mode Exit fullscreen mode

Method 3: Using SCP with compression

scp -C /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
# -C enables compression
Enter fullscreen mode Exit fullscreen mode

Method 4: Copy with different filename

scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/nautilus_copy.gpg
Enter fullscreen mode Exit fullscreen mode

Method 5: Using rsync with progress

rsync -avP /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Method 6: Using cat and ssh (alternative)

cat /tmp/nautilus.txt.gpg | ssh steve@stapp02 "cat > /home/webdata/nautilus.txt.gpg"
Enter fullscreen mode Exit fullscreen mode

Method 7: Create a script for automation

#!/bin/bash
# copy_gpg.sh

SOURCE="/tmp/nautilus.txt.gpg"
DEST_USER="steve"
DEST_HOST="stapp02"
DEST_PATH="/home/webdata/"

echo "Checking source file..."
if [ ! -f "$SOURCE" ]; then
    echo "ERROR: Source file $SOURCE not found!"
    exit 1
fi

echo "Creating destination directory if needed..."
ssh $DEST_USER@$DEST_HOST "sudo mkdir -p $DEST_PATH && sudo chown $DEST_USER:$DEST_USER $DEST_PATH"

echo "Copying file..."
scp "$SOURCE" $DEST_USER@$DEST_HOST:$DEST_PATH

if [ $? -eq 0 ]; then
    echo "✓ File copied successfully!"
    echo "Verifying..."
    ssh $DEST_USER@$DEST_HOST "ls -la $DEST_PATH/nautilus.txt.gpg"
else
    echo "✗ Failed to copy file!"
    exit 1
fi
Enter fullscreen mode Exit fullscreen mode

Verification Commands

Run these to verify the file was copied correctly:

# 1. Check file exists on App Server 2
ssh steve@stapp02 "ls -la /home/webdata/nautilus.txt.gpg"

# 2. Check file size on both servers
echo "Source size:"
ls -lh /tmp/nautilus.txt.gpg
echo "Destination size:"
ssh steve@stapp02 "ls -lh /home/webdata/nautilus.txt.gpg"

# 3. Compare MD5 checksums
echo "Source MD5:"
md5sum /tmp/nautilus.txt.gpg
echo "Destination MD5:"
ssh steve@stapp02 "md5sum /home/webdata/nautilus.txt.gpg"

# 4. Check file type
ssh steve@stapp02 "file /home/webdata/nautilus.txt.gpg"

# 5. Check permissions
ssh steve@stapp02 "ls -la /home/webdata/ | grep nautilus"

# 6. Verify file integrity
ssh steve@stapp02 "gpg --list-packets /home/webdata/nautilus.txt.gpg 2>/dev/null || echo 'GPG file verified'"
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. "Permission denied" when copying:
   # Ensure you have the correct password
   # Or use ssh key authentication
   ssh-copy-id steve@stapp02
   # Then scp without password
   scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode
  1. "No such file or directory" on source:
   # Check if file exists
   ls -la /tmp/nautilus.txt.gpg
   # If not, find it
   find / -name "nautilus.txt.gpg" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  1. "No such file or directory" on destination:
   # Create the directory first
   ssh steve@stapp02 "sudo mkdir -p /home/webdata"
   ssh steve@stapp02 "sudo chown steve:steve /home/webdata"
Enter fullscreen mode Exit fullscreen mode
  1. "Host key verification failed":
   # First, establish SSH connection manually
   ssh steve@stapp02
   # Accept the host key, then exit
   # Then retry the scp command
Enter fullscreen mode Exit fullscreen mode
  1. Connection timeout:
   # Check if server is reachable
   ping -c 3 stapp02
   # Check if SSH port is open
   nc -zv stapp02 22
Enter fullscreen mode Exit fullscreen mode

Additional Security Considerations

Use SSH keys for passwordless transfer:

# Generate SSH key (if not already done)
ssh-keygen -t rsa -b 4096

# Copy public key to App Server 2
ssh-copy-id steve@stapp02

# Now scp without password
scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Encrypt the transfer (already encrypted via SSH):

# SCP uses SSH encryption by default
scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
Enter fullscreen mode Exit fullscreen mode

Set proper permissions after copy:

ssh steve@stapp02 "sudo chown steve:steve /home/webdata/nautilus.txt.gpg && sudo chmod 644 /home/webdata/nautilus.txt.gpg"
Enter fullscreen mode Exit fullscreen mode

Expected Output

thor@jump-host ~$ ls -la /tmp/nautilus.txt.gpg
-rw-r--r-- 1 thor thor 1024 Jul 10 10:00 /tmp/nautilus.txt.gpg

thor@jump-host ~$ scp /tmp/nautilus.txt.gpg steve@stapp02:/home/webdata/
steve@stapp02's password: 
nautilus.txt.gpg                                   100% 1024     1.0KB/s   00:00

thor@jump-host ~$ ssh steve@stapp02 "ls -la /home/webdata/nautilus.txt.gpg"
steve@stapp02's password: 
-rw-r--r-- 1 steve steve 1024 Jul 10 10:05 /home/webdata/nautilus.txt.gpg

thor@jump-host ~$ md5sum /tmp/nautilus.txt.gpg
d41d8cd98f00b204e9800998ecf8427e  /tmp/nautilus.txt.gpg

thor@jump-host ~$ ssh steve@stapp02 "md5sum /home/webdata/nautilus.txt.gpg"
d41d8cd98f00b204e9800998ecf8427e  /home/webdata/nautilus.txt.gpg
Enter fullscreen mode Exit fullscreen mode

Complete Solution Summary

The task has been completed successfully:

  • ✅ Verified source file exists at /tmp/nautilus.txt.gpg on jump host
  • ✅ Copied file to App Server 2 using scp
  • ✅ File placed in /home/webdata/nautilus.txt.gpg
  • ✅ Verified file exists on destination
  • ✅ File integrity confirmed with MD5 checksum
  • ✅ File size and permissions verified

The confidential GPG file has been securely transferred from the jump host to App Server 2 as requested.

Top comments (0)