Log in to Ubuntu and transfer files over SSH
🎯 Introduction
In this article, you will use SSH from a Mac terminal to log in to Ubuntu. After logging in, run hostname in the same window to see Ubuntu's hostname. Then upload a text file from the Mac and open it on Ubuntu. Both cloud servers and local virtual machines support this exercise.
🧱 Understand the connection's source and destination
SSH provides an encrypted remote connection. Running ssh in your Mac terminal initiates the connection; OpenSSH Server on Ubuntu accepts it. After a successful login, commands entered in that window run on Ubuntu.
Before connecting, you need Ubuntu's login username, server address, and SSH port. A port is a number that distinguishes network services on a server. SSH uses 22 by default.
| Practice environment | Address and port |
|---|---|
| Cloud server | Use the public IP address and SSH port shown by the cloud provider, with the account it provides. |
| Ubuntu virtual machine on a Mac | Use the virtual machine's IP address and port 22. |
| VirtualBox virtual machine on Windows | After configuring NAT port forwarding as described in the course overview, use 127.0.0.1 and port 2222. |
If you forgot the username, run whoami in an Ubuntu console where you are already logged in. To find a Mac virtual machine's IP address, run the following in its Ubuntu window. -br gives brief output, and -4 shows IPv4 addresses only.
ip -br -4 address
Find an entry such as 192.168.64.5/24 on a network interface. Use only 192.168.64.5, before the slash, when connecting. The 127.0.0.1 address on lo refers only to the virtual machine itself when used inside it.
Cloud servers usually have SSH ready. If you did not select OpenSSH Server when installing Ubuntu in a local virtual machine, run sudo apt update in the Ubuntu console, then install it. Follow your system's installation prompts. Install OpenSSH on Ubuntu
sudo apt install openssh-server
🔑 Prepare a login key
The server must verify that you may use the account. Password authentication checks the account password. Key authentication uses a pair of files instead: the public key can go to the server, while the private key stays on the computer initiating the connection.
If your cloud server already has a key associated with it and you hold the matching private key, use that key. To create a new one, first create a key directory in the Mac terminal.
mkdir -p "$HOME/.ssh"
ssh-keygen generates keys. -t ed25519 selects the key type, and -f sets the private key's location. The command also creates a public key at the same location with a .pub suffix.
ssh-keygen -t ed25519 -f "$HOME/.ssh/linux-course"
You can set a passphrase for the private key during generation. If a connection later asks for it, your Mac uses it to unlock the private key. Display the public key with cat and copy the entire output line.
cat "$HOME/.ssh/linux-course.pub"
When creating a cloud server, enter that public key in the provider's SSH key settings. For a local virtual machine that allows password login, log in with the password first and add the public key to the Ubuntu account.
🛠 Log in and confirm you reached Ubuntu
Verify the server's identity first
On the first connection, SSH displays the server's fingerprint: a string used to verify its identity. First log in to your Ubuntu machine through the cloud console or virtual machine window and inspect its ED25519 host fingerprint. -l displays the fingerprint, and -f specifies the public key file. OpenSSH host key verification
sudo ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub
Record the part beginning with SHA256:. Compare it with the same key type in the SSH prompt and enter yes only after they match. If SSH displays a different key type, inspect the corresponding host public key through the console before comparing.
Connect from the Mac
The following example uses username learner and address 192.168.64.5. Replace them with your actual values. The Ubuntu account goes before @, and the server address goes after it. -i specifies the private key on your Mac.
ssh -i "$HOME/.ssh/linux-course" learner@192.168.64.5
For port 2222, add -p 2222 after ssh. If your local virtual machine allows password login, you can first connect with ssh learner@192.168.64.5 and enter the Ubuntu account password when prompted.
After login, run hostname and check that the result is your Ubuntu hostname. This window now runs commands just like the Ubuntu console. Enter exit to end the session and return to your local terminal.
🛠 Switch from password login to key authentication
If you already logged in successfully with a key, continue to the next section. To add a public key to an existing Ubuntu account, keep your current SSH session open. Copy the public key using the earlier cat command in another Mac terminal, then return to the logged-in Ubuntu window.
First prepare the .ssh directory on Ubuntu.
mkdir -p "$HOME/.ssh"
Allow only the current account to read, write, and enter this directory. As discussed earlier, 700 grants all three permissions to the owner and none to the other two categories.
chmod 700 "$HOME/.ssh"
Ubuntu uses authorized_keys to record which public keys may log in to the current account. Open it with Vim.
vim "$HOME/.ssh/authorized_keys"
For an empty file, press i. If it already has content, press uppercase G in normal mode to reach the last line, then lowercase o to open a new line. Paste the public key as one complete line, preserving existing content. Press Esc, type :wq, and press ↩ Return to save and quit.
Set this file's mode to 600 so only the current account can read and write it.
chmod 600 "$HOME/.ssh/authorized_keys"
Keep the existing session open and reconnect from a new Mac terminal. Add -o PreferredAuthentications=publickey to the previous section's ssh command to try only public key authentication for this connection. Close the old session only after verification succeeds.
🧰 Create a connection alias
Typing the address, account, and private key path every time invites mistakes. Save them in the Mac's SSH configuration file and name this practice machine linux-lab.
vim "$HOME/.ssh/config"
Add this block while preserving any existing server configuration. Host is your chosen alias. Set HostName, User, and Port to the actual address, username, and port. IdentityFile points to the local private key, and IdentitiesOnly yes restricts attempted identities to those specified in the configuration.
Host linux-lab
HostName 192.168.64.5
User learner
Port 22
IdentityFile ~/.ssh/linux-course
IdentitiesOnly yes
After saving, log in with the alias. Keep this Ubuntu window open to check the upload next.
ssh linux-lab
🛠 Upload and read a file
Prepare a destination directory on Ubuntu
Run this command in the logged-in Ubuntu window. incoming receives files sent from your computer.
mkdir -p "$HOME/linux-lab/incoming"
Start the copy from the Mac
In another Mac terminal, create ~/linux-course-upload.txt with Vim, enter uploaded from Mac, and save. scp copies files over SSH. The command below puts the Mac source first and the Ubuntu destination directory second. linux-lab: indicates a path on that server, relative to the remote account's home directory.
scp "$HOME/linux-course-upload.txt" linux-lab:linux-lab/incoming/
After copying, return to the Ubuntu window and read the file. Seeing uploaded from Mac confirms that it reached the server.
cat "$HOME/linux-lab/incoming/linux-course-upload.txt"
For repeated uploads or downloads, run sftp linux-lab on the Mac to start a file transfer session. At the sftp> prompt, ls lists remote files, put uploads, get downloads, and exit ends the session. The exercise uses get to download this same file.
🧰 What to check when a connection fails
| Symptom | First check |
|---|---|
Connection timed out | For a cloud server, check whether its security group allows your connection. For a virtual machine, check the network mode and forwarded address. |
Connection refused | Check the port and confirm that SSH is installed and started on Ubuntu. |
Permission denied (publickey) | Check the username, private key path, and public key in the remote account. |
| Host fingerprint change warning | First use the console to check whether the system was reinstalled or you reached another machine. |
To inspect Ubuntu's SSH service, run systemctl status ssh.socket ssh.service --no-pager in the console. It displays the SSH listening unit and service states. On Ubuntu 24.04, ssh.socket commonly listens and starts ssh.service when a connection arrives.
Edit remote files in VS Code
Once ssh linux-lab works in the terminal, try VS Code. Install Microsoft's Remote - SSH extension, open the Command Palette with ⇧ Shift + ⌘ Command + P on a Mac, run Remote-SSH: Connect to Host..., and choose linux-lab. Open the remote linux-lab directory after connecting. VS Code Remote - SSH documentation
❓ Knowledge check
Q1: Why verify the host fingerprint and configure a user public key?
The host fingerprint identifies the server you are connecting to. The user public key helps the server verify who may log in to an account.
Q2: Why can cat in the SSH window not find a file on my Mac?
After login, cat runs on Ubuntu. Return to the local terminal to read a Mac file. Upload it first if Ubuntu also needs to read it.
🧪 Exercise
In the Mac terminal, run cd "$HOME" to enter your home directory, then sftp linux-lab. At sftp>, enter get linux-lab/incoming/linux-course-upload.txt linux-course-download.txt. The first path is the Ubuntu file; the second is its downloaded name on the Mac.
Enter exit to return to the Mac terminal and inspect cat linux-course-download.txt. If it contains uploaded from Mac, you have verified the full upload and download round trip.
🎉 You can now log in to Ubuntu over SSH and transfer files between machines!
