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.

Undo
  • 291
  • 6
  • 17
Sebas
  • 535
  • 1
  • 7
  • 18
  • 1
    @Sebas, I think you meant all the **Bash builtins**, not just `echo`. – Cristian Ciupitu May 10 '14 at 18:25
  • @CristianCiupitu maybe, what is it? `cat` is disabled... actually everything was. – Sebas May 10 '14 at 18:27
  • 1
    @Sebas, that's because `cat` is an external program. The [Bash Builtin Commands](http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html) manual page has details about what could be available. – Cristian Ciupitu May 10 '14 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 '14 at 19:38
  • Here's me thinking chrooting would be the only solution... – rubenvb May 14 '14 at 08:16

6 Answers6

58

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.

fedorqui
  • 248
  • 4
  • 17
natxo asenjo
  • 5,641
  • 2
  • 25
  • 27
  • 4
    And, conveniently, the /sbin/ldconfig is statically linked. ldconfig is responsible for those symbolic links in the first place. – etherfish May 11 '14 at 08: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 '14 at 22:39
  • Yes, this is indeed more logical to proceed so even though the other answer was correct too. – Sebas May 12 '14 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 '16 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
Cristian Ciupitu
  • 6,226
  • 2
  • 41
  • 55
Mark Plotnick
  • 671
  • 5
  • 8
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...
Dennis Kaarsemaker
  • 18,793
  • 2
  • 43
  • 69
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.
Léo Lam
  • 231
  • 3
  • 9
aru
  • 231
  • 1
  • 2
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.

phoops
  • 2,073
  • 4
  • 18
  • 23
-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.

  • 1
    scp and sftp will not work as they will not be able to load that file either. – Florin Asăvoaie May 14 '14 at 00: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. – Robert Jacobs May 14 '14 at 18:33
  • 3
    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. – Florin Asăvoaie May 15 '14 at 05:53