Wednesday, December 11, 2013

Linux Overview



Linux is a hardware independent architecture derived from UNIX. It is divided into three levels. They are user, kernel and hardware levels. The hardware level contains device drivers and machine specific components. The kernel level is a mix of machine-dependent and machine-independent software. The user level is a collection of applications, like shells, editors and utilities.





Shorthand at the Command Prompt
  • / - root directory
  • ./ - current directory
  • ../ - parent directory
  • ~ - home directory
  • $ - typical prompt when logged in as ordinary user
  • # - typical prompt when logged in as root or superuser
  • ! - repeat specified command
  • !! - repeat previous command
  • & - run a program in background mode
  • x[Tab][Tab] - prints a list of all available completions for a command, where the beginning is ``x''
  • [Alt][Ctrl][F1] - switch to the first virtual text console
  • [Alt][Ctrl][Fn] - switch to the nth virtual text console. Typically, there are six on a Linux PC system.
  • [ArrowUp] - scroll through the command history (in bash)
  • [Shift][PageDown] - scroll terminal output down
  • [Ctrl][Alt][Del] - shut down the system and reboot
  • [Ctrl]c - kill the current process
  • [Ctrl]d - logout from the current terminal
  • [Ctrl]s - stop transfer to current terminal
  • [Ctrl]q - resume transfer to current terminal. This should be tried if the terminal stops responding.
  • [Ctrl]z - send current process to the background
  • [Leftmousebutton] - Hold down left mouse button and drag to highlight text. Releasing the button copies the region to the text buffer under X and (if gpm is installed) in console mode.
  • [Middlemousebutton] - Copies text from the text buffer and inserts it at the cursor location. With a two-button mouse, click on both buttons simultaneously. It is necessary for three-button emulation to be enabled, either under gpm or in XF86Config.
Typical Dot Files

  • .bash_logout - file executed by bash shell on logout
  • .bash_profile - initialization of bash shell run only on login. Bash looks first for a .bash_profile file when started as a login shell or with the -login option. If it does not find .bash_profile, it looks for .bash_login. If it doesn't find that, it looks for .profile. System-wide functions and aliases go in /etc/bashrc and default environment variables go in /etc/profile.
  • .bashrc - initialization command run when bash shell starts up as a non-login shell
Useful Files
  • /boot/vmlinuz - the typical location and name of the Linux kernel. In the Slackware distribution, the kernel is located at /vmlinuz.
  • /dev/fd0 - first floppy disk drive
  • /dev/fd0H1440 - driver for the first floppy drive in high density mode. Generally, this is invoked when formatting a floppy drive for a particular density. Slackware comes with drivers that allow for formatting a 3.5" diskette with up to 1.7MB of space. Red Hat and Mandrake do not contain these device driver files by default.
  • /dev/fd1 - second floppy disk drive
  • /dev/hda - first IDE hard drive
  • /dev/hdc - on many machines, the IDE cdrom drive. Most often, there is a symbolic link called /dev/cdrom which is just a link to the true cdrom driver file.
  • /dev/null - used when you want to send output into oblivion
  • /etc/aliases - file containing aliases used by sendmail and other MTAs (mail transport agents). After updating this file, it is necessary to run the newaliases utility for the changes to be passed to sendmail.
  • /etc/bashrc - system-wide default functions and aliases for the bash shell
  • /etc/conf.modules - aliases and options for configurable modules
  • /etc/crontab - shell script to run different commands periodically (hourly, daily, weekly, monthly, etc.)
  • /etc/DIR_COLORS - used to store colors for different file types when using ls command. The dircolors command uses this file when there is not a .dir_colors file in the user's home directory. Used in conjunction with the eval command (see below).
  • /etc/exports - specifies hosts to which file systems can be exported using NFS. Man exports contains information on how to set up this file for remote users.
  • /etc/fstab - contains information on partitions and filesystems used by system to mount different partitions and devices on the directory tree
  • /etc/HOSTNAME - stores the name of the host computer
  • /etc/hosts - contains a list of host names and absolute IP addresses.
  • /etc/hosts.allow - hosts allowed (by the tcpd daemon) to access Internet services
  • /etc/hosts.deny - hosts forbidden (by the tcpd daemon) to access Internet services
  • /etc/group - similar to /etc/passwd but for groups
  • /etc/inetd.conf - configures the inetd daemon to tell it what TCP/IP services to provide (which daemons to load at boot time). A good start to securing a Linux box is to turn off these services unless they are necessary.
  • /etc/inittab - runs different programs and processes on startup. This is typically the program which is responsible for, among other things, setting the default runlevel, running the rc.sysinit script contained in /etc/rc.d, setting up virtual login terminals, bringing down the system in an orderly fashion in response to [Ctrl][Alt][Del], running the rc script in /etc/rc.d, and running xdm for a graphical login prompt (only if the default runlevel is set for a graphical login).
  • /etc/issue - pre-login message. This is often overwitten by the /etc/rc.d/rc.S script (in Slackware) or by the /etc/rc.d/rc.local script (in Mandrake and Red Hat, and perhaps other rpm-based distributions). The relevant lines should be commented out (or changed) in these scripts if a custom pre-login message is desired.
  • /etc/lilo.conf - configuration file for lilo boot loader
  • /etc/motd - message of the day file, printed immediately after login. This is often overwritten by /etc/rc.d/rc.S (Slackware) or /etc/rc.d/rc.local (Mandrake/Red Hat) on startup. See the remarks in connection with /etc/issue.
  • /etc/mtab - shows currently mounted devices and partitions and their status
  • /etc/passwd - contains passwords and other information concerning users who are registered to use the system. For obvious security reasons, this is readable only by root. It can be modified by root directly, but it is preferable to use a configuration utility such as passwd to make the changes. A corrupt /etc/passwd file can easily render a Linux box unusable.
  • /etc/printcap - shows the setup of printers
  • /etc/profile - sets system-wide defaults for bash shell. It is this file in Slackware that sets up the DIR_COLORS environment variable for the color ls command. Also sets up other system-wide environment variables.
  • /etc/resolv.conf - contains a list of domain name servers used by the local machine
  • /etc/securetty - contains a list of terminals on which root can login. For security reasons, this should not include dialup terminals.
  • /etc/termcap - ASCII database defining the capabilities and characteristics of different consoles, terminals, and printers
  • /etc/X11/XF86Config - X configuration file. The location in Slackware is /etc/XF86Config.
  • /proc/cpuinfo - cpu information
  • /proc/filesystems - prints filesystems currently in use
  • /proc/interrupts - prints interrupts currently in use
  • /proc/ioports - contains a list of the i/o addresses used by various devices connected to the computer
  • /proc/kcore - The command ls -l /proc/kcore will give the amount of RAM on the computer. It's also possible to use the free command to get the same information (and more).
  • /proc/version - prints Linux version and other info
  • /var/log/messages - used by syslog daemon to store kernel boot-time messages
  • /var/log/lastlog - used by system to store information about last boot
  • /var/log/wtmp - contains binary data indicating login times and duration for each user on system


