scpcommandexamplestotransferfilesonLinux
Linux
BySilverMoon
OnMar15,2014
5Comments
Securecopy
Scp(SecureCopy)[Link]
[Link]
[Link]"protocol"thatdefines
howsecurecopyshouldworkandthe"program"(command)whichisinstalledasapartof
OpenSSHsuiteoftools.
Inthisquicktutorialweshalllookatafewexamplesthescpcommandandhowitcanbe
usedtotransferfilessecurely.
Installingscp
Scpisgenerallyinstalledbydefaultonmostlinuxdistrosasapartofopensshpackages.
Onubuntu/debianforexample,theopensshclientpackageprovidesthescpprogram.
$dpkgLopensshclient|grepscp
/usr/bin/scp
/usr/share/man/man1/[Link]
ItstheOpenSSHpackagethatprovidesthessh,scp,sftpprogramsalongwithmanyother
[Link],excepttouseandlearntheprogram.
Usingscp
[Link]
$scpsource_file_pathdestination_file_path
Dependingonthehost,thefilepathshouldincludethefullhostaddress,portnumber,
usernameandpasswordalongwiththedirectorypath.
Soifyouare"sending"filefromyourlocalmachinetoaremotemachine(uploading)the
syntaxwouldlooklikethis
$scp~/my_local_file.txtuser@remote_host.com:/some/remote/directory
Whencopyingfilefromremotehosttolocalhost(downloading),itslooksjustthereverse
$scpuser@remote_host.com:/some/remote/directory~/my_local_file.txt
#justdownloadthefile
$scpuser@[Link]:/some/path/[Link].
[Link],thereareacoupleof
[Link].
Andyes,[Link]
that,useamorepowerfultoolcalledrsync.
[Link]
Withverboseoutput,thescpprogramwouldoutputlotsofinformationaboutwhatitdoesin
[Link]
[Link]
intoissues.
$scpv~/[Link]@[Link]:/root/[Link]
Executing:program/usr/bin/sshhost192.168.1.3,userroot,commandscpvt/root/help2
OpenSSH_6.2p2Ubuntu6ubuntu0.1,OpenSSL1.0.1e11Feb2013
debug1:Readingconfigurationdata/home/enlightened/.ssh/config
debug1:Readingconfigurationdata/etc/ssh/ssh_config
debug1:/etc/ssh/ssh_configline19:Applyingoptionsfor*
debug1:Connectingto192.168.1.3[[Link]]port22.
debug1:Connectionestablished.
.....OUTPUTTRUNCATED
Theoutputwouldbebigandcontaindetailedinformationabouthowtheconnectionis
made,whatconfigurationandidentityfilesarebeingusedandsoon.
[Link]
Multiplefilescanbespecifiedseparatedbyaspacelikethis
$[Link]@remotehost:/path/directory/
Tocopymultiplefilesfromremotehosttocurrentlocaldirectory
$scpusername@remotehost:/path/directory/\{[Link],[Link]\}.
$scproot@[Link]:~/\{[Link],[Link]\}.
[Link](recursively)
Tocopyanentiredirectoryfromonehosttoanotherusetherswitchandspecifythe
directory
$scpvr~/Downloadsroot@[Link]:/root/Downloads
4.Copyfilesacross2remotehosts
Scpcancopyfilesfrom1remotehosttoanotherremotehostaswell.
$scpuser1@remotehost1:/some/remote/dir/foobar.txtuser2@remotehost2:/some/remote/dir/
[Link]
[Link]
[Link]
decompressedonthedestination.
$scpvrC~/Downloadsroot@[Link]:/root/Downloads
[Link]
gainwoulddependonhowmuchthefilescouldbecompressed.
[Link]
Ifyoudonotwantscptotakeuptheentireavailablebandwidth,thenusetheloptionto
limitthemaximumspeedinKbit/s.
$scpvrCl400~/Downloadsroot@[Link]:/root/Downloads
[Link]
Iftheremoteserverhassshdaemonrunningonadifferentport(defaultis22),thenyou
needtotellscptousethatparticularportnumberusingthe'P'option.
$scpvCP2200~/[Link]@[Link]:/some/path/[Link]
[Link]
The'p'option(smallcase),wouldpreservemodificationtimes,accesstimes,andmodes
fromtheoriginalfile.
$scpCp~/[Link]@[Link]:/some/path/[Link]
[Link]
Inquietmode('q'option),thescpoutputwouldgetsuppressed,andwoulddisablethe
progressmeteraswellaswarninganddiagnosticmessages.
$scpvCq~/[Link]@[Link]:/some/path/[Link]
[Link]
Whenusingkeybased(passwordless)authentication,youwouldneedtospecifythe
[Link]
commandandworksthesameway.
$scpvCqiprivate_key.pem~/[Link]@[Link]:/some/path/[Link]
11.Useadifferentssh_configfile
Usethe'F'optiontospecifyadifferentssh_configfile.
$scpvCF/home/user/my_ssh_config~/[Link]@[Link]:/some/path/[Link]
[Link]
ScpbydefaultusestheAEScipher/[Link]
[Link]
blowfishandarcfourareknowntobefasterthanAES(butlesssecure).
$scpcblowfishC~/local_file.txtusername@remotehost:/remote/path/[Link]
[Link]
significantspeedboostdependingonavailablebandwidth.
Summary
Althoughscpisveryefficientattransferringfilesecurely,itlacksnecessaryfeaturesofafile
[Link]
another.
AmorepowerfultoolisRsyncwhichnotonlyhasallfunctionsofscpbutaddsmore
[Link],itcancheckand
uploadonlythemodifiedfiles,ignoreexistingfilesandsoon.