For newcomers, SSH itself might look hard at first and file transferring even more complicated. How to connect to a remote machine to send or copy files from it?
I'm going to show you that it is not as complicated as it looks. As a matter of fact, it's quite easy!
First let's define the appearance of our remote connection for our examples:
- User:
dev
- Address:
123.45.67.89
Replace it with your real remote machine connection while following the examples
The interactive way
Let's use the SSH File Transfer Protocol, or SFTP.
With this tool, we connect to our remote machine and use it with a bash-like shell. Let's see how it works:
sftp dev@123.45.67.89
With SFTP we rely on file browsing to either receive or send files, after opening our connection let's explore the remote and local working directories.
Inside the remote we can navigate freely using cd
and ls
commands. See all available commands with help
.
sftp> pwd
Remote working directory: /home/dev
sftp> lpwd
Local working directory: /c/Users/User
Sending files from ๐ป local machine to the ๐ remote machine:
sftp> put # Press Tab
myfile1.txt
myfile2.txt
sftp> put myfile2.txt # ๐ Send myfile2.txt to the remote working directory
Receiving files or folders from ๐ remote machine to ๐ป local machine:
sftp> ls
file1.txt
file2.txt
folder
sftp> get file1.txt # ๐ Gets file1.txt to the local working directory
sftp> get -r folder
# ๐ To get an entire directory
The non-interactive way
We will be using the Secure Copy Protocol, or SCP.
It's a nice and quick way and all it takes is a ssh-like command with the file paths, a login prompt and there you go!
Sending files from ๐ป local machine to the ๐ remote machine:
scp ./file.txt dev@123.45.67.89:/home/dev/file.txt
# ๐ Your local file path ๐ Where to place it on the remote
Receiving files or folders from ๐ remote machine to ๐ป local machine:
# ๐ Where to place it on the local
scp dev@123.45.67.89:/home/dev/file.txt ./file.txt
# ๐ The remote file path
scp -r dev@123.45.67.89:/home/dev/folder ./folder
# ๐ To copy an entire directory
Although this is an easy way for a quick file transfer there are some caveats.
According toย OpenSSHย developers in April 2019, SCP is outdated, inflexible and not readily fixed; they recommend the use of more modern protocols likeย SFTPย andย rsyncย for file transfer.[3]ย As of OpenSSH version 9.0,ย
scp
ย client therefore uses SFTP for file transfers by default instead of the legacy SCP/RCP protocol.[4โ Wikipedia
Conclusion
As you can see, file transferring through SSH is not that hard. There are a few methods to accomplish it and after you get used to it, it's just like doing a regular connection.
Now you can get that file or send it to your remote box, have fun!
Attributions and Sources
- Photo by Brett Sayles from Pexels: https://www.pexels.com/photo/ethernet-cables-plugged-in-network-switch-2881224/
- https://en.wikipedia.org/wiki/Secure_copy_protocol
- https://en.wikipedia.org/wiki/SSH_File_Transfer_Protocol
- https://superuser.com/questions/134901/whats-the-difference-between-scp-and-sftp
- https://www.digitalocean.com/community/tutorials/how-to-use-sftp-to-securely-transfer-files-with-a-remote-server
Top comments (15)
Yeah, I'll use
scp
for a quick glob or directory, or ifrsync
isn't installed, butrsync
is much better in most cases.Hello Ben, I havent studied rsync in depth but it looks like a great option to have the files synced between remote and local host. Thanks for the comment :)
Neither of them have to be remote or local, by the way. You can scp or rsync on your local drive from one directory to another just as easily. With rsync though, it gives you the super powers to do things like only copy files that have changed, or to delete files that aren't in the source directory!
That's awesome, I'm definitely going to check out rsync, those features are super useful! thank you :)
Also, rsync is the base for the almighty rsnapshot, a great tool to make data backups across servers or between your hard drive and an external drive.
Hi Mateus,
I really like how your article was presented. If you're looking for more article ideas, I think something on basic
~/.ssh/config
usage would be helpful (and maybessh-agent
). It would really tie well with the content you have here. Keep up the good work!Hey Chris, thank you, that is awesome! I'll take a look at your suggestions with great appreciation :)
Great write up Mateus!
I really like the use of emoji pointers to annotate the code and call out specific pieces of the commands.
Thank you, I'm still looking at yours to get inspiration but I think that with time I'm going to get better at it. It's awesome that you read it, thanks again ๐
I appreciate that Mateus! I'm thrilled that you're working at it.
Keep up the great work man!
yeah.. same in my case i use rsync more often to grab files from staging to local server.. and sftp when sending files to third party's server
Well-documented!
Thank you Kudzai :)
what is this
Hello there, those are ways of transfering files through a ssh connection