April 26, 2011

Getting around Linux: Cliff Notes

Simple, short and sweet overview of file and directory management on CentOS 5.6

 

Command Description
pwd Print working directory
cd Change directory
mkdir Create a new directory
cp Copy files
mv Move or rename files
rm Delete a file or diretory. -r enables recursion
locate Locate a file specified on the file system
updatedb Updates the file database for the locate command
man Displays the documentation on a command

 

 

Tips: Typing cd or cd ~ always takes you to your home directory. ~ is a reference to your home directory.
Typing cd .. takes you to the upper level in the directory tree, while ‘.’ represents the current directory.
Try ls –l for a more detailed view. The –a switch reveals hidden and system files.

Superuser account

Each and every operating system comes with a Superuser account, often required for performing administrative-level tasks. This account is usually called root, although on some systems it can be named otherwise (‘admin’ or even ‘toor’). The Superuser has access to all fles and directories of the system and has the right to read, edit,  and execute all fles as well as change fle attributes and permissions.

Although an administrator should always have the possibility to access the root account, it is not recommended to constantly connect as the machine Superuser. In fact, some operating systems such as Ubuntu do not even allow you to do so. One of the great principles of computer security is least privilege—you should never be allowed to do more than what you need to do. Granting more privileges than one requires can only lead to situations where the system security and integrity get compromised. For that reason, it is highly recommended that you create user accounts, not only for physical users of your machine but also for applications to run in a secure environment with clearly  defned boundaries.

User accounts

One particular file in the system confguration directory holds the list of system users: /etc/passwd. Contrary to what the name suggests, it does not usually contain user passwords; they are, in most cases, stored using the shadow format in a separate file /etc/shadow for security reasons. It does, however, come with certain bits of information for each user. One line of the password file representing one user, the following syntax should be respected:

Name:password:ID:group ID:comment:home directory:login shell

The most basic syntax to add a user is: useradd username. This creates a new user account with the default settings (which can be customized)—a home directory for the user located in /home, no expiration date, the default group for users, and Bash as a login shell. If you add an account destined to be running a service such as syslog, it is recommended that you do not grant the user account shell access; consequently, you should make sure that the login shell is set to nologin (usually found in /sbin/nologin). The command would then be:

useradd --shell /sbin/nologin syslog

Group management

In addition to user accounts, Unix-based systems provide an even more advanced resource management mechanism—user groups. The purpose of a group is to have its own access permissions on fles and directories; all users belonging to the group will then inherit the group permissions. A user account has to belong to at least one group—the user’s primary group—although it may also belong to secondary groups.In practice, the list of groups on the system is stored in the /etc/group fle. Each line of the fle represents one group, respecting the following syntax:

Group name:password:group ID:user list

Again, if you wish to create a new group on your system, you have two options: either add a new line to the /etc/group file, or use the dedicated groupadd command. Its syntax is simple—groupadd groupname. There are some optional parameters to the command, which you can discover by running man groupadd. Similar to the user management system, you will also fnd groupmod and  groupdel commands for respectively editing group settings and deleting a group. More importantly, how to add a user to a group? It is done by either editing the  /etc/group file to append the username at the end of the line corresponding to  the group you wish to add the user to, or by using the following command: usermod --append --groups groupname username

Programs and processes

Running a program in the shell is not as simple as entering its flename. There are a couple of subtle details that you should understand about the way Bash handles the execution of binaries and scripts.

Starting an application

There are three different situations that you may face when you want to execute a program or a script from the shell: The program you want to execute is located in the current working directory. Solution: Prefx the flename with ./ (dot slash), which forces the shell to look for fles in the current working directory only. In order to start a program and have it running in the background instead of blocking the shell completely. This is done by appending a simple & at the end of the line. As soon as you send the command, the pid (Process Identifer—a number identifying a running process on your system) will show up and the prompt will return. Once the execution terminates, a message appears to indicate its completion, along with the original command used to start the process.

Process management

Linux allocates a number to each and every process running on the computer. This number is called the Process Identifer (pid). Knowing the pid is important in various situations, some of which you are  about to discover. Firstly, how does one fnd the pid of a process? Although there are a number of ways you could do that, most of them rely on a single tool—ps. Its many options (combined with the piping mechanism) will allow you to retrieve various details:

The ps aux | grep sshd command can be dissected into three components:

  1. 1.  ps aux is a command that lists all processes currently running on the system.
  2. 2.  | (pipe) redirects the output of the command placed before the pipe to the command placed after it. Running ps aux generally returns a long list of processes, so you’ll want to shorten the list to a single process.
  3. 3.  grep syslogd receives data from the ps aux command and only outputs  lines containing the specifed words. In other words, grep acts as the  flter, retaining lines containing syslogd.

