11

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.

  • it doesn't work just as you describe already? – mikeserv Nov 10 '15 at 10:34
  • 1
    sftp won't let you do that. rsync can, as can ssh cp ... – roaima Nov 10 '15 at 14:00
  • 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 – TheHidden Nov 10 '15 at 16:17
  • 1
    @roaima ssh cp? as in scp? how? – technical_difficulty Oct 6 '18 at 23:25
11

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.

  • 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 Nov 10 '15 at 13:25
  • @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. – polemon Nov 11 '15 at 10:16
5

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).

  • 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? – TheHidden Nov 10 '15 at 11:16
  • @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. – polemon Nov 10 '15 at 12:29
  • 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. – TheHidden Nov 10 '15 at 12:32
  • @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! – polemon Nov 10 '15 at 12:51
2

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.

1

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!

0

The other answers already explain how to transfer a symbolic link to another machine. However, regardless of how you transfer the symlink, you will not be able to use this symlink to access a file in /etc/ via FTP, SFTP, SCP, SSH or any other way. These services tend to be chrooted to a given directory or otherwise prevented from providing access to files that were not explicitly shared. Of course, there are always ways to hack around this on older systems, but such vulnerabilities tend to be patched up and upgraded as soon as they're found. I'm sorry, I don't think what you want to do is going to be possible.

  • fair enough, atleast I tried I guess :( – TheHidden Nov 10 '15 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.