Thursday, July 30, 2009

Linux Commands


System Information Commands

  • df: The df command displays filesystem disk space usage for all mounted partitions. "df -h" is probably the most useful - it uses megabytes (M) and gigabytes (G) instead of blocks to report. (-h means "human-readable")

  • du: The du command displays the disk usage for a directory. It can either display the space used for all subdirectories or the total for the directory you run it on. Example:

user@users-desktop:~$ du /media/floppy 1032    /media/floppy/files 1036    /media/floppy/ user@users-desktop:~$ du -sh /media/floppy 1.1M    /media/floppy/
  • -s means "Summary" and -h means "Human Readable"

  • free: The free command displays the amount of free and used memory in the system. "free -m" will give the information using megabytes, which is probably most useful for current computers.

  • top: The top command displays information on your Linux system, running processes and system resources, including CPU, RAM & swap usage and total number of tasks being run. To exit top, press"q".

  • uname -a: The uname command with the -a option prints all system information, including machine name, kernel name & version, and a few other details. Most useful for checking which kernel you're using.

  • lsb_release -a: The lsb_release command with the -a option prints version information for the Linux release you're running, for example:

Clearing history from terminal
history -c


For listing shares in a network
~$ smbclient -L 192.168.1.2 -U%


For mounting a file in network

dbott@gutsy:~$ sudo mount -t cifs //192.168.1.2/Music /home/dbott/Music -o iocharset=utf8,file_mode=0777,dir_mode=0777

File protection with chmod

File protection with chmod
CommandMeaning
chmod 400 fileTo protect a file against accidental overwriting.
chmod 500 directoryTo protect yourself from accidentally removing, renaming or moving files from this directory.
chmod 600 fileA private file only changeable by the user who entered this command.
chmod 644 fileA publicly readable file that can only be changed by the issuing user.
chmod 660 fileUsers belonging to your group can change this file, others don't have any access to it at all.
chmod 700 fileProtects a file against any access from other users, while the issuing user still has full access.
chmod 755 directoryFor files that should be readable and executable by others, but only changeable by the issuing user.
chmod 775 fileStandard file sharing mode for a group.
chmod 777 fileEverybody can do everything to this file.

Logging on to another group

When you type id on the command line, you get a list of all the groups that you can possibly belong to, preceded by your user name and ID and the group name and ID that you are currently connected with. However, on many Linux systems you can only be actively logged in to one group at the time. By default, this active or primary group is the one that you get assigned from the /etc/passwd file. The fourth field of this file holds users' primary group ID, which is looked up in the /etc/group file. An example:


Special modes

For the system admin to not be bothered solving permission problems all the time, special access rights can be given to entire directories, or to separate programs. There are three special modes:

  • Sticky bit mode: After execution of a job, the command is kept in the system memory. Originally this was a feature used a lot to save memory: big jobs are loaded into memory only once. But these days memory is inexpensive and there are better techniques to manage it, so it is not used anymore for its optimizing capabilities on single files. When applied to an entire directory, however, the sticky bit has a different meaning. In that case, a user can only change files in this directory when she is the user owner of the file or when the file has appropriate permissions. This feature is used on directories like /var/tmp, that have to be accessible for everyone, but where it is not appropriate for users to change or delete each other's data. The sticky bit is indicated by a t at the end of the file permission field:

    ~> ls -ld /var/tmp
    drwxrwxrwt 19 root root 8192 Jan 16 10:37 /var/tmp/

    The sticky bit is set using the command chmod o+t directory. The historic origin of the "t" is in UNIX' save Text access feature.

  • SUID (set user ID) and SGID (set group ID): represented by the character s in the user or group permission field. When this mode is set on an executable file, it will run with the user and group permissions on the file instead of with those of the user issuing the command, thus giving access to system resources.

  • SGID (set group ID) on a directory: in this special case every file created in the directory will have the same group owner as the directory itself (while normal behavior would be that new files are owned by the users who create them). This way, users don't need to worry about file ownership when sharing directories:


Linking files -Linux File system



Linking files

A link is nothing more than a way of matching two or more file names to the same set of file data. There are two ways to achieve this:
  • Hard link: Associate two or more file names with the same inode. Hard links share the same data blocks on the hard disk, while they continue to behave as independent files.

    There is an immediate disadvantage: hard links can't span partitions, because inode numbers are only unique within a given partition.

  • Soft link or symbolic link (or for short: symlink): a small file that is a pointer to another file. A symbolic link contains the path to the target file instead of a physical location on the hard disk. Since inodes are not used in this system, soft links can span across partitions.



Creating symbolic links

