Working with the Linux File System


Ubuntu uses a file system, or layout of hierarchical directories similar to that used by other UNIX variants (such as Mac OS X). Nearly all Linux distributions use a similar directory structure, and Linux distribution vendors have generally agreed on the naming and location of critical Linux files and directories.

Note

The effort to build a consensus regarding the Linux directory structure began in 1993 with the Filesystem Hierarchy Standard (FHS), a draft proposal that addressed not only Linux issues, but also those of other operating systems, such as BSD. Red Hat has stated that it is committed to stay compliant with the FHS, which specifies the location and names of files and directories.

Ubuntu uses the current 2.3 standard. Key additions to this include the creation of a /media directory for removable storage devices such as DVD drives and zip disks (although keeping /mnt as a temporary mount point). udev has also been added to Ubuntu; although it is not a part of FHS 2.3, it enables /dev to become a dynamically managed folder allowing the hot-plugging of devices onto the system and the creation (on demand) of device nodes.


The commonality of how the Linux directory structure is laid out is very useful for open source developers because it cuts down the amount of work they have to do to get their programs to work with different distributions. For a programmer to know, for example, that the useradd command is always under /usr/sbin means that he can create shell scripts and other utilities that take advantage of this, and know that they will work universally. Perhaps unsurprisingly, given the ancestry of Linux, you will find that other UNIX-like operating systems follow the same directory organization. Of course, you will not get the most of knowing this secret unless you actually learn a little about how the directories are organized, along with the contents of files and directories, and where software should be installed and files stored.

A good knowledge of the Linux file system pays dividends to pretty much every system administrator. Knowing where to find configuration files as well as the location of key directories will help you fix your system faster when you have a problem.

Viewing the Linux File System

Look at the layout of a typical Ubuntu system by using the list directory contents command, ls, like this:

$ ls / bin    dev    initrd          lib         mnt   root  sys  var boot   etc    initrd.img      lost+found  opt   sbin  tmp  vmlinuz cdrom  home   initrd.img.old  media       proc  srv   usr  vmlinuz.old 


Note

This section provides an overview of the Ubuntu file system. You might find a fewer or greater number of directories than discussed here in your own system. When some software packages are installed, they create new directories. Updating software packages might also remove or change the name of some directories. See Chapter 7, "Managing Software," for more information on installing, upgrading, or removing software from your Linux system.


