How to copy symlinks as symlinks from one machine to another?

65,296

Solution 1

If you tar up the symlink, then you can copy the tarball over to the remote machine any way you like. When you untar it, you'll find that you've only copied the symbolic link and nothing else. For example:

To test this, first I created a text file.

echo this is a test > file.txt

Then create a symlink to the file and compress it into a tar gzip.

ln -s file.txt link.lnk
tar czvf tarball.tar.gz link.lnk

Transfer this file to your other machine and then uncompress it:

tar xzvf tarball.tar.gz

You will find that the symlink is there, but not the original file that it had pointed to.

Of course, if you wanted to transfer both the original file and the symlink while preserving their relationship, you can put them both in the tarball and they would both be transferred, and the symlink should still be correctly pointed to the original file when you uncompress the tarball in the destination location.

Solution 2

It is a little unclear how exactly you want to deal with the symlink. As I understand it, you want to recreate the symlink on the other system. Symlinks are filesystem dependent, the protocol used to copy files must be aware of that. A good example is using rsync with the -a option.

more specifically, the -l option, but -a is probably what you want.

It will recreate symlinks without pulling the targets over (dereferencing the links).

Solution 3

I know @polemon has already answered this.

But doing my tad bit for quick solution.

I had to do man rsync to know which switch I wanna use...

rsync -avz -e ssh /file or sym link/to/copy/ user@ip:/dest/path/

Although, in your case you need to make sure that links are not broken!

Solution 4

A very easy solution (for the home directory):

tar -c SYMLINKFILE | ssh machine 'tar -x'

or

tar -c SYMLINKFILE | ssh machine 'tar -xC /some/other/location  '

e.g.

tar -c .bashrc | ssh another_machine 'tar -x'

to provide a symlinked .bashrc placing to another (e.g. network) destination.

Solution 5

You can mount the remote machine on the local one using sshfs:

sshfs remote.machine:/home/name/remotedir/ /home/name/localdir/

Now you can treat the new mount point as if part of the local machine file system and thus copy symlinks normally.

Share:
65,296

Related videos on Youtube

TheHidden
Author by

TheHidden

Updated on September 18, 2022

Comments

  • TheHidden
    TheHidden almost 2 years

    Let's assume I have two identical systems. On the first system I've created a symbolic link. On the second one I want to copy that symlink via sftp and the symlink shall work the same (i.e if the symlink links to /etc/, after copying it over, only the symlink will be copied and only the symlink (not the linked files) )

    Any ideas on how I can do this / if I can do this? I only want to copy the symbolic link, nothing else; just the reference.

    • mikeserv
      mikeserv over 8 years
      it doesn't work just as you describe already?
    • roaima
      roaima over 8 years
      sftp won't let you do that. rsync can, as can ssh cp ...
    • TheHidden
      TheHidden over 8 years
      The thing I am trying to achieve is copying over the symbolic map... I dont want the files.. I just want the bit of code that says FOLDER > ETC ... I just want to create a symlink off the server and move it to the server its needed on... im trying to hack out of an enclosed file
    • technical_difficulty
      technical_difficulty over 5 years
      @roaima ssh cp? as in scp? how?
  • TheHidden
    TheHidden over 8 years
    I see hm, so there would be no way to copy it like a file and retain the link? I only have the ability to directly move files over I was hoping to build the symlink on the identical system and copy it over or am I just being to hopeful?
  • polemon
    polemon over 8 years
    @user1779617 first of all: when you say "move", do you actually mean "move" or "copy"? Because "move" implies deleting the original file (this was also the reason why I've edited your question). Second thing: are you limited to a specific file transfer protocol (like FTP only)? If so, we should discuss it within the scope of that, and the filesystems we're talking about.
  • TheHidden
    TheHidden over 8 years
    SFTP only, download only from the machine that needs the symlink, so I mean copy from the master to the slave SFTP only and the sftp connection is from the slave only.
  • polemon
    polemon over 8 years
    @user1779617 I'm assuming you mean "client" (slave) and "server" (master). sftp will traverse subdirectories with the -r switch, but not follow symlinks, as it abstracts filesystems. sftp supports creating symlinks on the host. You should at least try downloading just the symlink (when the symlink is ~/etc -> /etc/ do get etc without the trailing /) and see what happens. It depends on the FS of the server and the client. Please edit your original question accordingly!
  • TheHidden
    TheHidden over 8 years
    this is rather interesting but sadly I dont think it will help me, I dont have any control over the slave all these SFTPs are done via APIs to locked down folders, but I am trying to hack the box using symlinks because there is a bit of an emergency and I need to get access from the master which pushes selected updates, for safety reasons the files i send dont execute, but if I can symlink I can inject code into an already existing process (theoretically) by replacing the file via the symlink ... but to do it I need to copy a symlink over 100% raw :(
  • TheHidden
    TheHidden over 8 years
    fair enough, atleast I tried I guess :(
  • polemon
    polemon over 8 years
    @user1779617 It is still kinda unclear what you mean with "slave" and "master", because you seem to use both terms for both server and client interchangeably. If you need to create a symlink on the server, you can do that with SFTP, however depending on the configuration, you might not be able to look outside of the chrooted realm of the SFTP-daemon. Please refer to the man page of sftp (linux.die.net/man/1/sftp)! There is the command symlink specifically for what you seem to need.
  • FKEinternet
    FKEinternet almost 3 years
    I'd like to vote this answer up about a hundred times: In all my years of working with 'nix systems I'd never come across sshfs before but it solves boatloads of problems I've run into and would have simplified so many tasks that caused me pain and grief...
  • FKEinternet
    FKEinternet almost 3 years
    If you use the method sinekonata described, the symlinks will work 100% as expected.
  • FKEinternet
    FKEinternet almost 3 years
    This isn't what the OP was trying to do - he wanted the symlinks to refer to files on the machine he was attacking. It's also not as easy as the sshfs method sinekonata described.