If you access the server via SSH, you can also copy files through it over an encrypted connection, without the need for a separate FTP account. You can choose from three tools:
- scp: simple, for one-off copying;
- rsync: for large amounts of data, migration and repeated synchronisation, as it only transfers changes and can resume after an interruption;
- SFTP: for interactive file management and use with graphical programs (FileZilla, WinSCP, Cyberduck).
All three tools use the same username, password or SSH key as for SSH login (see: How do I connect to my VPS via SSH?). On Linux, macOS and Windows 10/11 (PowerShell), the scp and sftp commands are available by default.
scp
# Fájl feltöltése a szerverre
scp mentes.sql janos@203.0.113.10:/home/janos/
# Fájl letöltése a szerverről az aktuális könyvtárba
scp janos@203.0.113.10:/var/log/nginx/error.log .
# Teljes mappa feltöltése
scp -r weboldal/ janos@203.0.113.10:/var/www/
# Nem alapértelmezett SSH port (nagy P betűvel!)
scp -P 2255 mentes.sql janos@203.0.113.10:/home/janos/
rsync
rsync must be installed on both machines (sudo dnf install rsync or sudo apt install rsync).
rsync -avz --progress weboldal/ janos@203.0.113.10:/var/www/weboldal/
-a(archive): copies including subdirectories, and preserves permissions, the owner and the modification time,-v: lists the copied files,-z: compresses data during transmission (useful on slow connections, can be omitted on fast networks),--progress: shows the progress.
If you run the command again, it will only copy the files that have changed, so an interrupted copy operation can easily be resumed.
Please note the slash
A slash (/) at the end of the source changes the result:
rsync -a weboldal/ cel/a copies the contents of theweboldalfolder to thecelfolder;rsync -a weboldal cel/copies theweboldalfolder itself, so the result will becel/weboldal/.
In the test (AlmaLinux 10.2), following the first run, the target files were a.php, cache and e.log; in the second run, it was a single weboldal folder.
Further useful links
# Próbafuttatás: kiírja, mit csinálna, de nem másol semmit
rsync -avn weboldal/ janos@203.0.113.10:/var/www/weboldal/
# Nem alapértelmezett SSH port
rsync -avz -e "ssh -p 2255" weboldal/ janos@203.0.113.10:/var/www/weboldal/
# Mappák kihagyása
rsync -avz --exclude 'cache/' --exclude '*.log' weboldal/ janos@203.0.113.10:/var/www/weboldal/
# A célon törli azokat a fájlokat, amelyek a forrásban már nincsenek meg
rsync -avz --delete weboldal/ janos@203.0.113.10:/var/www/weboldal/
Always try the --delete option first with the -n (dry run) option: if you mistype the source or destination, the files at the destination may be deleted. During a dry run, rsync displays what it would delete (deleting regi.txt in the test), the output ends with (DRY RUN), and the file remains in place.
Migration between two servers
When migrating a server, the quickest way is to run rsync directly on the new server and transfer the data from the old one, so that it does not go via your own internet connection:
# Az új szerveren futtatva
rsync -avz --progress janos@regi.szerver.ip:/var/www/ /var/www/
For longer copy operations, run the command in tmux or screen so that a connection drop does not interrupt it (see: Running commands over an intermittent SSH connection).
sftp
sftp establishes an interactive connection, allowing you to browse the server’s files using commands:
sftp janos@203.0.113.10
| Command | Report |
|---|---|
ls, cd, pwd |
Listing and changing directories on the server |
lls, lcd, lpwd |
the same on your own computer |
put fajl |
Upload (use put -r mappa to upload an entire folder) |
get fajl |
download (use get -r mappa to download the entire folder) |
rm, mkdir, rename |
Delete, create folder, rename |
exit |
log out |
For a graphical interface, use a program that supports SFTP (such as FileZilla, WinSCP or Cyberduck): select SFTP as the protocol, and enter the server address, SSH port 22 (or your own port), and your username and password or key.
Transferring a large folder in one step
Transferring lots of small files (such as a WordPress site) is quicker if you compress them into a single archive as you go:
tar -czf - weboldal/ | ssh janos@203.0.113.10 "tar -xzf - -C /var/www/"
For more information on using tar: creating and extracting tar.gz archives.