Important Directories


  • /bin - essential UNIX commands such as ls, etc. Should contain all binaries needed to boot the system or run it in single-user mode
  • /boot - files used during booting and possibly the kernel itself are stored here
  • /dev - contains device files for various devices on system
  • /etc - files used by subsystems such as networking, NFS, and mail. Includes tables of disks to mount, processes to run on startup, etc.
  • /etc/profile.d - contains scripts that are run by /etc/profile upon login.
  • /etc/rc.d - contains a number of shell scripts that are run on bootup at different run levels. There is also typically an rc.inet1 script to set up networking (in Slackwar), an rc.modules script to load modular device drivers, and an rc.local script that can be edited to run commands desired by the administrator, along the lines of autoexec.bat in DOS.
  • /etc/rc.d/init.d - contains most of the initialization scripts themselves on an rpm-based system.
  • /etc/rc.d/rc*.d - where ``*'' is a number corresponding to the default run level. Contains files for services to be started and stopped at that run level. On rpm-based systems, these files are symbolic links to the initialization scripts themselves, which are in /etc/rc.d/init.d.
  • /etc/skel - directory containing several example or skeleton initialization shells. Often contains subdirectories and files used to populate a new user's home directory.
  • /etc/X11 - configuration files for the X Window system
  • /home - home directories of individual users
  • /lib - standard shared library files
  • /lib/modules - modular device driver files, most with .o extensions
  • /mnt - typical mount point for many user-mountable devices such as floppy drives, cd-rom readers, etc. Each device is mounted on a subdirectory of /mnt.
  • /proc - virtual file system that provides a number of system statistics
  • /root - home directory for root
  • /sbin - location of binaries used for system administration, configuration, and monitoring
  • /tmp - directory specifically designed for programs and users to store temporary files.
  • /usr - directory containing a number of subdirectory with programs, libraries, documentation, etc.
  • /usr/bin - contains most user commands. Should not contain binaries necessary for booting the system, which go in /bin. The /bin directory is generally located on the same disk partition as /, which is mounted in read-only mode during the boot process. Other filesystems are only mounted at a later stage during startup, so putting binaries essential for boot here is not a good idea.
  • /usr/bin/X11 - most often a symbolic link to /usr/X11R6/bin, which contains executable binaries related to the X Window system
  • /usr/doc - location of miscellaneous documentation, and the main location of program documentation files under Slackware
  • /usr/include - standard location of include files used in C programs such as stdio.h
  • /usr/info - primary location of the GNU info system files
  • /usr/lib - standard library files such as libc.a. Searched by the linker when programs are compiled.
  • /usr/lib/X11 - X Window system distribution
  • /usr/local/bin - yet another place to look for comon executables
  • /usr/man - location of manual page files
  • /usr/sbin - other commands used by superuser for system administration
  • /usr/share - contains subdirectories where many installed programs have configuration, setup and auxiliary files
  • /usr/share/doc - location of program documentation files under Mandrake and Red Hat
  • /usr/src - location of source programs used to build system. Source code for programs of all types are often unpacked in this directory.
  • /usr/src/linux - often a symbolic link to a subdirectory whose name corresponds to the exact version of the Linux kernel that is running. Contains the kernel sources.
  • /var - administrative files such as log files, used by various utilities
  • /var/log/packages - contains files, each of which has detailed information on an installed package in Slackware. The same file can also be found at /var/adm/packages, since the adm subdirectory is a symbolic link to log. Each package file contains a short description plus a list of all installed files.
  • /var/log/scripts - package installation scripts in Slackware are stored here. You can inspect these scripts to see what special features are included in individual packages.
  • /var/spool - temporary storage for files being printed, mail that has not yet been picked up, etc. 



