Post

Mac Users: SSH & SCP Hands-On Guide (Week 2)

Mac Users: SSH & SCP Hands-On Guide (Week 2)

Mac Users: SSH & SCP Hands-On Guide (Week 2)

Windows users have WSL. Mac users use Docker instead — same commands, same learning.


Prerequisites

  • Docker installed and running
  • Terminal open

Step 1: Create a Working Directory

Open Terminal and run:

1
2
mkdir docker_ssh
cd docker_ssh

All files for this exercise go here. Keep Terminal open in this folder for the rest of the guide.


Step 2: Create the Dockerfile

Inside docker_ssh, create a file called Dockerfile (no extension, capital D):

1
nano Dockerfile

Paste the following, then save with Ctrl+O, Enter, Ctrl+X:

1
2
3
4
5
6
7
8
9
10
11
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y openssh-server && \
    mkdir /run/sshd && \
    echo 'root:password' | chpasswd && \
    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
    ssh-keygen -A

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

Confirm the file is there:

1
2
ls
# Dockerfile

Step 3: Build and Start the SSH Server

1
2
3
4
5
6
7
8
9
# Build the image (takes ~1 min first time)
docker build -t ssh-practice .

# Run it as a background container
docker run -d --name ssh-practice -p 2222:22 ssh-practice

# Confirm it's running
docker ps
# You should see ssh-practice with port 0.0.0.0:2222->22/tcp

Your practice “remote server” is now running at localhost:2222.

  • Username: root
  • Password: password

Step 4: SSH with Password

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Connect using password
ssh root@localhost -p 2222
# Type: password (you won't see it as you type — that's normal)

# You're inside the server now. Try a few commands:
whoami
# root

ls /
# bin  boot  dev  etc  home  ...

pwd
# /root

# Done — exit the server
exit

Step 5: SSH with Keys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. Generate a key pair on your Mac (run this in your Mac Terminal, not inside the container)
ssh-keygen -t ed25519 -C "practice-key"
# Press Enter for all three prompts (default location, no passphrase)

# This creates two files:
# ~/.ssh/id_ed25519       ← private key (never share this)
# ~/.ssh/id_ed25519.pub   ← public key (this goes on the server)

# 2. Try logging in with key BEFORE copying it to the server — it will FAIL
ssh -i ~/.ssh/id_ed25519 root@localhost -p 2222
# Permission denied (publickey) — expected! Key not on server yet.

# 3. Copy your public key to the server (last time you'll need the password)
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 root@localhost
# Type: password

# 4. Try again with key — no password this time
ssh -i ~/.ssh/id_ed25519 root@localhost -p 2222
# Logged in immediately — no password prompt!

exit

Why step 2 fails on purpose: The server doesn’t know your public key yet. Think of it like this — public key = lock (goes on the server), private key = key (stays on your Mac). ssh-copy-id installs your lock on the server. After that, your key opens it.


Step 6: SCP — Transfer Files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 1. SSH in and create a test file on the server
ssh root@localhost -p 2222
touch testfile.txt
echo "this file came from the server" > testfile.txt
exit

# 2. Download the file using SCP with password
scp -P 2222 root@localhost:~/testfile.txt .
# Type password when prompted

# 3. Download the file using SCP with -i (key, no password)
scp -i ~/.ssh/id_ed25519 -P 2222 root@localhost:~/testfile.txt testfile_key.txt

# 4. Download without -i (key is auto-detected from default location ~/.ssh/id_ed25519)
scp -P 2222 root@localhost:~/testfile.txt testfile_auto.txt

# Confirm all three copies are here
ls -la testfile*.txt

# Upload a file from your Mac to the server
echo "this file came from my Mac" > fromMac.txt
scp -P 2222 fromMac.txt root@localhost:~/

# SSH in to confirm it arrived
ssh root@localhost -p 2222
ls ~/fromMac.txt
cat ~/fromMac.txt
exit

Remember: SSH uses -p (lowercase) for port. SCP uses -P (uppercase). Easy to mix up!


Step 7: Run a Command Without Logging In

1
2
3
4
# Run a single command on the server without an interactive session
ssh root@localhost -p 2222 "df -h"
ssh root@localhost -p 2222 "whoami"
ssh root@localhost -p 2222 "ls /etc"

This is how scripts automate tasks on remote servers without needing a human to log in.


Step 8: Cleanup

1
2
docker stop ssh-practice
docker rm ssh-practice

To start again, run the build and run commands from Step 3:

1
2
3
cd docker_ssh
docker build -t ssh-practice .
docker run -d --name ssh-practice -p 2222:22 ssh-practice

The image is cached so the build will be near-instant. Note: files you created inside the container will be gone — that’s expected. Files on your Mac (docker_ssh/) are unaffected.


Exercises

Exercise 1: Connect and Explore

1
2
3
4
5
1. SSH into the container using password
2. Run: ls /etc
3. Run: whoami
4. Run: pwd
5. Exit

Answer:

1
2
3
4
5
ssh root@localhost -p 2222
ls /etc
whoami
pwd
exit

Exercise 2: Key Setup

1
2
3
4
1. Generate an ed25519 key pair
2. Attempt login with key — confirm it fails
3. Copy key to server
4. Login with key — confirm it works without a password

Answer:

1
2
3
4
ssh-keygen -t ed25519 -C "practice-key"
ssh -i ~/.ssh/id_ed25519 root@localhost -p 2222       # fails
ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 2222 root@localhost
ssh -i ~/.ssh/id_ed25519 root@localhost -p 2222       # succeeds

Exercise 3: Create and Download a File

1
2
3
4
5
6
1. SSH into the container
2. Create: echo "hello from server" > message.txt
3. Exit
4. SCP the file to your Mac using -i
5. SCP the file again without -i (save as message2.txt)
6. Confirm both landed: ls -la message.txt message2.txt

Answer:

1
2
3
4
5
6
7
8
ssh root@localhost -p 2222
echo "hello from server" > message.txt
exit

scp -i ~/.ssh/id_ed25519 -P 2222 root@localhost:~/message.txt .
scp -P 2222 root@localhost:~/message.txt message2.txt

ls -la message.txt message2.txt

Exercise 4: Upload a File to the Server

1
2
3
1. Create on your Mac: echo "hello from mac" > fromMac.txt
2. SCP it to the server's home directory
3. SSH in and confirm it's there with cat

Answer:

1
2
3
4
5
6
echo "hello from mac" > fromMac.txt
scp -P 2222 fromMac.txt root@localhost:~/

ssh root@localhost -p 2222
cat ~/fromMac.txt
exit

Exercise 5: Run a Command Remotely

1
2
3
4
Without logging in interactively, use SSH to run:
1. df -h
2. whoami
3. ls /var/log

Answer:

1
2
3
ssh root@localhost -p 2222 "df -h"
ssh root@localhost -p 2222 "whoami"
ssh root@localhost -p 2222 "ls /var/log"
This post is licensed under CC BY 4.0 by the author.