32

I deleted a critical symbolic link - libc.so.6. I have the file it should point at, but the basic commands such as ln or wget won't work anymore due to the link missing. However, echo or other Bash builtins work.

I am looking for a way to recreate this symbolic link.

5
  • 1
    @Sebas, I think you meant all the Bash builtins, not just echo. May 10, 2014 at 18:25
  • @CristianCiupitu maybe, what is it? cat is disabled... actually everything was.
    – Sebas
    May 10, 2014 at 18:27
  • 1
    @Sebas, that's because cat is an external program. The Bash Builtin Commands manual page has details about what could be available. May 10, 2014 at 18:30
  • 3
    I assume you mean GNU/Linux based systems when you say "Unix" as many other *nix systems have "rescue" versions of the standard utilities that are statically linked just for those "oops" moments.
    – Chris S
    May 10, 2014 at 19:38
  • Here's me thinking chrooting would be the only solution...
    – rubenvb
    May 14, 2014 at 8:16

6 Answers 6

57

you can use ldconfig, it recreates the symlink:

# rm /lib/libc.so.6 
rm: remove symbolic link `/lib/libc.so.6'? y
# ls -l /lib/libc*
ls: error while loading shared libraries: libc.so.6: cannot open shared object file:
# ldconfig 
# ls -l /lib/libc*
[skip]
lrwxrwxrwx. 1 root root      12 May 11 07:59 /lib/libc.so.6 -> libc-2.12.so

just tested it, as you see.

4
  • 4
    And, conveniently, the /sbin/ldconfig is statically linked. ldconfig is responsible for those symbolic links in the first place.
    – etherfish
    May 11, 2014 at 8:49
  • 16
    That's not really just "convenient", a statically-linked binary is pretty much a necessary design component of the tool that maintains your dynamic libraries! But it is what makes it an ideal tool for fixing this problem, and IMHO the only "correct" way. The question here is really not about a deleted symlink (99.999% of those can be deleted without consequence), it's "I broke my system's dynamic library store". Making @natxo's suggestion, "fix it using the tool that manages that store", obvious and sensible. Anything else (manually recreating the link) is a hacky workaround.
    – FeRD
    May 11, 2014 at 22:39
  • Yes, this is indeed more logical to proceed so even though the other answer was correct too.
    – Sebas
    May 12, 2014 at 15:54
  • (necromancy, sorry) Biggest reason this matters, BTW, is deleting symlinks isn't the only way you can screw up your dynamic library store. Say, for example, you accidentally renamed /lib/libc-2.12.so in the example above to /lib/foobar. Well, crap, no more mv. But ldconfig -l /lib/foobar is even smart enough to make /lib/libc.so.6 point to the wrong-named file. (The args are required, default ldconfig ignores filenames that don't start with "lib" and contain ".so".) At which point you can mv it back (or cp -p if you're paranoid/smart), then run ldconfig again to clean up.
    – FeRD
    Dec 23, 2016 at 14:58
44

CentOS 6 generally comes with busybox, a statically-linked set of Unix tools, installed in /sbin. You can run it like this:

/sbin/busybox ln -s libc-2.12.so /lib/libc.so.6
3
  • 1
    +1, just tested it after removing the symlink in a test vm, it woks in centos 6.5 May 11, 2014 at 5:52
  • +1 This answers the generic question in the question title as well.
    – MattBianco
    May 13, 2014 at 12:05
  • On Debian/Ubuntu it's /bin/busybox Aug 14, 2019 at 18:47
23

Set LD_PRELOAD to preload the relevant library. I tried it out with libpthread and it seems to work:

root@spirit:~# mv /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libpthread.so.0-bak
root@spirit:~# chattr
chattr: error while loading shared libraries: libpthread.so.0: cannot open shared object file: No such file or directory
root@spirit:~# LD_PRELOAD=/lib/x86_64-linux-gnu/libpthread.so.0-bak chattr
Usage: chattr [-RVf] [-+=AaCcDdeijsSu] [-v version] files...
1
  • interesting, it regroups with what the others said.
    – Sebas
    May 10, 2014 at 20:13
23

sln serves exactly that purpose: to fix symbolic links when you can't use regular ln because you broke an essential symlink. To quote its man page:

DESCRIPTION

  The  sln  program creates symbolic links.  Unlike the ln(1) program, it
  is statically linked.  This means that if for some reason  the  dynamic
  linker  is  not  working,  sln  can  be  used to make symbolic links to
  dynamic libraries.
1
  • nice, I did not know this tool. In centos it is part of glibc, so it must be installed by default May 12, 2014 at 11:31
8

You can set LD_LIBRARY_PATH variable to include the directory where real libc.so.6 is:

 export LD_LIBRARY_PATH="/dir/for/libc.so.6/:$LD_LIBRARY_PATH"

Also, execute ldconfig for it to recreate the links. This should make the commands work so you can then use ln commands to fix your system.

Another way would be to boot via LiveCD and link file there.

5
  • Then only way to boot off livecd and link file there in chroot.
    – ek9
    May 10, 2014 at 18:15
  • 1
    Also, you should just set LD_LIBRARY_PATH to include directory where libc.so.6 file is. This might let you use the commands as it should find the lib.
    – ek9
    May 10, 2014 at 18:20
  • darn, sorry I didn't think of this first.
    – ek9
    May 10, 2014 at 18:31
  • 2
    That doesn't work, becuase the file linked to isn't named libc.so.6. You'll need to set LD_PRELOAD as in my answer. May 10, 2014 at 19:39
  • 1
    I tried it on Fedora 20 and it didn't work. May 10, 2014 at 20:53
-4

Use scp or sftp to copy a statically linked version of ln. Make sure it is executable. Then use it to fix the file.

3
  • 1
    scp and sftp will not work as they will not be able to load that file either. May 14, 2014 at 0:18
  • scp, sftp, ftp would be run from a remote host. This assumes that the daemon in the broken machine is running already. Other possibilities for file transfer are file systems that are already mounted, either local or remote. May 14, 2014 at 18:33
  • 4
    Please stop misinformation. SCP requires the scp binary to be present and will be executed on both hosts. SFTP also launches new processes out of OpenSSH that require the respective binary. Most of the FTP daemons do the same. May 15, 2014 at 5:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .