Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Examendoelen in dit hoofdstuk
- Met SSH op systemen op afstand werken
- Inloggen en van gebruiker wisselen in multi-user targets
- Een systeem normaal opstarten, herstarten en afsluiten
- Veilig bestanden tussen systemen verplaatsen
Log in on the local console
The console is the screen and keyboard of the machine itself (or the console window of a virtual machine). You log in there with a user name and password.
- On a server without a graphical desktop you get a text login prompt:
server1 login:. - On a server with a desktop you log in on the graphical login screen; a terminal window then gives you a shell.
- The text in
/etc/issueis shown before the login prompt.
After login, check who you are and where:
Examentip
In the exam you work on virtual machines through a console in the browser. Keep it open: you need it when the network or SSH is broken.
Switch between terminals
Linux has several virtual terminals on the same screen. Ctrl+Alt+F1 … F6 switch between them.
| Keys | What you get (RHEL with desktop) |
|---|---|
Ctrl+Alt+F1 | The graphical login screen |
Ctrl+Alt+F2 | Your graphical session |
Ctrl+Alt+F3 … F6 | Text consoles (tty3–tty6) |
On a server without a desktop, Alt+F1 … Alt+F6 switch between text consoles. As root you can also use chvt N.
chvt 3Op het werk
When the desktop hangs, switch to a text console (Ctrl+Alt+F3), log in and fix it (for example kill the hanging program).
Understand pseudo terminals
Terminal windows and SSH sessions do not use a real console. They get a pseudo terminal: /dev/pts/0, /dev/pts/1 …
Reboot and shut down the right way
Always stop a system through systemd, so services can stop cleanly and file systems are written. Never just pull the power.
systemctl reboot
systemctl poweroff
shutdown -r +5 "Reboot for kernel update in 5 minutes"
shutdown -c
shutdown -h nowsystemctl reboot/systemctl poweroff= restart / switch off now.rebootandpoweroffare short forms.shutdowncan wait (+5= in 5 minutes,22:00= at a time) and sends a message to logged-in users.shutdown -ccancels it.systemctl haltstops the system but does not switch off the power.
Let op
A hard reset (power button, echo b > /proc/sysrq-trigger) can damage open files. Use it only when the system does not react at all.
Examentip
Reboot your exam systems at the end and check that everything still works. Work that does not survive a reboot does not count.
Log in to another server with SSH
SSH (Secure Shell) gives you an encrypted shell on another system. Everything you type and see is protected.
ssh user@hostlogs in as that user. Withoutuser@, your own user name is used.- The first time, SSH shows the fingerprint of the server and asks you to trust it. Answer
yesonly when you are sure it is the right server. It is then saved in~/.ssh/known_hosts. - A command after the host runs on the remote system and SSH ends at once.
-X(or-Y) lets you start graphical programs from the remote server on your screen.
Let op
"REMOTE HOST IDENTIFICATION HAS CHANGED" means the server key is different. It can be a reinstall — or an attack. Check before you remove the old key with ssh-keygen -R server2.
Copy files securely with scp
scp copies files over SSH. The remote side is written as user@host:path.
scp /etc/hosts student@server2:/tmp/
scp student@server2:/var/log/messages ./server2-messages
scp -r ~/project server2:/home/student/
scp -P 2222 backup.tar.gz server2:/srv/-rcopies folders.-P(capital) sets the port. (In ssh it is a small-p.)-p(small) keeps times and permissions.
Onthoud
scp = cp over SSH: scp SOURCE TARGET, remote side = user@host:path.
Transfer files with sftp
sftp opens an interactive file transfer session over SSH, like an FTP client but encrypted.
sftp student@server2| Command | What it does |
|---|---|
ls / lls | List remote / local files |
cd / lcd | Change remote / local folder |
pwd / lpwd | Show remote / local folder |
get file | Download a file |
put file | Upload a file |
mget *.log / mput *.txt | Download / upload many files |
mkdir / rm | Make a remote folder / remove a remote file |
exit | Leave |
The l in front means "local". Everything else works on the remote side.
Synchronize folders with rsync
rsync copies only what is different. The second run of a big copy takes seconds. It works locally and over SSH.
rsync -av ~/project/ server2:/srv/project/
rsync -av --delete /var/www/html/ /backup/html/
rsync -avn --delete /var/www/html/ /backup/html/
rsync -avz -e "ssh -p 2222" /data/ server2:/data/| Option | Meaning |
|---|---|
-a | Archive: recursive, keep permissions, owners, times, links |
-v | Show what is copied |
-z | Compress during the transfer |
--delete | Remove files in the target that are gone in the source |
-n | Dry run: show what would happen, change nothing |
-P | Show progress and keep half-done files |
Let op
A slash at the end of the source matters. src/ copies the content of src; src copies the folder itself into the target.
Op het werk
Always run rsync -avn --delete first. Read the list. Then run it without -n.
Log in with SSH keys
A key pair replaces the password: the private key stays on your computer, the public key goes to the server in ~/.ssh/authorized_keys.
- Make a key pair on the client (your computer or server1).
ssh-keygen -t ed25519 - Copy the public key to the server.
ssh-copy-id student@server2 - Log in: no password is asked any more (only the passphrase of the key, if you set one).
ssh student@server2
Controleer het
The permissions on the server must be tight, or SSH ignores the key.
ssh server2 'ls -ld ~/.ssh; ls -l ~/.ssh/authorized_keys'| File | Where | Permissions |
|---|---|---|
~/.ssh/id_ed25519 | Client | 600 — never share it |
~/.ssh/id_ed25519.pub | Client | 644 — this is the one you copy |
~/.ssh/authorized_keys | Server | 600 |
~/.ssh/ | Both | 700 |
Onthoud
private key stays home, public key travels.
Examentip
The exam objective "Configure key-based authentication for SSH" = ssh-keygen + ssh-copy-id + test the login. Chapter 20 adds the server settings.
Use a passphrase and ssh-agent
A passphrase encrypts the private key, so a stolen key file is useless. ssh-agent keeps the unlocked key in memory, so you type the passphrase only once per session.
ssh-keygen -t ed25519 -C "student@server1"
eval "$(ssh-agent)"
ssh-add
ssh-add -l- With a passphrase: safer, but you must unlock the key.
- Without a passphrase: needed for automatic jobs (cron, scripts), but protect the key file well and limit what it can do on the server.
Op het werk
For automation, use a separate key without passphrase per job, and limit it in authorized_keys (for example from="10.0.0.5",command="/usr/local/bin/backup.sh").
Switch to another user in a session
You do not need to log out to work as another user. su - USER opens a login shell as that user; exit takes you back.
Manage the server in a browser with Cockpit
Cockpit is a web console for RHEL: logs, services, storage, network, users and a terminal in the browser, on port 9090.
- Install it and start the socket.
- Open the firewall (often already open on RHEL).
firewall-cmd --add-service=cockpit --permanent firewall-cmd --reload - Open
https://server1:9090in a browser and log in with a normal user. Choose "administrative access" to make changes.
Op het werk
Cockpit is great to show a junior colleague what is going on. In production, allow port 9090 only from the admin network.
Remember the main points
The most important facts of this chapter, in short.
- Local logins use virtual consoles (Ctrl+Alt+F1…F6, or
chvt); remote shells use pseudo terminals (/dev/pts/N). - Reboot with
systemctl rebootand stop withsystemctl poweroff; never just pull the power. ssh user@hostopens a remote shell; the first connection asks you to trust the host key (saved in~/.ssh/known_hosts).scpandsftpcopy files over SSH;rsynccopies only what changed and can keep permissions (-a).- Key-based login:
ssh-keygenmakes a key pair,ssh-copy-idputs the public key in~/.ssh/authorized_keyson the server. - A passphrase protects the private key;
ssh-agentandssh-addkeep it unlocked for a session. su -switches to another user with a full login environment.- Cockpit is the web console (port 9090) for many admin tasks.
Learn the key terms
The words of this chapter in simple English.
| Term | Meaning |
|---|---|
| Console | The screen and keyboard of the computer itself. |
| Virtual terminal | One of several text logins on the local console (tty1–tty6). |
| Pseudo terminal | A terminal made by software, for example for SSH (/dev/pts/0). |
| SSH | Secure Shell: encrypted remote login and file copy. |
| Host key | The key that proves the identity of an SSH server. |
| Key pair | A private key (secret) and a public key (shared) for login without a password. |
| authorized_keys | The file on the server with the public keys that may log in. |
| Passphrase | The password that protects a private key. |
| Cockpit | A web interface to manage a RHEL server. |
Check what you learned
Think about the answer first, then open it.
1How do you switch to the third virtual console?
Ctrl+Alt+F3 (or chvt 3 as root).
3Where does SSH store the host keys of servers you trust?
In ~/.ssh/known_hosts.
4How do you set up login from server1 to server2 without a password?
On server1: ssh-keygen, then ssh-copy-id user@server2.
5Which file permission must the private key have?
Only the owner may read it, for example 600 (chmod 600 ~/.ssh/id_ed25519).