su v/s su - command

The su and su - commands in Linux are both used to switch to another user, typically the root user, but they behave differently in terms of the environment they provide.

su command

The su command (short for "switch user") switches the current session to another user account. When used without specifying a username, it defaults to the root account.


Basic Usage:


$ su


This prompts for the root password and opens a new shell as the root user without loading the root user's environment settings, inheriting the environment variables (e.g., PATH) from the original users.


Switching to Another User:


$ su username


This will prompt for the user's password, then open a new shell for user without loading her login environment.

su - command

The su - command provides a "login shell" for the target user, meaning it simulates a full login and loads the target user's environment.


Basic Usage:


$ su -


This prompts for the root password, then switches to a root login shell with root's environmental variables, PATH, HOME, etc., as if the root user had logged in directly.


Switching to Another User with su -


$ su - username


This switches to the user account with his/her full login environment and loads users shell settings, such as environment variables, working directory, and login scripts like .bash_profile and more


Summary of Differences


Command

Environment

Working Directory

Example

su

Inherits the calling user's environment

Stays in the current directory

su
su username

su - 

Loads the target user's full environment (Login Shell)

Switches to the target user's home directory

su -

su - username

What is Library

A library is a collection of pre-compiled code (functions, routines, and subroutines) that programs can use to perform common tasks. Libraries are designed to be reusable, allowing multiple programs to access their functionalities without each needing its own copy of the code. This makes software more efficient, as it reduces redundancy, saves disk space, and optimizes memory usage.

Types of Libraries in Linux


Static Libraries:

  • These libraries are files that are linked directly into the program at compile-time, which means the functions in the library are incorporated into the executable itself.

  • The filename extension for static libraries is usually .a (e.g., libm.a)

  • Advantages: Since everything is included in the executable, the program doesn't rely on external files, which can make it more portable.

  • Disadvantages: The executable size is larger, as all library code is embedded, and updates require recompiling the program with the new library version.


Dynamic (Shared) Libraries:


  • Dynamic libraries are linked at runtime rather than compile-time, meaning they are separate from the executable and loaded into memory only when needed.

  • The filename extension for shared libraries is usually .so (shared object) (e.g., libm.so.6).

  • Advantages: Since the library is loaded at runtime, the executable size is smaller, and multiple programs can share a single instance of the library in memory.

  • Disadvantages: Programs depend on the presence of these libraries in the system, so removing or updating a shared library can affect all programs that use it. 

How Libraries Work in Linux

Libraries provide common code that multiple applications can use, such as handling files, performing calculations, or managing network connections; when an application needs a function, it calls the functions from the library rather than including the code itself.


Example of common libraries:


  • Standard C Library (glibc): Contains basic functions for file handling, memory management, and other core functionalities. It's one of the most essential libraries in Linux.

  • Math Library (libm): Provides mathematical functions, like sin(), cos(), and sqrt().

Linking Libraries in Linux


Static Linking: The library code is embedded directly into the executable during compilation. 


$ gcc -o programname programname.c .path/to/libm.a


Dynamic Linking: The Linker adds references to the library instead of the actual code and the operating system loads the library at runtime.


$ gcc -o programname programname.c -lm 


Commands for working with libraries


ldd command:


  • Lists shared libraries required by a binary

$ ldd /bin/ls


ldconfig command:


  • Updates the system's library cache (useful after installing a new shared library).

$ sudo ldconfig


Libraries in Linux are integral to both system operations and application development, as they provide a structured, modular way of organizing reusable code.


What is Binary

 

A binary typically refers to compiled executable files or programs that the system can run directly. These binaries are written in Binary code (machine code) that the operating system's processor can understand and execute without further translation. They contrast with scripts or source code files, which require an interpreter or compiler to be run.

Types of Binaries


Executable Binaries:

  • These are compiled programs, usually in ELF (Executable and Linkable Format) on Linux System, and can be run directly by the operating system.

  • They are stored in directories such as /bin, /usr/bin, /sbin, and /usr/local/bin.


Library Binaries:


  • Library files, typically with .so extensions (e.g., libc.so), contain shared code that can be used by multiple programs. They are found in directories like /lib, /usr/lib, and /usr/local/lib.

  • These are dynamically linked at runtime to executable binaries that require them, helping save memory and disk space.


Kernel Binaries:


  • The Linux kernel itself and its modules are compiled binary files.

  • They reside in directories such as /boot and /lib/modules.

Common Characteristics of Binaries in Linux

  • Binary Format: Most Linux binaries are in the ELF format, which includes information such as sections, headers, and program data required for execution.

  • Permissions: Binaries need to have execute permissions to run.

  • Path Execution: Binaries in the system PATH (e.g., /bin, /use/bin) can be executed without specifying the full path, while others need an absolute or relative path.



Using Binaries in Linux

  • Running Binaries: If a binary is located in a directory that's part of the PATH environment variable, you can simply type its name in the terminal.


$ ls 


  • For binaries outside of the PATH, specify the full path:


$ /home/usr/scripts/program_name

Inspecting Binaries:


  • Use file command to determine the file type

$ file /bin/ls

  • Use ldd command to list shared libraries required by a binary

$ ldd /bin/ls


  • Use strace command to trace system calls and signals received by a binary

$ strace /bin/ls 

Locating Binaries:


  • Use which command to find a binary's path
    $ which ls


  • Use whereis command to locate binaries, source files, and manuals
    $ whereis ls 


Binaries are fundamental in Linux for running everything from core system programs to user applications. They form the backbone of system operations by offering efficient, precompiled code that the system can execute immediately.