هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- نسخ الملفات بأمان باستخدام scp و sftp و rsync
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/| Option | Meaning |
|---|---|
-r | Copy folders |
-P PORT | SSH port (capital P!) |
-p | Keep times and rights |
-i KEY | Use this key |
-C | Compress on the way |
ملاحظة
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.
تذكّر
The : after the host name is what makes it remote. scp file server2 (no colon) makes a local file named server2.
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| Command | Works on |
|---|---|
ls, cd, pwd, mkdir, rm | The remote side |
lls, lcd, lpwd | The local side |
put FILE / put -r DIR | Upload |
get FILE / get -r DIR | Download |
bye or exit | Quit |
في العمل
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.
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/| Option | Meaning |
|---|---|
-a | Archive: folders, links, rights, times, owners (as root) |
-v | Show the files |
-n | Dry run: show what would happen, change nothing |
-z | Compress on the way (slow networks) |
--delete | Delete files on the target that are gone in the source |
-A / -X | Also ACLs / extended attributes (SELinux labels) |
-e 'ssh -p N' | Use another SSH port or options |
--progress / -P | Show progress (-P also keeps half-done files) |
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.
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:
from="192.168.122.10",no-pty,no-port-forwarding,no-agent-forwarding ssh-ed25519 AAAAC3Nza...k9 backup@server1from= allows the key only from server1's address.
Choose the right tool
All three use SSH. Pick by the job.
| Job | Tool |
|---|---|
| One or a few files, quick | scp |
| Look around first, then pick files | sftp |
| Big folders, backups, run again and again | rsync |
| Through a jump host | Any of them with -J host (scp, sftp) or -e 'ssh -J host' (rsync) |
More: the RHCSA lesson about rsync.