Skip to content

Work on another machine: ssh and scp

IntermediateLesson 31 of 668 min

Log in to another machine with ssh, run commands there, come back with exit, then copy files both ways with scp. Practise on a pretend server.

Servers rarely have a screen and keyboard of their own. You reach them from your own computer with ssh (secure shell), which gives you a prompt on the other machine. As in the last lesson, web is a pretend server inside this page; nothing leaves your browser.

Logging in

ssh web

The first time you connect to a machine, ssh shows its fingerprint and asks whether to trust it. Here the practice shell answers yes for you; on a real system, check it first. Then you log in with a password or, better, a key (that's in the Advanced level).

Once you're in, the prompt changes to learner@web. Every command now runs on web, with web's files:

hostname
ls
exit

exit (or Ctrl+D) brings you back to your laptop. Watch the prompt: knowing which machine you're on is half the skill.

To run just one command there and come straight back, put it after the name: ssh web uptime.

Copying files: scp

scp copies over ssh, and works like cp. The side with a machine's name and a colon is on that machine:

CommandCopies
scp notes.txt web:your notes.txt to your home folder on web
scp notes.txt web:/tmp/to a folder on web
scp web:/etc/hostname .a file from web to here
scp -r photos web:a whole folder

Run scp on your laptop, not after logging in to web. Forget the colon, and scp copies to a local file called web.

Your turn

Log in to web, read README.txt in your home folder there, and come back with exit. Then copy report.txt from your laptop to your home folder on web, and copy web’s home page, /var/www/html/index.html, to your home folder on the laptop.

Next