The symbolic link is particularly interesting for beginning users: they are fairly obvious to see and you don't need to worry about partitions.

The command to make links is ln. In order to create symlinks, you need to use the -s option:

ln -s targetfile linkname

The application of symbolic links is widespread. They are often used to save disk space, to make a copy of a file in order to satisfy installation requirements of a new program that expects the file to be in another location, they are used to fix scripts that suddenly have to run in a new environment and can generally save a lot of work. A system admin may decide to move the home directories of the users to a new location, disk2 for instance, but if he wants everything to work like before, like the /etc/passwd file, with a minimum of effort he will create a symlink from /home to the new location /disk2/home.

common configuration files




bashrc The system-wide configuration file for the Bourne Again SHell. Defines functions and aliases for all users. Other shells may have their own system-wide config files, like cshrc.
crontab and the cron.* directories Configuration of tasks that need to be executed periodically - backups, updates of the system databases, cleaning of the system, rotating logs etc.
mtab Currently mounted file systems. It is advised to never edit this file.
nsswitch.conf Order in which to contact the name resolvers when a process demands resolving of a host name.
pam.d Configuration of authentication modules.
passwd Lists local users. Use the shadow utilities useradd, usermod and userdel to edit this file. Edit manually only when you really know what you are doing.
printcap Outdated but still frequently used printer configuration file. Don't edit this manually unless you really know what you are doing.
profile System wide configuration of the shell environment: variables, default properties of new files, limitation of resources etc.
rc* Directories defining active services for each run level.
resolv.conf Order in which to contact DNS servers (Domain Name Servers only).
sendmail.cf Main config file for the Sendmail server.
services Connections accepted by this machine (open ports).
sndconfig or sound Configuration of the sound card and sound events.
ssh Directory containing the config files for secure shell client and server.
sysconfig Directory containing the system configuration files: mouse, keyboard, network, desktop, system clock, power management etc. (specific to RedHat)
X11 Settings for the graphical server, X. RedHat uses XFree, which is reflected in the name of the main configuration file, XFree86Config. Also contains the general directions for the window managers available on the system, for example gdm, fvwm, twm, etc.
xinetd.* or inetd.conf Configuration files for Internet services that are run from the system's (extended) Internet services daemon (servers that don't run an independent daemon).

important files and directories

The kernel

The kernel is the heart of the system. It manages the communication between the underlying hardware and the peripherals. The kernel also makes sure that processes and daemons (server processes) are started and stopped at the exact right times. The kernel has a lot of other important tasks, so many that there is a special kernel-development mailing list on this subject only, where huge amounts of information are shared. It would lead us too far to discuss the kernel in detail. For now it suffices to know that the kernel is the most important file on the system.

The shell


A shell can best be compared with a way of talking to the computer, a language. Most users do know that other language, the point-and-click language of the desktop. But in that language the computer is leading the conversation, while the user has the passive role of picking tasks from the ones presented. It is very difficult for a programmer to include all options and possible uses of a command in the GUI-format. Thus, GUIs are almost always less capable than the command or commands that form the backend.

The shell, on the other hand, is an advanced way of communicating with the system, because it allows for two-way conversation and taking initiative. Both partners in the communication are equal, so new ideas can be tested. The shell allows the user to handle a system in a very flexible way. An additional asset is that the shell allows for task automation.

Shell types


  • sh or Bourne Shell: the original shell still used on UNIX systems and in UNIX related environments. This is the basic shell, a small program with few features. When in POSIX-compatible mode, bash will emulate this shell.

  • bash or Bourne Again SHell: the standard GNU shell, intuitive and flexible. Probably most advisable for beginning users while being at the same time a powerful tool for the advanced and professional user. On Linux, bash is the standard shell for common users. This shell is a so-called superset of the Bourne shell, a set of add-ons and plug-ins. This means that the Bourne Again SHell is compatible with the Bourne shell: commands that work in sh, also work in bash. However, the reverse is not always the case. All examples and exercises in this book use bash.

  • csh or C Shell: the syntax of this shell resembles that of the C programming language. Sometimes asked for by programmers.

  • tcsh or Turbo C Shell: a superset of the common C Shell, enhancing user-friendliness and speed.

  • ksh or the Korn shell: sometimes appreciated by people with a UNIX background. A superset of the Bourne shell; with standard configuration a nightmare for beginning users.

echo $shell will give the shell on which we r working!


Linux file system layout

