The file is on another machine
The app runs on a server in a data centre, and the config file you need is on it. You cannot sit in front of that machine. You log in over the network, look, and copy the file to your own, all from one terminal.
By the end of this lesson
Note
I can become another user and use another machine from the terminal.
Key words
- User: an account on a machine, with its own files.
- sudo: run one command as the administrator.
- ssh: log in to another machine over the network.
- scp: copy a file to another machine.
- Service: a program that runs in the background.
Starter: what you already know
Note
Answer in your head. 1. What does whoami print? 2. Why should not everyone be able to delete every file? 3. What is a process?
Before you start
Note
Press Start the lab on the bar at the top of this lesson. This lab is a whole Ubuntu machine of your own, so it takes a little longer to come up than a container. Each Run button types its command into the machine's terminal, one after another in the same session. If one says the machine is still starting, wait a few seconds and press it again. Where the lesson says Type this, the command never ends by itself or asks you something, so press Open terminal on the bar and type it yourself.
Type this: become another user
In the terminal, type su dev, then the password DevLab2026. Nothing shows as you type a password. Type whoami: it says dev. Type exit to come back. For the administrator, root, type sudo su: no password is asked in this lab, and the prompt ends in #. sudo also runs one command as the administrator.
Another machine: ssh and scp
ssh runs commands on another machine and scp copies a file to it. Here the other machine is devbox, which is this same Ubuntu under the account dev, with a key already set up so no password is asked. The first time, ssh says it added the machine to its list. The commands are the ones you would use on a real server.
cdShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
echo "email Sam" > notes.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ssh dev@devbox whoamiShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
scp notes.txt dev@devbox:/home/devShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
ssh dev@devbox ls /home/devShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Type this: log in, look about, leave
In the terminal, type ssh dev@devbox. The prompt changes: you are now typing on the other machine, as dev. Run pwd and ls, and you see the notes.txt that you copied. Type exit to come back.
Check
Tip
Which command copies a file to another machine, and how does it name the place? Answer: scp, and the place is user@machine:folder.
Install a program, look after a service
sudo apt install -y tree installs a program from Ubuntu's archive. systemctl looks after services: status shows one and restart stops it and starts it again. --no-pager prints everything at once.
sudo apt install -y treeShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
tree projectsShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
systemctl status lab-web --no-pagerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
sudo systemctl restart lab-webShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Type this: edit a file in vi
Some servers have only vi. In the terminal, type vi notes.txt. You cannot type yet. Press a to start adding text, then type a line. Press Esc to stop adding. Then type :wq and Enter to save and quit. To quit and throw your changes away, type :q! instead. Check with cat notes.txt.
A common mistake
Warning
Many people think that being the administrator, root, is the normal way to work. It is not: root can delete anything with no warning, so a typing mistake does much more damage. Work as yourself, and use sudo for the one command that needs it.
Exit check
Tip
Answer without looking back. 1. Which command runs one command as the administrator? 2. How do you leave an ssh session? 3. In vi, how do you save and quit? Answers: sudo; exit; Esc, then :wq.
The cheat sheet
Move: pwd, ls -la, cd, cd -, ~. Files: mkdir -p, touch, echo >, cat, rm, rm -rf (take care), chmod +x. Read: tail -n, tail -f, grep, |. Find: find / -name 'x' 2>/dev/null, which, ps -ef | grep x, kill, then kill -9. Machine: df -h, du -sh *, free -h. Archives: unzip, gzip, gunzip, tar -czf, tar -xzf. Web: wget, curl -s. People: whoami, su, sudo, ssh user@host, scp file user@host:dir. Software: sudo apt install -y, systemctl status|restart. Editor: vi (a, Esc, :wq, :q!).
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. This lesson is worked in a VM lab: each student has an Ubuntu machine of their own, and a Run types into its terminal and shows what it printed. Key learning points: 1. su and sudo change who you are for a moment. 2. ssh and scp reach another machine. 3. apt and systemctl install and look after software. Common mistake: working as root all the time. Equipment: the lab and a browser. Tip: have the class say which of the thirty commands they would now write on a sticky note. The lab has no check and marks nothing; set a hand-in of your own if you want one. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.