linux - Copy file from local host to remote host

03
2014-05
  • thumbtackthief

    This answer must exist somewhere as it's so basic, but I can't find it.

    I have a file on my local computer that I want to copy to a remote host. I know how to use scp, but if I do it from my local host to the remote host, I get a permission error. I can log in to the remote host with root privileges, but I don't know how to identify my local machine (I tried both the internal and external IP addresses and timed out both times). Either method would work for me (although ideally I'd like to know how to do both!)

    This is part of an exercise to learn Linux better, so if I'm not presenting the necessary information please let me know what's specifically needed and I'll do my best to elaborate.

    (Bonus question: Would love any recommendations for good Linux resources!)

  • Answers
  • Darth Android

    This is exactly what rsync was created for! As long as you have SSH access to the remote machine, and the remote machine has rsync installed, then you can:

    rsync -avz /path/to/local/file username@host:/destination/path
    

    This will connect over SSH to host as username, launch a private rsync daemon, and then copy the local file to the remote location over the encrypted SSH tunnel. You can also specify a remote location first and a local location second to copy remote files to your local system. If you specify a folder to copy, it will copy the folder and all of it's contents.

    Breakdown of the flags:

    • -a "Archive" - copy recursively, and preserve attributes / permissions where possible.
    • -v "Verbose" - Print out each filename after it's copied
    • -z "Compress" - gzip the data as it goes over the network. Faster transfers and lower bandwidth at the cost of CPU processing.

    If you're transferring large files, the --progress option might also be useful.

  • Piskvor
    ssh username@remotehost tee /home/user/example.file < ./example.local.file
    

    What this does:

    • reads in the contents of local file ./example.local.file
    • SSHs into remotehost as user username
    • launches tee at the remote host
    • and instructs it to put what it gets as input into /home/user/example.file

    Advantages:

    • simple

    Disadvantages:

    • overwrites the whole file, regardless of remote file's state
    • which also means the whole file will always be transferred across the wire
    • tee will echo back everything it receives; this may be undesirable with binary files

  • Related Question

    linux - How do I connect to an SSH server from Ubuntu?
  • codeLes

    I've been given this information (changed to protect the innocent):

    Hostname: secret_server.homeserver.org
    Port: 4033
    Username: user
    Password: $ecrectP#ras3
    Protocol: scp
    

    I don't have to have a nice GUI. What's the key to connecting to this server? I understand that SCP will copy files from a source directory to my target directory (both ways). How can I connect via SSH to this server so I can traverse the directories and find the files I need?

    the following terminal command just times out:

    ssh -p 4033 user@secret_server.homeserver.org
    

    is it me or the server?


  • Related Answers
  • Juha Syrjälä

    If the connection just times out, there may be some network problem. For example, some firewall blocks the connection.

    Can you ping the server?

    ping secret_server.homeserver.org
    

    In addition, ssh connection may be blocked, and only file copying with scp is allowed.

    Does it work if you use scp to copy files?

  • pgs

    Protocol: scp

    It's possible that they've enabled only scp, and that ssh remains disabled.

  • heavyd

    If you can't get in using ssh, sftp still might be an option. It's not a full shell but at least you can traverse directories, list files and transfer them.

    sftp -oPort=4033 user@secret_server.homeserver.org:/path_to_destination/
    
  • Richard Hoskins
    scp -p 4033 file_to_copy user@secret_server.homeserver.org:/path_to_destination/