Subdirectories of the root directory
DirectoryContent
/binCommon programs, shared by the system, the system administrator and the users.
/bootThe startup files and the kernel, vmlinuz. In some recent distributions also grub data. Grub is the GRand Unified Boot loader and is an attempt to get rid of the many different boot-loaders we know today.
/devContains references to all the CPU peripheral hardware, which are represented as files with special properties.
/etcMost important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows
/homeHome directories of the common users.
/initrd(on some distributions) Information for booting. Do not remove!
/libLibrary files, includes files for all kinds of programs needed by the system and the users.
/lost+foundEvery partition has a lost+found in its upper directory. Files that were saved during failures are here.
/miscFor miscellaneous purposes.
/mntStandard mount point for external file systems, e.g. a CD-ROM or a digital camera.
/netStandard mount point for entire remote file systems
/optTypically contains extra and third party software.
/procA virtual file system containing information about system resources. More information about the meaning of the files in proc is obtained by entering the command man proc in a terminal window. The file proc.txt discusses the virtual file system in detail.
/rootThe administrative user's home directory. Mind the difference between /, the root directory and /root, the home directory of the root user.
/sbinPrograms for use by the system and the system administrator.
/tmpTemporary space for use by the system, cleaned upon reboot, so don't use this for saving any work!
/usrPrograms, libraries, documentation etc. for all user-related programs.
/varStorage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.

How can you find out which partition a directory is on? Using the df command with a dot (.) as an option shows the partition the current directory belongs to, and informs about the amount of space used on this partition:



icts@09cpu0161:~$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda5 67282996 12299376 51565796 20% /
tmpfs 1025328 0 1025328 0% /lib/init/rw
varrun 1025328 104 1025224 1% /var/run
varlock 1025328 0 1025328 0% /var/lock
udev 1025328 252 1025076 1% /dev
tmpfs 1025328 92 1025236 1% /dev/shm
/dev/sda3 53263504 67636 53195868 1% /media/D-Drive


Every partition has its own file system. By imagining all those file systems together, we can form an idea of the tree-structure of the entire system, but it is not as simple as that. In a file system, a file is represented by an inode, a kind of serial number containing information about the actual data that makes up the file: to whom this file belongs, and where is it located on the hard disk.

Every partition has its own set of inodes; throughout a system with multiple partitions, files with the same inode number can exist.

Each inode describes a data structure on the hard disk, storing the properties of a file, including the physical location of the file data. When a hard disk is initialized to accept data storage, usually during the initial system installation process or when adding extra disks to an existing system, a fixed number of inodes per partition is created. This number will be the maximum amount of files, of all types (including directories, special files, links etc.) that can exist at the same time on the partition. We typically count on having 1 inode per 2 to 8 kilobytes of storage.

At the time a new file is created, it gets a free inode. In that inode is the following information:

  • Owner and group owner of the file.

  • File type (regular, directory, ...)

  • Permissions on the file

  • Date and time of creation, last read and change.

  • Date and time this information has been changed in the inode.

  • Number of links to this file

  • File size

  • An address defining the actual location of the file data.

The only information not included in an inode, is the file name and directory. These are stored in the special directory files. By comparing file names and inode numbers, the system can make up a tree-structure that the user understands. Users can display inode numbers using the -i option to ls. The inodes have their own separate space on the disk.


icts@09cpu0161:/$ ls -i
3432449 bin 843777 lib64 2293761 srv
1540097 boot 11 lost+found 1 sys
12 cdrom 3956737 media 2367489 tmp
1268 dev 1105921 mnt 3284993 usr
2588673 etc 1114113 opt 1335297 var
3416065 home 1 proc 16 vmlinuz
15 initrd.img 1736705 root 13 vmlinuz.old
14 initrd.img.old 376833 sbin
3457025 lib 1843201 selinux

the Linux file system

Sorts of files

Most files are just files, called regular files; they contain normal data, for example text files, executable files or programs, input for or output from a program and so on.

While it is reasonably safe to suppose that everything you encounter on a Linux system is a file, there are some exceptions.

  • Directories: files that are lists of other files.

  • Special files: the mechanism used for input and output. Most special files are in /dev, we will discuss them later.

  • Links: a system to make a file or directory visible in multiple parts of the system's file tree. We will talk about links in detail.

  • (Domain) sockets: a special file type, similar to TCP/IP sockets, providing inter-process networking protected by the file system's access control.

  • Named pipes: act more or less like sockets and form a way for processes to communicate with each other, without using network socket semantics.

File types in a long list
SymbolMeaning
-Regular file
dDirectory
lLink
cSpecial file
sSocket
pNamed pipe
bBlock device

Linux family tree


Linux family tree

Monday, July 20, 2009

Compiled and build Blender 2.5

Sucessfully compiled and bulid Blender2.5