An administrator’s best friend—top

Another tool that you will fnd particularly useful if you run a high traffc website is top. This program lists all the processes currently running on the system with their pid, which is sorted by their CPU usage. On top of that, the list refreshes everysecond until you interrupt the execution fow (with Ctrl+C, for example) or stop the application by pressing the Q key. This allows you to keep track of the most resource-hungry processes.

Linux Filesystem

Directory Description
/ Primary hierarchy root and root directory of the entire file system hierarchy.
/bin/ Essential command binaries that need to be available in single user mode; for all users, e.g., cat, ls, cp.
/boot/ Boot loader files, e.g., kernels, initrd; often a separate partition
/dev/ Essential devices, e.g./dev/null.
/etc/ Host-specific system-wide configuration files
There has been controversy over the meaning of the name itself. In early versions of the UNIX Implementation Document from Bell labs, /etc is referred to as the /etcetra directory, as this directory historically held everything that did not belong elsewhere (however, the FHS restricts /etc to static configuration files and may not contain binaries). Since the publication of early documentation, the directory name has been re-designated in various ways. Recent interpretations include Backronyms such as “Editable Text Configuration” or “Extended Tool Chest”.
/etc/opt/
Configuration files for /opt/.
/etc/X11/
Configuration files for the X Window System, version 11.
/etc/sgml/
Configuration files for SGML.
/etc/xml/
Configuration files for XML.
/home/ Users’ home directories, containing saved files, personal settings, etc.; often a separate partition.
/lib/ Libraries essential for the binaries in /bin/ and /sbin/.
/media/ Mount points for removable media such as CD-ROMs (appeared in FHS-2.3).
/mnt/ Temporarily mounted filesystems.
/opt/ Optional application software packages.
/proc/ Virtual filesystem documenting kernel and process status as text files, e.g., uptime, network. In Linux, corresponds to a Procfs mount.
/root/ Home directory for the root user.
/sbin/ Essential system binaries, e.g., init, ip, mount.
/srv/ Site-specific data which is served by the system.
/tmp/ Temporary files (see also /var/tmp). Often not preserved between system reboots.
/usr/ Secondary hierarchy for read-only user data; contains the majority of (multi-)user utilities and applications.
/usr/bin/
Non-essential command binaries (not needed in single user mode); for all users.
/usr/include/
Standard include files.
/usr/lib/
Libraries for the binaries in /usr/bin/ and /usr/sbin/.
/usr/sbin/
Non-essential system binaries, e.g., daemons for various network-services.
/usr/share/
Architecture-independent (shared) data.
/usr/src/
Source code, e.g., the kernel source code with its header files.
/usr/X11R6/
X Window System, Version 11, Release 6.
/usr/local/
Tertiary hierarchy for local data, specific to this host. Typically has further subdirectories, e.g.bin/lib/share/.
/var/ Variable files—files whose content is expected to continually change during normal operation of the system—such as logs, spool files, and temporary e-mail files. Sometimes a separate partition.
/var/cache/
Application cache data. Such data is locally generated as a result of time-consuming I/O or calculation. The application must be able to regenerate or restore the data. The cached files can be deleted without data loss
/var/lib/
State information. Persistent data modified by programs as they run, e.g., databases, packaging system metadata, etc.
/var/lock/
Lock files. Files keeping track of resources currently in use.
/var/log/
Log files. Various logs.
/var/mail/
Users’ mailboxes.
/var/run/
Information about the running system since last boot, e.g., currently logged-in users and running daemons.
/var/spool/
Spool for tasks waiting to be processed, e.g., print queues and unread mail.
/var/spool/mail/
Deprecated location for users’ mailboxes.
/var/tmp/
Temporary files to be preserved between reboots.
pwd Print working directory
cd Change directory
mkdir Create a new directory
cp Copy files
mv Move or rename files
rm Delete a file or diretory. -r enables recursion
locate Locate a file specified on the file system
updatedb Updates the file database for the locate command
man Displays the documentation on a command

4 Responses

  1. What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.

  2. daigoumee says:

    Do you people have a facebook fan page? I looked for one on twitter but could not discover one, I would really like to become a fan!

  3. Livegigs says:

    Hey There. I found your blog the use of msn. That is a really neatly written article. I’ll make sure to bookmark it and come back to learn extra of your helpful info. Thanks for the post. I’ll definitely return.

Leave a Reply