Monday, October 16, 2017

How to check which libraries are used by a program or process on Linux ?

Check shared library dependencies of a program executable

To find out what libraries a particular executable depends on, you can use ldd command. This command invokes dynamic linker to find out library dependencies of an executable.
Syntax:

ldd /path/to/program

Example:

[ram@linuxforfreshers.com]$ldd /usr/bin/ssh
            linux-vdso.so.1 =>  (0x00007ffe099dc000)
            libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f578f64a000)
            libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f578f26e000)
            libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f578f069000)
            libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f578ee50000)
            libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f578ec35000)
            libgssapi_krb5.so.2 => /usr/lib/x86_64-linux-gnu/libgssapi_krb5.so.2 (0x00007f578e9ed000)
            libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f578e628000)
            libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f578e3ea000)
            /lib64/ld-linux-x86-64.so.2 (0x000055f5ccd53000)
            libkrb5.so.3 => /usr/lib/x86_64-linux-gnu/libkrb5.so.3 (0x00007f578e11e000)
            libk5crypto.so.3 => /usr/lib/x86_64-linux-gnu/libk5crypto.so.3 (0x00007f578deef000)
            libcom_err.so.2 => /lib/x86_64-linux-gnu/libcom_err.so.2 (0x00007f578dceb000)
            libkrb5support.so.0 => /usr/lib/x86_64-linux-gnu/libkrb5support.so.0 (0x00007f578dadf000)
            libkeyutils.so.1 => /lib/x86_64-linux-gnu/libkeyutils.so.1 (0x00007f578d8db000)
            libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f578d6bc000)



Note that it is NOT recommended to run ldd with any untrusted third-party executable because some versions of ldd may directly invoke the executable to identify its library dependencies, which can be security risk.
Instead, a safer way to show library dependencies of an unknown application binary is to use the following command.

Syntax:

objdump -p /path/to/program

Example:

[ram@linuxforfreshers.com]$objdump -p /usr/bin/ssh | grep NEEDED
  NEEDED               libselinux.so.1
  NEEDED               libcrypto.so.1.0.0
  NEEDED               libdl.so.2
  NEEDED               libz.so.1
  NEEDED               libresolv.so.2
  NEEDED               libgssapi_krb5.so.2
  NEEDED               libc.so.6

Check shared library dependencies of a running process

If you want to find out what shared libraries are loaded by a running process, you can use pldd command, which shows all shared objects loaded into a process at run-time.
Syntax:

sudo pmap <PID>

Method 2: Using lsof

Syntax:

 lsof -P -T -p Application_PID