GNU Binary Utilities


Below is list of several GNU utilities where we can use these in our day-to-day software development

ld
nm
ldd
ar
c++filt
objdump 
strip
readelf
ranlib
size  
strings
ldconfig 
catchsegv


ldd
Another very useful tool for working with shared libraries is ldd. It tells you which shared libraries an executable program uses. print shared library dependencies  Here's an example:

papaya$ ldd wibble
libstuff.so => libstuff.so (0x400af000)
libm.so.5 => /lib/libm.so.5 (0x400ba000)
libc.so.5 => /lib/libc.so.5 (0x400c3000)

The three fields in each line are the name of the library, the full path to the instance of the library that is used, and where in the virtual address space the library is mapped to.

If ldd outputs not found for a certain library, you are in trouble and won't be able to run the program in question. You will have to search for a copy of that library. Perhaps it is a library shipped with your distribution that you opted not to install, or it is already on your hard disk, but the loader (the part of the system that loads every executable program) cannot find it.
Ex :
ldd  scimAppD
ldd libSCIM_CAPD.so

nm
This command allows you to retrieve information on symbol names inside an object file or executable file. By default, the output gives you a symbol name and its virtual address. What good is that? Suppose you are compiling code and the compiler complains that you have an unresolved symbol _foo. You search all of your source code and cannot find anywhere where you use this symbol. Perhaps it got pulled in from some template or a macro buried in one of the dozens of include files that compiled along with your code.
 The command:

nm -guA *.o | grep foo

shows all the modules that refer to foo. If you want to find out what library defines foo, simply use:

nm -gA /usr/lib/* | grep foo

GNU nm lists the symbols from object files objfile.... If no object files are listed as arguments, nm assumes the file `a.out'. The command "nm" lists symbols contained in the object file or shared library.

ar
The GNU ar program creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original individual files (called members of the archive).

C++filt
The C++ and Java languages provides function overloading, which means that you can write many functions with the same name (providing each takes parameters of different types). All C++ and Java function names are encoded into a low-level assembly label (this process is known as mangling). The c++filt (1) program does the inverse mapping: it decodes (demangles) low-level names into user-level names so that the linker can keep these overloaded functions from clashing. reverses the C++ symbol name-mangling that gcc(1) does, so if the program uses c++ then you can read the function names.

·       catchsegv - prints out the backtrace of function calls for a program receiving a SIGSEGV. - don't forget to compile with -g if you want file names and line numbers to be accessible.

objdump displays information about one or more object files. The options control what particular information to display. This information is mostly useful to programmers who are working on the compilation tools, as opposed to programmers who just want their program to compile and work.

·       GNU strip discards all symbols from object files objfile. The list of object files may include archives. At least one object file must be given.

·       readelf displays information about one or more ELF format object files. The options control what particular information to display.
·       The GNU size utility lists the section sizes--and the total size--for each of the object or archive files objfile in its argument list. By default, one line of output is generated for each object file or each module in an archive.


·       For each file given, GNU strings prints the printable character sequences that are at least 4 characters long (or the number given with the options below) and are followed by an unprintable character. By default, it only prints the strings from the initialized and loaded sections of object files; for other types of files, it prints the strings from the whole file.

ldconfig
By default, the loader looks only in /lib and /usr/lib. If you have libraries in another directory, create an environment variable LD_LIBRARY_PATH and add the directories separated by colons. If you believe that everything is set up correctly, and the library in question still cannot be found, run the command ldconfig as root, which refreshes the linker system cache.

About the Author

I am currently working as Senior Developer at CISCO Systems, India.
I Pursued M.Tech degree in Computer Science from V.T.U, Bangalore and B.Tech in Computer Science from Nagarjuna University.

My Hobbies are Coding , Watching TV, Listening to music.
Reach me at naresh.master3@gmail.com