Skip to content
Laviyx Learn

Try

Linux and Bash course menu

SSH Tutorial: Log In to Linux Servers and Copy Files

Connect to servers with SSH, set up key-based login, use the SSH config file, copy files with scp and rsync, and harden a server by disabling password login.

Intermediate5 min readUpdated
On this page

SSH (Secure Shell) lets you run commands on another computer over an encrypted connection. It is how nearly every Linux server is managed. In this lesson, the "server" is this same machine, localhost, which is why the commands work without a second computer.

Connecting#

The form is ssh user@host:

Terminal
ssh asha@203.0.113.10

The first time, SSH asks you to confirm the server's fingerprint, and stores it in ~/.ssh/known_hosts. Type exit to log out.

Key-based login#

Passwords can be guessed. Keys are safer and need no typing. Create a key pair with ssh-keygen. In real use, add a passphrase when asked; we use an empty one (-N "") only so that this example can run unattended:

Terminal
ssh-keygen -q -t ed25519 -f ~/.ssh/id_ed25519 -N ""
ls -l ~/.ssh | awk 'NR > 1 {print $1, $NF}'
Output
-rw------- id_ed25519
-rw-r--r-- id_ed25519.pub

id_ed25519 is the private key, which you never share. id_ed25519.pub is the public key, which goes on the server.

Put the public key in the server's ~/.ssh/authorized_keys. Normally ssh-copy-id asha@server does this for you, and it is the same as:

Terminal
cat ~/.ssh/id_ed25519.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
ssh -o StrictHostKeyChecking=accept-new localhost 'echo "Logged in to $(hostname) as $(whoami)"' 2> /dev/null
Output
Logged in to laviyx as asha

No password was needed. As you can see, a command after the host runs remotely, and prints its output here.

The SSH config file#

Save long commands in ~/.ssh/config:

Terminal
cat > ~/.ssh/config <<'EOF'
Host web
    HostName localhost
    User asha
    IdentityFile ~/.ssh/id_ed25519
EOF
chmod 600 ~/.ssh/config
ssh web hostname
Output
laviyx

Now ssh web is enough. Add Port 2222 if a server uses a different port.

Copy files with scp#

scp copies over SSH. A colon marks the remote side:

Terminal
echo "report" > report.txt
mkdir -p ~/backups
scp -q report.txt web:backups/
ssh web 'ls backups'
Output
report.txt

Download by swapping the arguments, as in scp web:backups/report.txt ., and add -r for folders.

Sync with rsync#

rsync copies only what has changed, which makes it ideal for backups and deployments:

Terminal
mkdir -p site
echo "home" > site/index.html
echo "about" > site/about.html
rsync -az --stats site/ web:public/ | grep 'files transferred'
echo "home v2" > site/index.html
rsync -az --stats site/ web:public/ | grep 'files transferred'
Output
Number of regular files transferred: 2
Number of regular files transferred: 1

The second run sent one file. -a keeps permissions and times, and -z compresses.

Securing a server#

On any server exposed to the internet, disable password logins once your key works. Edit /etc/ssh/sshd_config:

Text
PasswordAuthentication no
PermitRootLogin no

Then reload the service. Keep your current session open, and test a new login in a second terminal, so that you cannot lock yourself out.

Terminal
sudo systemctl reload ssh

Automated bots try to log in to all public servers. Tools such as fail2ban block them after repeated failures.

Common mistakes#

Practice#

Practice exercise

Using the web shortcut, count the files in the remote public folder, then print the remote file public/index.html.

Show solution
Terminal
ssh web 'ls public | wc -l'
ssh web cat public/index.html
Output
2
home v2

Key takeaway#

Next: Scheduling Jobs with cron.

Finished this lesson?