Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

Hoofdstuk 8Naam in de cursus: Transferring Files

Copy files safely between systems

Files often must go from one server to another: backups, web sites, logs, installation files. Over SSH, the copy is encrypted and uses the same keys you already have. This chapter shows scp for quick copies, sftp for interactive work and rsync for big folders and repeated syncs.

  • 6 Lessen
  • 12 Commando's
  • 1 Labs

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

Vaardigheden in dit hoofdstuk

  • Bestanden veilig kopiëren met scp, sftp en rsync

8.1

Copy files with scp

scp SOURCE TARGET works like cp, but one side is user@host:path.

scp report.pdf student@server2:/tmp/
scp student@server2:/var/log/dnf.log .
scp -r ~/project server2:~/
scp -P 2222 backup.tar.gz admin@server2:/backup/
scp -p *.conf server2:/etc/myapp/
scp server2:/etc/hosts server3:/tmp/
OptionMeaning
-rCopy folders
-P PORTSSH port (capital P!)
-pKeep times and rights
-i KEYUse this key
-CCompress on the way

Notitie

In RHEL 10, scp uses the SFTP protocol inside. The commands stay the same. Paths with special characters may need different quoting than with old servers.

Onthoud

The : after the host name is what makes it remote. scp file server2 (no colon) makes a local file named server2.

Commando's in deze les

8.2

Work interactively with sftp

sftp HOST opens an FTP-like session over SSH: look around, then download (get) or upload (put).

sftp student@server2
Connected to server2.
sftp> pwd
Remote working directory: /home/student
sftp> ls -l
sftp> cd /var/www/html
sftp> put index.html
sftp> get -r logs
sftp> lcd /tmp
sftp> lls
sftp> mkdir upload
sftp> bye
sftp -b batch.txt deploy@server2
CommandWorks on
ls, cd, pwd, mkdir, rmThe remote side
lls, lcd, lpwdThe local side
put FILE / put -r DIRUpload
get FILE / get -r DIRDownload
bye or exitQuit

Op het werk

You can give a user only SFTP and no shell (ForceCommand internal-sftp in sshd). That is a safe way to let partners upload files.

8.3

Sync folders with rsync

rsync copies only what changed. The first run copies everything; the next runs are very fast. It works over SSH.

rsync -av ~/project/ server2:~/project/
rsync -av --delete /srv/www/ server2:/srv/www/
rsync -avn --delete /srv/www/ server2:/srv/www/
rsync -avz --progress bigfiles/ server2:/data/
rsync -av -e 'ssh -p 2222' /etc/ admin@server2:/backup/etc/
rsync -aAXv /srv/ /mnt/backup/srv/
rsync -av --exclude='*.tmp' --exclude='.cache/' ~/ server2:/backup/home/
OptionMeaning
-aArchive: folders, links, rights, times, owners (as root)
-vShow the files
-nDry run: show what would happen, change nothing
-zCompress on the way (slow networks)
--deleteDelete files on the target that are gone in the source
-A / -XAlso ACLs / extended attributes (SELinux labels)
-e 'ssh -p N'Use another SSH port or options
--progress / -PShow progress (-P also keeps half-done files)

Commando's in deze les

8.4

Mind the slash at the end

With rsync, a / at the end of the source means "the content of the folder". Without it, the folder itself is copied into the target.

rsync -a /srv/www/ server2:/data/
ssh server2 ls /data
index.html  css  img
rsync -a /srv/www server2:/data/
ssh server2 ls /data
www

Let op

--delete with a wrong slash or a wrong target can delete a lot. Always run the command with -n first and read the list.

8.5

Copy only through SSH keys

For scripts and cron jobs, use a key without passphrase that may do only the copy — never a password in a script.

ssh-keygen -t ed25519 -f ~/.ssh/backup_ed25519 -N ''
ssh-copy-id -i ~/.ssh/backup_ed25519.pub backup@server2
rsync -a -e 'ssh -i ~/.ssh/backup_ed25519' /srv/ backup@server2:/backup/srv/

Make the key safer on server2 with options in front of it in ~/.ssh/authorized_keys:

~backup/.ssh/authorized_keys (on server2)
from="192.168.122.10",no-pty,no-port-forwarding,no-agent-forwarding ssh-ed25519 AAAAC3Nza...k9 backup@server1

from= allows the key only from server1's address.

8.6

Choose the right tool

All three use SSH. Pick by the job.

JobTool
One or a few files, quickscp
Look around first, then pick filessftp
Big folders, backups, run again and againrsync
Through a jump hostAny of them with -J host (scp, sftp) or -e 'ssh -J host' (rsync)

More: the RHCSA lesson about rsync.

Oefenen

Alle commando's in dit hoofdstuk