To get a more detailed picture, install and use the TRee command to show the root or base directory layout, along with associated subdirectories, like this (note that your system's /usr/src directory might be somewhat different, depending on the version of Ubuntu you have installed or if you have updated Ubuntu with a new kernel, and that not all subdirectories are listed):

$ tree -dx / / |-- bin |-- boot |   `-- grub |-- cdrom -> media/cdrom |-- dev |-- etc |   |-- X11 |   |-- acpi |   |-- apt |   |-- beagle |   |-- cups |   |-- gdm |   |-- ppp |-- home |   `-- andrew |-- initrd |-- lib |   |-- modules |-- lost+found |-- media |   |-- USB |   |-- cdrom -> cdrom0 |   |-- cdrom-1 |   `-- cdrom0 |-- mnt |-- opt |-- proc |-- root |-- sbin |-- srv |-- sys |-- tmp |-- usr |   |-- X11R6 |   |   |-- bin |   |   `-- lib |   |-- bin |   |   `-- X11 -> ../bin |   |-- games |   |-- include |   |-- lib |   |-- local |   |-- sbin |   |-- share |   `-- src |       |-- linux-headers-2.6.15-22 |       |-- linux-headers-2.6.15-22-386 |       |-- linux-headers-2.6.15-23 |       `-- linux-headers-2.6.15-23-386 `var 


This example (pruned from more than 18,000 directories) shows the higher-level directories and corresponds to the directories and descriptions in Table 5.1.

Table 5.1. Basic Linux Directories

Name

Description

/

The root directory

/bin

Essential commands

/boot

Boot loader files, Linux kernel

/dev

Device files

/etc

System configuration files

/home

User home directories

/initrd

Initial RAM disk boot support (used during boot time)

/lib

Shared libraries, kernel modules

/lost+found

Directory for recovered files (if found after a file system check)

/media

Mount point for removable media such as DVDs and floppy disks

/mnt

Usual mount point for local, remote file systems

/opt

Add-on software packages

/proc

Kernel information, process control

/root

Super-user (root home)

/sbin

System commands (mostly root only)

/srv

Holds data for system services some of which is currently stored in /var

/sys

Real-time information on devices used by the kernel

/tftpboot

Network boot support

/tmp

Temporary files

/usr

Secondary software file hierarchy

/var

Variable data (such as logs); spooled files


Some of the important directories in Table 5.1, such as those containing user and root commands or system configuration files, are discussed in the following sections. You will use and edit files under these directories when you use Ubuntu.

Use Essential Commands from the /bin and /sbin Directories

The /bin directory (about 4.1MB after a default install) contains essential commands used by the system when running and booting Linux. In general, only the root operator uses the commands in the /sbin directory. Many (though not all) of these commands are statically linked; such commands do not depend on software libraries residing under the /lib or /usr/lib directories. Nearly all the other applications on your system are dynamically linkedmeaning that they require external software libraries (also known as shared libraries) in order to run.

Tip

Because the system contains dynamically linked applications, you might sometimes get dependency errors when installing or upgrading software packages; in those situations, a supporting library (or application) might not be present. See Chapter 7 for more information on working with dynamically linked applications and other methods of avoiding such problems. Thankfully, "dependency hell" is largely a thing of the past due to programs such as yum.


Store the Booted Kernel and View Stored Devices in the /boot and /dev Directories

The /boot directory contains a compressed version of the Linux kernel (loaded at boot time), along with other files that describe the kernel or provide information for booting Linux. When you rebuild or install a new kernel, the kernel and related files are placed in this directory (see Chapter 35, "Kernel and Module Management," for more information on rebuilding or installing a kernel).

Linux device files are contained under the /dev directory. Note that under Linux, nearly everything on your system is a file. This means that (with the exception of network interfaces; see the note that follows the upcoming list) regular files; directories; hard drive partitions; serial, printer, or USB ports; and video and sound devices all are files!

The /dev directory contains more than 800 files representing devices that may or may not be in use on your system. Some of the most commonly used devices in this directory include

  • IDE (Integrated Drive Electronics) hard drives, such as /dev/hda and /dev/hdb

  • CD-ROM drives; some of which are IDE, others of which are CD-RW (CD read/write) drives emulated as SCSI (Small Computer Systems Interface) devices such as /dev/scd0

  • Serial ports, such as /dev/ttyS0 for COM1, /dev/ttyS1 for COM2, and so on

  • Pointing devices, including /dev/input/mice and others

  • Printers, such as /dev/lp0

Note

Network interfaces (such as eth0 or ppp0) are not represented by Linux device files, but are created in memory when activated. See Chapter 18, "Network Connectivity," for more information.


Use and Edit Files in the /etc Directory

More than 2300 system configuration files and directories reside under the /etc directory if you carry out a default install of the software included with this book. Some major software packages, such as Apache, OpenSSH, and xinetd, have directories of configuration files under /etc. Other important system-related configuration files in /etc are

  • fstab The system file system table is a text file listing each hard drive, CD-ROM, floppy, or other storage device attached to your PC. The table indexes each device's partition information with a place in your Linux file system (directory layout) and lists other options for each device when used with Linux. Nearly all entries in fstab can be manipulated by root using the mount command.

  • inittab The system initialization table defines the default runlevel, also known as run-control level or system state. Changes to this file can determine whether your system boots to a graphical or text login, as well as whether dial-up remote access is enabled. (You learn about default runlevels in the section "System Services and Runlevels" located in Chapter 15, "Automating Tasks.")

  • modprobe.conf This configuration file contains directions and options used when loading kernel modules to enable various types of hardware, such as sound, USB, networking, and so on (discussed in the section "Managing Modules" in Chapter 35). The contents of this file are used during boot time, and the file can be manually edited or automatically updated by Ubuntu.

  • passwd The list of users for the system, along with user account information. The contents of this file can be changed by various programs, such as useradd or chsh.

  • printcap The system's printer capabilities database (discussed in the section "Overview of Ubuntu Printing" in Chapter 12, "Printing with Ubuntu").

  • shells A list of approved shells (command-line interfaces).

Protect the Contents of User Directories/home

The most important data on a Linux system reside in user's directories, found under the /home directory. Segregating the system and user data can be helpful in preventing data loss and making the process of backing up easier. For example, having user data reside on a separate file system or mounted from a remote computer on the network might help shield users from data loss in the event of a system hardware failure.

Use the Contents of the /proc Directory to Interact with the Kernel

The content of the /proc directory is created from memory and only exists while Linux is running. This directory contains special "files" that either extract information from or send information to the kernel. Many Linux utilities extract information from dynamically created directories and files under this directory, also known as a virtual file system. For example, the free command obtains its information from a file named meminfo:

$ free              total       used       free     shared     buffers    cached Mem:       1036096    1022940      13156          0      121780    476892 -/+ buffers/cache:     424268     611828 Swap:            0          0          0 


This information constantly changes as the system is used. You can get the same information by using the cat command to see the contents of the meminfo file:

$ cat /proc/meminfo MemTotal:      1036096 kB MemFree:         12024 kB Buffers:        121808 kB Cached:         477672 kB SwapCached:          0 kB Active:         609764 kB Inactive:       286756 kB HighTotal:      131008 kB HighFree:          168 kB LowTotal:       905088 kB LowFree:         11856 kB SwapTotal:           0 kB SwapFree:            0 kB Dirty:               4 kB Writeback:           0 kB Mapped:         403412 kB Slab:           103856 kB CommitLimit:    518048 kB Committed_AS:   771220 kB PageTables:       2432 kB VmallocTotal:   114680 kB VmallocUsed:     10596 kB VmallocChunk: 103800 kB 


The /proc directory can also be used to dynamically alter the behavior of a running Linux kernel by "echoing" numerical values to specific files under the /proc/sys directory.

Note

The Linux kernel has a number of built-in protections, but good system administration security policies and a secure firewall protecting your gateway, router, or Internet-connected system are the best protection you can use.


Other ways to use the /proc directory include

  • Getting CPU information, such as the family, type, and speed from /proc/cpuinfo.

  • Viewing important networking information under /proc/net, such as active interfaces information under /proc/net/dev, routing information in /proc/net/route, and network statistics in /proc/net/netstat.

  • Retrieving file system information.

  • Reporting media mount point information via USB; for example, the Linux kernel will report what device to use to access files (such as /dev/sda) if a USB camera or hard drive is detected on the system. You can use the dmesg command to see this information or find information about these devices under the Device File System directory /proc/devfs (see the Linux Devfs FAQ at http://www.atnf.csiro.au/~rgooch/linux/docs/devfs.html if the source code for the Linux kernel is installed). The file /usr/src/linux-2.6/Documentation/usb/proc_usb_info.txt contains general information about USB and the /proc directory, as well as what to expect in files under this directory. Note that devfs might be supported, but will generally be obsolete in the 2.6 kernel because /proc/udev replaces it as a way of managing hot-plug devices on your system.

  • Gettingthe kernel version in /proc/version, performance information such as uptime in /proc/uptime, or other statistics such as CPU load, swap file usage, and processes in /proc/stat.

Work with Shared Data in the /usr Directory

The /usr directory (nearly 2GB in size if you do a default install) contains software applications, libraries, and other types of shared data for use by anyone on the system. Many Linux system administrators give /usr its own partition. A number of subdirectories under /usr contain the X Window System (/usr/X11R6), manual pages (/usr/share/man), software package shared files (/usr/share/name_of_package, such as /usr/share/emacs), additional application or software package documentation (/usr/share/doc), and an entire subdirectory tree of locally built and installed software, /usr/local.

Note

Although there is a /usr/X11R6 directory, it actually links through to /usr/bin. The reason for this round robin approach is to maintain compatibility with applications that still expect X to reside under /usr/X11R6. X11R7 does away with a separate directory under /usr hence why this link is in place.


Temporary File Storage in the /tmp Directory

As its name implies, the /tmp directory is used for temporary file storage; as you use Linux, various programs create files in this directory. The /tmp directory is cleaned of stale files each day by the tmpwatch command. (A stale file is any file not used after 10 days.) Ubuntu is configured by default to use tmpwatch to check /tmp each day by settings in your system's scheduling table, /etc/crontab.

Access Variable Data Files in the /var Directory

The /var directory contains subdirectories used by various system services for spooling and logging. Many of these variable data files, such as print spooler queues, are temporary, whereas others, such as system and kernel logs, are renamed and rotated in use. Incoming electronic mail is usually directed to files under /var/spool/mail.

Linux also uses /var for other important system services. These include the top-most File Transfer Protocol (FTP) directory under /var/ftp (see Chapter 23, "Remote File Serving with FTP"), and the Apache web server's initial home page directory for the system, /var/www/html. (See Chapter 20, "Apache Web Server Management," for more information on using Apache.)



Ubuntu Unleashed
Ubuntu Unleashed 2011 Edition: Covering 10.10 and 11.04 (6th Edition)
ISBN: 0672333449
EAN: 2147483647
Year: 2006
Pages: 318

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net