Sunday, 29 June 2014

UNIX INTERVIEW QUESTIONS



UNIX INTERVIEW QUESTIONS


I’ve put together a document here to use to evaluate someone’s UNIX knowledge. You can freely use and distribute this document providing it is in accordance with the TFS © Policy on our main page.


Level I

How can you tell what shell you are running on a UNIX system?

Answer :
You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID.
What are conditions on which deadlock can occur while swapping the processes?
Answer :
All processes in the main memory are asleep. All ‘ready-to-run’ processes are swapped out.
There is no space in the swap device for the new incoming process that are swapped out of the main memory. There is no space in the main memory for the new incoming process.
How do you change File Access Permissions?
Answer :
Every file has following attributes:
owner’s user ID ( 16 bit integer )
owner’s group ID ( 16 bit integer )
File access mode word
‘r w x -r w x- r w x’
(user permission-group permission-others permission)
r-read, w-write, x-execute
To change the access mode, we use chmod(filename,mode).
Example:
To change mode of myfile to ‘rw-rw-r–’ (ie. read, write permission for user - read,write permission for group - only read permission for others)  we give the args as:
chmod(myfile,0664) .
Each operation is represented by discrete values
‘r’ is 4
‘w’ is 2
‘x’ is 1
Therefore, for ‘rw’ the value is 6(4+2).
Example 2:
To change mode of myfile to ‘rwxr–r–’ we give the args as:
chmod(myfile,0744).


List the system calls used for process management.
Answer :
System calls        Description
fork()                To create a new process
exec()                To execute a new program in a process
wait()                To wait until a created process completes its execution
exit()                To exit from a process execution
getpid()            To get a process identifier of the current process
getppid()            To get parent process identifier
nice()                To bias the existing priority of a process
brk()                To increase/decrease the data segment size of a process


What is the difference between Swapping and Paging?

Answer :

Swapping:
Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.

Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory.

It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.

What is the difference between cmp and diff commands?
Answer :

cmp - Compares two files byte by byte and displays the first mismatch
diff - tells the changes to be made to make the files identical


What is meant by the nice value?
Answer :

Nice value is the value that controls {increments or decrements} the priority of the process. This value that is returned by the nice () system call. The equation for using nice value is:
Priority = (“recent CPU usage”/constant) + (base- priority) + (nice value)
Only the administrator can supply the nice value. The nice () system call works for the running process only. Nice value of one process cannot affect the nice value of the other process.




What is a daemon?
Answer :
A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for requests and responding to them. It can also be defined as the background process that does not belong to a terminal session. Many system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the NNTP daemon, which handles USENET news. Many other daemons may exist. Some of the most common daemons are:
init: Takes over the basic running of the system when the kernel has finished the boot process.
inetd: Responsible for starting network services that do not have their own stand-alone daemons. For example, inetd usually takes care of incoming rlogin, telnet, and ftp connections.
cron: Responsible for running repetitive tasks on a regular schedule.

What are the process states in UNIX?
Answer :
As a process executes it changes state according to its circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a signal.
Zombie : The process is dead but have not been removed from the process table.


How are devices represented in UNIX?
All devices are represented by files called special files that are located in/dev directory. Thus, device files and other files are named and accessed in the same way. A 'regular file' is just an ordinary data file in the disk. A 'block special file' represents a device with characteristics similar to a disk (data transfer in terms of blocks). A 'character special file' represents a device with characteristics similar to a keyboard (data transfer is by stream of bits in sequential order).
What is 'inode'?
All UNIX files have its description stored in a structure called 'inode'. The inode contains info about the file-size, its location, time of last access, time of last modification, permission and so on. Directories are also represented as files and have an associated inode. In addition to descriptions about the file, the inode contains pointers to the data blocks of the file. If the file is large, inode has indirect pointer to a block of pointers to additional data blocks (this further aggregates for larger files). A block is typically 8k.
Inode consists of the following fields:
File owner identifier
File type
File access permissions
File access times
Number of links
File size
Location of the file data
Brief about the directory representation in UNIX
A Unix directory is a file containing a correspondence between filenames and inodes. A directory is a special file that the kernel maintains. Only kernel modifies directories, but processes can read directories. The contents of a directory are a list of filename and inode number pairs. When new directories are created, kernel makes two entries named '.' (refers to the directory itself) and '..' (refers to parent directory).
System call for creating directory is mkdir (pathname, mode).
What are the Unix system calls for I/O?
open(pathname,flag,mode) - open file
creat(pathname,mode) - create file
close(filedes) - close an open file
read(filedes,buffer,bytes) - read data from an open file
write(filedes,buffer,bytes) - write data to an open file
lseek(filedes,offset,from) - position an open file
dup(filedes) - duplicate an existing file descriptor
dup2(oldfd,newfd) - duplicate to a desired file descriptor
fcntl(filedes,cmd,arg) - change properties of an open file
ioctl(filedes,request,arg) - change the behaviour of an open file
The difference between fcntl anf ioctl is that the former is intended for any open file, while the latter is for device-specific operations.
How do you change File Access Permissions?
Every file has following attributes:
owner's user ID ( 16 bit integer )
owner's group ID ( 16 bit integer )
File access mode word
'r w x -r w x- r w x'
(user permission-group permission-others permission)
r-read, w-write, x-execute
To change the access mode, we use chmod(filename,mode).
Example 1:
To change mode of myfile to 'rw-rw-r--' (ie. read, write permission for user - read,write permission for group - only read permission for others) we give the args as:
chmod(myfile,0664) .
Each operation is represented by discrete values
'r' is 4
'w' is 2
'x' is 1
Therefore, for 'rw' the value is 6(4+2).
Example 2:
To change mode of myfile to 'rwxr--r--' we give the args as:
chmod(myfile,0744).
What are links and symbolic links in UNIX file system?
A link is a second name (not a file) for a file. Links can be used to assign more than one name to a file, but cannot be used to assign a directory more than one name or link filenames on different computers.
Symbolic link 'is' a file that only contains the name of another file.Operation on the symbolic link is directed to the file pointed by the it.Both the limitations of links are eliminated in symbolic links.
Commands for linking files are:
Link ln filename1 filename2
Symbolic link ln -s filename1 filename2
What is a FIFO?
FIFO are otherwise called as 'named pipes'. FIFO (first-in-first-out) is a special file which is said to be data transient. Once data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. It is used in interprocess communication where a process writes to one end of the pipe (producer) and the other reads from the other end (consumer).
How do you create special files like named pipes and device files?
The system call mknod creates special files in the following sequence.
1. kernel assigns new inode,
2. sets the file type to indicate that the file is a pipe, directory or special file,
3. If it is a device file, it makes the other entries like major, minor device numbers.
For example:
If the device is a disk, major device number refers to the disk controller and minor device number is the disk.
Discuss the mount and unmount system calls
The privileged mount system call is used to attach a file system to a directory of another file system; the unmount system call detaches a file system. When you mount another file system on to your directory, you are essentially splicing one directory tree onto a branch in another directory tree. The first argument to mount call is the mount point, that is , a directory in the current file naming system. The second argument is the file system to mount to that point. When you insert a cdrom to your unix system's drive, the file system in the cdrom automatically mounts to /dev/cdrom in your system.
How does the inode map to data block of a file?
Inode has 13 block addresses. The first 10 are direct block addresses of the first 10 data blocks in the file. The 11th address points to a one-level index block. The 12th address points to a two-level (double in-direction) index block. The 13th address points to a three-level(triple in-direction)index block. This provides a very large maximum file size with efficient access to large files, but also small files are accessed directly in one disk read.
What is a shell?
A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface. The shell converts them to system calls to the OS or forks off a process to execute the command. System call results and other information from the OS are presented to the user through an interactive interface. Commonly used shells are sh,csh,ksh etc.
Brief about the initial process sequence while the system boots up.
While booting, special process called the 'swapper' or 'scheduler' is created with Process-ID 0. The swapper manages memory allocation for processes and influences CPU allocation. The swapper inturn creates 3 children:
the process dispatcher,
vhand and
dbflush
with IDs 1,2 and 3 respectively.
This is done by executing the file /etc/init. Process dispatcher gives birth to the shell. Unix keeps track of all the processes in an internal data structure called the Process Table (listing command is ps -el).
What are various IDs associated with a process?
Unix identifies each process with a unique integer called ProcessID. The process that executes the request for creation of a process is called the 'parent process' whose PID is 'Parent Process ID'. Every process is associated with a particular user called the 'owner' who has privileges over the process. The identification for the user is 'UserID'. Owner is the user who executes the process. Process also has 'Effective User ID' which determines the access privileges for accessing resources like files.
getpid() -process id
getppid() -parent process id
getuid() -user id
geteuid() -effective user id
Explain fork() system call.
The `fork()' used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. We can tell which is which by checking the return value from `fork()'. The parent gets the child's pid returned to him, but the child gets 0 returned to him.
Predict the output of the following program code
main()
{
fork();
printf("Hello World!");
}
Answer:
Hello World!Hello World!
Explanation:
The fork creates a child that is a duplicate of the parent process. The child begins from the fork().All the statements after the call to fork() will be executed twice.(once by the parent process and other by child). The statement before fork() is executed only by the parent process.
Predict the output of the following program code
main()
{
fork(); fork(); fork();
printf("Hello World!");
}

Answer:
"Hello World" will be printed 8 times.
Explanation:
2^n times where n is the number of calls to fork()
List the system calls used for process management:
System calls Description
fork() To create a new process
exec() To execute a new program in a process
wait() To wait until a created process completes its execution
exit() To exit from a process execution
getpid() To get a process identifier of the current process
getppid() To get parent process identifier
nice() To bias the existing priority of a process
brk() To increase/decrease the data segment size of a process
How can you get/set an environment variable from a program?:
Getting the value of an environment variable is done by using `getenv()'. Setting the value of an environment variable is done by using `putenv()'.
How can a parent and child process communicate?
A parent and child can communicate through any of the normal inter-process communication schemes (pipes, sockets, message queues, shared memory), but also have some special ways to communicate that take advantage of their relationship as a parent and child. One of the most obvious is that the parent can get the exit status of the child.
What is a zombie?
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls `wait()'; In the interval between the child terminating and the parent calling `wait()', the child is said to be a `zombie' (If you do `ps', the child will have a `Z' in its status field to indicate this.)
What are the process states in Unix?
As a process executes it changes state according to its circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a signal.
Zombie : The process is dead but have not been removed from the process table.
What Happens when you execute a program?
When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system. Each process has process context, which is everything that is unique about the state of the program you are currently running. Every time you execute a program the UNIX system does a fork, which performs a series of operations to create a process context and then execute your program in that context. The steps include the following:
Allocate a slot in the process table, a list of currently running programs kept by UNIX.
Assign a unique process identifier (PID) to the process.
iCopy the context of the parent, the process that requested the spawning of the new process.
Return the new PID to the parent process. This enables the parent process to examine or control the process directly. After the fork is complete, UNIX runs your program.
What Happens when you execute a command?
When you enter 'ls' command to look at the contents of your current working directory, UNIX does a series of things to create an environment for ls and the run it: The shell has UNIX perform a fork. This creates a new process that the shell will use to run the ls program. The shell has UNIX perform an exec of the ls program. This replaces the shell program and data with the program and data for ls and then starts running that new program. The ls program is loaded into the new process context, replacing the text and data of the shell. The ls program performs its task, listing the contents of the current directory.
What is a Daemon?
A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for requests and responding to them. It can also be defined as the background process that does not belong to a terminal session. Many system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the NNTP daemon, which handles USENET news. Many other daemons may exist. Some of the most common daemons are:
init: Takes over the basic running of the system when the kernel has finished the boot process.
inetd: Responsible for starting network services that do not have their own stand-alone daemons. For example, inetd usually takes care of incoming rlogin, telnet, and ftp connections.
cron: Responsible for running repetitive tasks on a regular schedule.
What is 'ps' command for?
The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.
How would you kill a process?
The kill command takes the PID as one argument; this identifies which process to terminate. The PID of a process can be got using 'ps' command.
What is an advantage of executing a process in background?
The most common reason to put a process in the background is to allow you to do something else interactively without waiting for the process to complete. At the end of the command you add the special background symbol, &. This symbol tells your shell to execute the given command in the background.
Example: cp *.* ../backup& (cp is for copy)
How do you execute one program from within another?
The system calls used for low-level process creation are execlp() and execvp(). The execlp call overlays the existing program with the new one , runs that and exits. The original program gets back control only when an error occurs. execlp(path,file_name,arguments..); //last argument must be NULL A variant of execlp called execvp is used when the number of arguments is not known in advance. execvp(path,argument_array); //argument array should be terminated by NULL
What is IPC? What are the various schemes available?
The term IPC (Inter-Process Communication) describes various ways by which different process running on some operating system communicate between each other. Various schemes available are as follows: Pipes:
One-way communication scheme through which different process can communicate. The problem is that the two processes should have a common ancestor (parent-child relationship). However this problem was fixed with the introduction of named-pipes (FIFO).
Message Queues :
Message queues can be used between related and unrelated processes running on a machine.
Shared Memory:
This is the fastest of all IPC schemes. The memory to be shared is mapped into the address space of the processes (that are sharing). The speed achieved is attributed to the fact that there is no kernel involvement. But this scheme needs synchronization.
Various forms of synchronisation are mutexes, condition-variables, read-write locks, record-locks, and semaphores.
What is the difference between Swapping and Paging?
Swapping: Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory.
It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.
What is major difference between the Historic Unix and the new BSD release of Unix System V in terms of Memory Management?
Historic Unix uses Swapping – entire process is transferred to the main memory from the swap device, whereas the Unix System V uses Demand Paging – only the part of the process is moved to the main memory. Historic Unix uses one Swap Device and Unix System V allow multiple Swap Devices.
What is the main goal of the Memory Management?
It decides which process should reside in the main memory, Manages the parts of the virtual address space of a process which is non-core resident, Monitors the available main memory and periodically write the processes into the swap device to provide more processes fit in the main memory simultaneously.
What is a Map?
A Map is an Array, which contains the addresses of the free space in the swap device that are allocatable resources, and the number of the resource units available there.
This allows First-Fit allocation of contiguous blocks of a resource. Initially the Map contains one entry – address (block offset from the starting of the swap area) and the total number of resources. Kernel treats each unit of Map as a group of disk blocks. On the allocation and freeing of the resources Kernel updates the Map for accurate information.
What scheme does the Kernel in Unix System V follow while choosing a swap device among the multiple swap devices?
Kernel follows Round Robin scheme choosing a swap device among the multiple swap devices in Unix System V.
What is a Region?
A Region is a continuous area of a process’s address space (such as text, data and stack). The kernel in a ‘Region Table’ that is local to the process maintains region. Regions are sharable among the process.
What are the events done by the Kernel after a process is being swapped out from the main memory?
When Kernel swaps the process out of the primary memory, it performs the following:
Kernel decrements the Reference Count of each region of the process. If the reference count becomes zero, swaps the region out of the main memory,
Kernel allocates the space for the swapping process in the swap device,
Kernel locks the other swapping process while the current swapping operation is going on,
The Kernel saves the swap address of the region in the region table.
Is the Process before and after the swap are the same? Give reason.
Process before swapping is residing in the primary memory in its original form. The regions (text, data and stack) may not be occupied fully by the process, there may be few empty slots in any of the regions and while swapping Kernel do not bother about the empty slots while swapping the process out. After swapping the process resides in the swap (secondary memory) device. The regions swapped out will be present but only the occupied region slots but not the empty slots that were present before assigning. While swapping the process once again into the main memory, the Kernel referring to the Process Memory Map, it assigns the main memory accordingly taking care of the empty slots in the regions.
What do you mean by u-area (user area) or u-block?
This contains the private data that is manipulated only by the Kernel. This is local to the Process, i.e. each process is allocated a u-area.
What are the entities that are swapped out of the main memory while swapping the process out of the main memory?
All memory space occupied by the process, process’s u-area, and Kernel stack are swapped out, theoretically. Practically, if the process’s u-area contains the Address Translation Tables for the process then Kernel implementations do not swap the u-area.
What is Fork swap?
fork() is a system call to create a child process. When the parent process calls fork() system call, the child process is created and if there is short of memory then the child process is sent to the read-to-run state in the swap device, and return to the user state without swapping the parent process. When the memory will be available the child process will be swapped into the main memory.
What is Expansion swap?
At the time when any process requires more memory than it is currently allocated, the Kernel performs Expansion swap. To do this Kernel reserves enough space in the swap device. Then the address translation mapping is adjusted for the new virtual address space but the physical memory is not allocated. At last Kernel swaps the process into the assigned space in the swap device. Later when the Kernel swaps the process into the main memory this assigns memory according to the new address translation mapping.
How the Swapper works?
The swapper is the only process that swaps the processes. The Swapper operates only in the Kernel mode and it does not uses System calls instead it uses internal Kernel functions for swapping. It is the archetype of all kernel process.
What are the processes that are not bothered by the swapper? Give Reason.
Zombie process: They do not take any up physical memory.
Processes locked in memories that are updating the region of the process.
Kernel swaps only the sleeping processes rather than the ‘ready-to-run’ processes, as they have the higher probability of being scheduled than the Sleeping processes.
What are the requirements for a swapper to work?
The swapper works on the highest scheduling priority. Firstly it will look for any sleeping process, if not found then it will look for the ready-to-run process for swapping. But the major requirement for the swapper to work the ready-to-run process must be core-resident for at least 2 seconds before swapping out. And for swapping in the process must have been resided in the swap device for at least 2 seconds. If the requirement is not satisfied then the swapper will go into the wait state on that event and it is awaken once in a second by the Kernel.
What are the criteria for choosing a process for swapping into memory from the swap device?
The resident time of the processes in the swap device, the priority of the processes and the amount of time the processes had been swapped out.
What are the criteria for choosing a process for swapping out of the memory to the swap device?
The process’s memory resident time,
Priority of the process and
The nice value.
What do you mean by nice value?
Nice value is the value that controls {increments or decrements} the priority of the process. This value that is returned by the nice () system call. The equation for using nice value is: Priority = (“recent CPU usage”/constant) + (base- priority) + (nice value) Only the administrator can supply the nice value. The nice () system call works for the running process only. Nice value of one process cannot affect the nice value of the other process.
What are conditions on which deadlock can occur while swapping the processes?
All processes in the main memory are asleep.
All ‘ready-to-run’ processes are swapped out.
There is no space in the swap device for the new incoming process that are swapped out of the main memory.
There is no space in the main memory for the new incoming process.
What are conditions for a machine to support Demand Paging?
Memory architecture must based on Pages,
The machine must support the ‘restartable’ instructions.
What is ‘the principle of locality’?
It’s the nature of the processes that they refer only to the small subset of the total data space of the process. i.e. the process frequently calls the same subroutines or executes the loop instructions.
What is the working set of a process?
The set of pages that are referred by the process in the last ‘n’, references, where ‘n’ is called the window of the working set of the process.
What is the window of the working set of a process?
The window of the working set of a process is the total number in which the process had referred the set of pages in the working set of the process.
What is called a page fault?
Page fault is referred to the situation when the process addresses a page in the working set of the process but the process fails to locate the page in the working set. And on a page fault the kernel updates the working set by reading the page from the secondary device.
What are data structures that are used for Demand Paging?
Kernel contains 4 data structures for Demand paging. They are,
Page table entries,
Disk block descriptors,
Page frame data table (pfdata),
Swap-use table.
What are the bits that support the demand paging?
Valid, Reference, Modify, Copy on write, Age. These bits are the part of the page table entry, which includes physical address of the page and protection bits.
Page address
Age
Copy on write
Modify
Reference
Valid
Protection
How the Kernel handles the fork() system call in traditional Unix and in the System V Unix, while swapping?
Kernel in traditional Unix, makes the duplicate copy of the parent’s address space and attaches it to the child’s process, while swapping. Kernel in System V Unix, manipulates the region tables, page table, and pfdata table entries, by incrementing the reference count of the region table of shared regions.
Difference between the fork() and vfork() system call?
During the fork() system call the Kernel makes a copy of the parent process’s address space and attaches it to the child process. But the vfork() system call do not makes any copy of the parent’s address space, so it is faster than the fork() system call. The child process as a result of the vfork() system call executes exec() system call. The child process from vfork() system call executes in the parent’s address space (this can overwrite the parent’s data and stack ) which suspends the parent process until the child process exits.
What is BSS(Block Started by Symbol)?
A data representation at the machine level, that has initial values when a program starts and tells about how much space the kernel allocates for the un-initialized data. Kernel initializes it to zero at run-time.
What is Page-Stealer process?
This is the Kernel process that makes rooms for the incoming pages, by swapping the memory pages that are not the part of the working set of a process. Page-Stealer is created by the Kernel at the system initialization and invokes it throughout the lifetime of the system. Kernel locks a region when a process faults on a page in the region, so that page stealer cannot steal the page, which is being faulted in.
Name two paging states for a page in memory?
The two paging states are:
The page is aging and is not yet eligible for swapping,
The page is eligible for swapping but not yet eligible for reassignment to other virtual address space.
What are the phases of swapping a page from the memory?
Page stealer finds the page eligible for swapping and places the page number in the list of pages to be swapped. Kernel copies the page to a swap device when necessary and clears the valid bit in the page table entry, decrements the pfdata reference count, and places the pfdata table entry at the end of the free list if its reference count is 0.
What is page fault? Its types?
Page fault refers to the situation of not having a page in the main memory when any process references it. There are two types of page fault :
Validity fault,
Protection fault.
In what way the Fault Handlers and the Interrupt handlers are different?
Fault handlers are also an interrupt handler with an exception that the interrupt handlers cannot sleep. Fault handlers sleep in the context of the process that caused the memory fault. The fault refers to the running process and no arbitrary processes are put to sleep.
What is validity fault?
If a process referring a page in the main memory whose valid bit is not set, it results in validity fault. The valid bit is not set for those pages:
that are outside the virtual address space of a process,
that are the part of the virtual address space of the process but no physical address is assigned to it.
What does the swapping system do if it identifies the illegal page for swapping?
If the disk block descriptor does not contain any record of the faulted page, then this causes the attempted memory reference is invalid and the kernel sends a “Segmentation violation” signal to the offending process. This happens when the swapping system identifies any invalid memory reference.
What are states that the page can be in, after causing a page fault?
On a swap device and not in memory,
On the free page list in the main memory,
In an executable file,
Marked “demand zero”,
Marked “demand fill”.
In what way the validity fault handler concludes?
It sets the valid bit of the page by clearing the modify bit.
It recalculates the process priority.
At what mode the fault handler executes?
At the Kernel Mode.
What do you mean by the protection fault?
Protection fault refers to the process accessing the pages, which do not have the access permission. A process also incur the protection fault when it attempts to write a page whose copy on write bit was set during the fork() system call.
How the Kernel handles the copy on write bit of a page, when the bit is set?
In situations like, where the copy on write bit of a page is set and that page is shared by more than one process, the Kernel allocates new page and copies the content to the new page and the other processes retain their references to the old page. After copying the Kernel updates the page table entry with the new page number. Then Kernel decrements the reference count of the old pfdata table entry. In cases like, where the copy on write bit is set and no processes are sharing the page, the Kernel allows the physical page to be reused by the processes. By doing so, it clears the copy on write bit and disassociates the page from its disk copy (if one exists), because other process may share the disk copy. Then it removes the pfdata table entry from the page-queue as the new copy of the virtual page is not on the swap device. It decrements the swap-use count for the page and if count drops to 0, frees the swap space.
For which kind of fault the page is checked first?
The page is first checked for the validity fault, as soon as it is found that the page is invalid (valid bit is clear), the validity fault handler returns immediately, and the process incur the validity page fault. Kernel handles the validity fault and the process will incur the protection fault if any one is present.
In what way the protection fault handler concludes?
After finishing the execution of the fault handler, it sets the modify and protection bits and clears the copy on write bit. It recalculates the process-priority and checks for signals.
How the Kernel handles both the page stealer and the fault handler?
The page stealer and the fault handler thrash because of the shortage of the memory. If the sum of the working sets of all processes is greater that the physical memory then the fault handler will usually sleep because it cannot allocate pages for a process. This results in the reduction of the system throughput because Kernel spends too much time in overhead, rearranging the memory in the frantic pace.
Explain different types of Unix systems.
The most widely used are: 1. System V (AT&T) 2. AIX (IBM) 3. BSD (Berkeley) 4. Solaris (Sun) 5. Xenix ( A PC version of Unix)
Explain kernal and shell.
Kernal: It carries out basic operating system functions such as allocating memory, accessing files and handling communications. Shell:A shell provides the user interface to the kernal.There are 3 major shells : C-shell, Bourne shell , Korn shell
What is ex and vi ?
ex is Unix line editor and vi is the standard Unix screen editor.
Which are typical system directories below the root directory?
(1)/bin: contains many programs which will be executed by users (2)/etc : files used by administrator (3)/dev: hardware devices (4)/lib: system libraries (5)/usr: application software (6)/home: home directories for different systems.
Construct pipes to execute the following jobs.
1. Output of who should be displayed on the screen with value of total number of users who have logged in displayed at the bottom of the list.
2. Output of ls should be displayed on the screen and from this output the lines containing the word ‘poem’ should be counted and the count should be stored in a file.
3. Contents of file1 and file2 should be displayed on the screen and this output should be appended in a file
.
From output of ls the lines containing ‘poem’ should be displayed on the screen along with the count.
4. Name of cities should be accepted from the keyboard . This list should be combined with the list present in a file. This combined list should be sorted and the sorted list
should be stored in a file ‘newcity’.
5. All files present in a directory dir1 should be deleted any error while deleting should be stored in a file ‘errorlog’.
Explain the following commands.
$ ls > file1
$ banner hi-fi > message
$ cat par.3 par.4 par.5 >> report
$ cat file1>file1
$ date ; who
$ date ; who > logfile
$ (date ; who) > logfile
What is the significance of the “tee” command?
It reads the standard input and sends it to the standard output while redirecting a copy of what it has read to the file specified by the user.
What does the command “ $who | sort –logfile > newfile” do?
The input from a pipe can be combined with the input from a file . The trick is to use the special symbol “-“ (a hyphen) for those commands that recognize the hyphen as std input.
In the above command the output from who becomes the std input to sort , meanwhile sort opens the file logfile, the contents of this file is sorted together with the output of who (rep by the hyphen) and the sorted output is redirected to the file newfile.
What does the command “$ls | wc –l > file1” do?
ls becomes the input to wc which counts the number of lines it receives as input and instead of displaying this count , the value is stored in file1.
Which of the following commands is not a filter man , (b) cat , (c) pg , (d) head
man A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result to the std output.
How is the command “$cat file2 “ different from “$cat >file2 and >> redirection operators ?
is the output redirection operator when used it overwrites while >> operator appends into the file.
Explain the steps that a shell follows while processing a command.
After the command line is terminated by the key, the shell goes ahead with processing the command line in one or more passes. The sequence is well defined and assumes the following order.
Parsing: The shell first breaks up the command line into words, using spaces and the delimiters, unless quoted. All consecutive occurrences of a space or tab are replaced here with a single space.
Variable evaluation: All words preceded by a $ are valuated as variables, unless quoted or escaped.
Command substitution: Any command surrounded by back quotes is executed by the shell which then replaces the standard output of the command into the command line.
Wild-card interpretation: The shell finally scans the command line for wild-cards (the characters *, ?, [, ]).
Any word containing a wild-card is replaced by a sorted list of
filenames that match the pattern. The list of these filenames then forms the arguments to the command.
PATH evaluation: It finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command.
What difference between cmp and diff commands?
cmp - Compares two files byte by byte and displays the first mismatch diff - tells the changes to be made to make the files identical
  1. What are the main differences between Apache 1.x and 2.x?
  2. What does the “route” command do?
  3. What are the read/write/execute bits on a directory mean?
  4. What does iostat do?
  5. what does vmstat do?
  6. What does netstat do?
  7. What is the most graceful way to bring a system into single user mode?
  8. How do you determine disk usage?
  9. What is AWK?
  10. What is SED?
  11. What is the difference between binaries in /bin, and /usr/bin?
  12. What is a dynamically linked file?
  13. What is a statically linked file?
How are devices represented in UNIX?
All devices are represented by files called special files that are located in/dev directory. Thus, device files and other files are named and accessed in the same way. A 'regular file' is just an ordinary data file in the disk. A 'block special file' represents a device with characteristics similar to a disk (data transfer in terms of blocks). A 'character special file' represents a device with characteristics similar to a keyboard (data transfer is by stream of bits in sequential order).
2. What is 'inode'?
All UNIX files have its description stored in a structure called 'inode'. The inode contains info about the file-size, its location, time of last access, time of last modification, permission and so on. Directories are also represented as files and have an associated inode. In addition to descriptions about the file, the inode contains pointers to the data blocks of the file. If the file is large, inode has indirect pointer to a block of pointers to additional data blocks (this further aggregates for larger files). A block is typically 8k.
Inode consists of the following fields:
  • File owner identifier
  • File type
  • File access permissions
  • File access times
  • Number of links
  • File size
  • Location of the file data
3. Brief about the directory representation in UNIX
A Unix directory is a file containing a correspondence between filenames and inodes. A directory is a special file that the kernel maintains. Only kernel modifies directories, but processes can read directories. The contents of a directory are a list of filename and inode number pairs. When new directories are created, kernel makes two entries named '.' (refers to the directory itself) and '..' (refers to parent directory).
System call for creating directory is mkdir (pathname, mode).
4. What are the Unix system calls for I/O?
  • open(pathname,flag,mode) - open file
  • creat(pathname,mode) - create file
  • close(filedes) - close an open file
  • read(filedes,buffer,bytes) - read data from an open file
  • write(filedes,buffer,bytes) - write data to an open file
  • lseek(filedes,offset,from) - position an open file
  • dup(filedes) - duplicate an existing file descriptor
  • dup2(oldfd,newfd) - duplicate to a desired file descriptor
  • fcntl(filedes,cmd,arg) - change properties of an open file
  • ioctl(filedes,request,arg) - change the behaviour of an open file
The difference between fcntl anf ioctl is that the former is intended for any open file, while the latter is for device-specific operations.
5. How do you change File Access Permissions?
Every file has following attributes:
owner's user ID ( 16 bit integer )
owner's group ID ( 16 bit integer )
File access mode word
'r w x -r w x- r w x'

(user permission-group permission-others permission)
r-read, w-write, x-execute
To change the access mode, we use chmod(filename,mode).
Example 1:
To change mode of myfile to 'rw-rw-r–' (ie. read, write permission for user - read,write permission for group - only read permission for others) we give the args as:
chmod(myfile,0664) .
Each operation is represented by discrete values
'r' is 4
'w' is 2
'x' is 1

Therefore, for 'rw' the value is 6(4+2).
Example 2:
To change mode of myfile to 'rwxr–r–' we give the args as:
chmod(myfile,0744).
 
6. What are links and symbolic links in UNIX file system?
A link is a second name (not a file) for a file. Links can be used to assign more than one name to a file, but cannot be used to assign a directory more than one name or link filenames on different computers.
Symbolic link 'is' a file that only contains the name of another file.Operation on the symbolic link is directed to the file pointed by the it.Both the limitations of links are eliminated in symbolic links.
Commands for linking files are:
Link ln filename1 filename2
Symbolic link ln -s filename1 filename2

7. What is a FIFO?
FIFO are otherwise called as 'named pipes'. FIFO (first-in-first-out) is a special file which is said to be data transient. Once data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. It is used in interprocess communication where a process writes to one end of the pipe (producer) and the other reads from the other end (consumer).
8. How do you create special files like named pipes and device files?
The system call mknod creates special files in the following sequence.
1. kernel assigns new inode,
2. sets the file type to indicate that the file is a pipe, directory or special file,
3. If it is a device file, it makes the other entries like major, minor device numbers.
For example:
If the device is a disk, major device number refers to the disk controller and minor device number is the disk.
9. Discuss the mount and unmount system calls
The privileged mount system call is used to attach a file system to a directory of another file system; the unmount system call detaches a file system. When you mount another file system on to your directory, you are essentially splicing one directory tree onto a branch in another directory tree. The first argument to mount call is the mount point, that is , a directory in the current file naming system. The second argument is the file system to mount to that point. When you insert a cdrom to your unix system's drive, the file system in the cdrom automatically mounts to /dev/cdrom in your system.
10. How does the inode map to data block of a file?
Inode has 13 block addresses. The first 10 are direct block addresses of the first 10 data blocks in the file. The 11th address points to a one-level index block. The 12th address points to a two-level (double in-direction) index block. The 13th address points to a three-level(triple in-direction)index block. This provides a very large maximum file size with efficient access to large files, but also small files are accessed directly in one disk read.
11. What is a shell?
A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface. The shell converts them to system calls to the OS or forks off a process to execute the command. System call results and other information from the OS are presented to the user through an interactive interface. Commonly used shells are sh,csh,ks etc.
12. Brief about the initial process sequence while the system boots up.
While booting, special process called the 'swapper' or 'scheduler' is created with Process-ID 0. The swapper manages memory allocation for processes and influences CPU allocation. The swapper inturn creates 3 children:
  • the process dispatcher,
  • vhand and
  • dbflush
with IDs 1,2 and 3 respectively.
This is done by executing the file /etc/init. Process dispatcher gives birth to the shell. Unix keeps track of all the processes in an internal data structure called the Process Table (listing command is ps -el).
13. What are various IDs associated with a process?
Unix identifies each process with a unique integer called ProcessID. The process that executes the request for creation of a process is called the 'parent process' whose PID is 'Parent Process ID'. Every process is associated with a particular user called the 'owner' who has privileges over the process. The identification for the user is 'UserID'. Owner is the user who executes the process. Process also has 'Effective User ID' which determines the access privileges for accessing resources like files.
  • getpid() -process id
  • getppid() -parent process id
  • getuid() -user id
  • geteuid() -effective user id
14. Explain fork() system call.
The `fork()' used to create a new process from an existing process. The new process is called the child process, and the existing process is called the parent. We can tell which is which by checking the return value from `fork()'. The parent gets the child's pid returned to him, but the child gets 0 returned to him.
15. Predict the output of the following program code
main()
{
  fork();
  printf("Hello World!");
}

Answer:
Hello World!Hello World!

Explanation:

The fork creates a child that is a duplicate of the parent process. The child begins from the fork().All the statements after the call to fork() will be executed twice.(once by the parent process and other by child). The statement before fork() is executed only by the parent process.
16. Predict the output of the following program code
main()
{
fork(); fork(); fork();
printf("Hello World!");
}

Answer:
"Hello World" will be printed 8 times.
Explanation:
2^n times where n is the number of calls to fork()
17. List the system calls used for process management:
System calls Description
  • fork() To create a new process
  • exec() To execute a new program in a process
  • wait() To wait until a created process completes its execution
  • exit() To exit from a process execution
  • getpid() To get a process identifier of the current process
  • getppid() To get parent process identifier
  • nice() To bias the existing priority of a process
  • brk() To increase/decrease the data segment size of a process.
18. How can you get/set an environment variable from a program?
Getting the value of an environment variable is done by using `getenv()'. Setting the value of an environment variable is done by using `putenv()'.
19. How can a parent and child process communicate?
A parent and child can communicate through any of the normal inter-process communication schemes (pipes, sockets, message queues, shared memory), but also have some special ways to communicate that take advantage of their relationship as a parent and child. One of the most obvious is that the parent can get the exit status of the child.
20. What is a zombie?
When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls `wait()'; In the interval between the child terminating and the parent calling `wait()', the child is said to be a `zombie' (If you do `ps', the child will have a `Z' in its status field to indicate this.)
21. What are the process states in Unix?
As a process executes it changes state according to its circumstances. Unix processes have the following states:
Running : The process is either running or it is ready to run .
Waiting : The process is waiting for an event or for a resource.
Stopped : The process has been stopped, usually by receiving a signal.
Zombie : The process is dead but have not been removed from the process table.

What is the use of ‘grep’ command?
‘grep’ is a pattern search command. It searches for the pattern, specified in the command line with appropriate option, in a file(s).
Syntax : grep
Example : grep 99mx mcafile
What is the difference between cat and more command?
Cat displays file contents. If the file is large the contents scroll off the screen before we view it. So command 'more' is like a pager which displays the contents page by page.
Write a command to kill the last background job?
Kill $!
Which command is used to delete all files in the current directory and all its sub-directories?
rm -r *
Write a command to display a file’s contents in various formats?
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.
What will the following command do?
$ echo *
It is similar to 'ls' command and displays all the files in the current directory.
Is it possible to create new a file system in UNIX?
Yes, ‘mkfs’ is used to create a new file system.
Is it possible to restrict incoming message?
Yes, using the ‘mesg’ command.
What is the use of the command "ls -x chapter[1-5]"
ls stands for list; so it displays the list of the files that starts with 'chapter' with suffix '1' to '5', chapter1, chapter2, and so on.
Is ‘du’ a command? If so, what is its use?
Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and free space of the disk.
Is it possible to count number char, line in a file; if so, How?
Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.
Name the data structure used to maintain file identification?
‘inode’, each file has a separate inode and a unique inode number.
How many prompts are available in a UNIX system?
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).
How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.
How to switch to a super user status to gain privileges?
Use ‘su’ command. The system asks for password and when valid entry is made the user gains super user (admin) privileges.
What are shell variables?
Shell variables are special variables, a name-value pair created and maintained by the shell.
Example: PATH, HOME, MAIL and TERM
What is redirection?
Directing the flow of data to the file or from the file for input or output.
Example : ls > wc
How to terminate a process which is running and the specialty on command kill 0?
With the help of kill command we can terminate the process.
Syntax: kill pid
Kill 0 - kills all processes in your system except the login shell.
What is a pipe and give an example?
A pipe is two or more commands separated by pipe char '|'. That tells the shell to arrange for the output of the preceding command to be passed as input to the following command.
Example : ls -l | pr
The output for a command ls is the standard input of pr.
When a sequence of commands are combined using pipe, then it is called pipeline.
Explain kill() and its possible return values.
There are four possible results from this call:
‘kill()’ returns 0. This implies that a process exists with the given PID, and the system would allow you to send signals to it. It is system-dependent whether the process could be a zombie.
‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with the given PID, or security enhancements are causing the system to deny its existence. (On some systems, the process could be a zombie.)
‘kill()’ returns -1, ‘errno == EPERM’ the system would not allow you to kill the specified process. This means that either the process exists (again, it could be a zombie) or draconian security enhancements are present (e.g. your process is not allowed to send signals to *anybody*).
‘kill()’ returns -1, with some other value of ‘errno’ you are in trouble! The most-used technique is to assume that success or failure with ‘EPERM’ implies that the process exists, and any other error implies that it doesn't.
An alternative exists, if you are writing specifically for a system (or all those systems) that provide a ‘/proc’ filesystem: checking for the existence of ‘/proc/PID’ may work.

FreeBSD can be used in various ways. One of them is typing commands to a text terminal. A lot of the flexibility and power of a UNIX® operating system is readily available at your hands when using FreeBSD this way. This section describes what “terminals” and “consoles” are, and how you can use them in FreeBSD.

3.2.1 The Console

If you have not configured FreeBSD to automatically start a graphical environment during startup, the system will present you with a login prompt after it boots, right after the startup scripts finish running. You will see something similar to:
Additional ABI support:.
Local package initialization:.
Additional TCP options:.
 
Fri Sep 20 13:01:06 EEST 2002
 
FreeBSD/i386 (pc3.example.org) (ttyv0)
 
login:
The messages might be a bit different on your system, but you will see something similar. The last two lines are what we are interested in right now. The second last line reads:
FreeBSD/i386 (pc3.example.org) (ttyv0)
This line contains some bits of information about the system you have just booted. You are looking at a “FreeBSD” console, running on an Intel or compatible processor of the x86 architecture[1]. The name of this machine (every UNIX machine has a name) is pc3.example.org, and you are now looking at its system console--the ttyv0 terminal.
Finally, the last line is always:
login:
This is the part where you are supposed to type in your “username” to log into FreeBSD. The next section describes how you can do this.

3.2.2 Logging into FreeBSD

FreeBSD is a multiuser, multiprocessing system. This is the formal description that is usually given to a system that can be used by many different people, who simultaneously run a lot of programs on a single machine.
Every multiuser system needs some way to distinguish one “user” from the rest. In FreeBSD (and all the UNIX-like operating systems), this is accomplished by requiring that every user must “log into” the system before being able to run programs. Every user has a unique name (the “username”) and a personal, secret key (the “password”). FreeBSD will ask for these two before allowing a user to run any programs.
Right after FreeBSD boots and finishes running its startup scripts[2], it will present you with a prompt and ask for a valid username:
login:
For the sake of this example, let us assume that your username is john. Type john at this prompt and press Enter. You should then be presented with a prompt to enter a “password”:
login: john
Password:
Type in john's password now, and press Enter. The password is not echoed! You need not worry about this right now. Suffice it to say that it is done for security reasons.
If you have typed your password correctly, you should by now be logged into FreeBSD and ready to try out all the available commands.
You should see the MOTD or message of the day followed by a command prompt (a #, $, or % character). This indicates you have successfully logged into FreeBSD.

3.2.3 Multiple Consoles

Running UNIX commands in one console is fine, but FreeBSD can run many programs at once. Having one console where commands can be typed would be a bit of a waste when an operating system like FreeBSD can run dozens of programs at the same time. This is where “virtual consoles” can be very helpful.
FreeBSD can be configured to present you with many different virtual consoles. You can switch from one of them to any other virtual console by pressing a couple of keys on your keyboard. Each console has its own different output channel, and FreeBSD takes care of properly redirecting keyboard input and monitor output as you switch from one virtual console to the next.
Special key combinations have been reserved by FreeBSD for switching consoles[3]. You can use Alt-F1, Alt-F2, through Alt-F8 to switch to a different virtual console in FreeBSD.
As you are switching from one console to the next, FreeBSD takes care of saving and restoring the screen output. The result is an “illusion” of having multiple “virtual” screens and keyboards that you can use to type commands for FreeBSD to run. The programs that you launch on one virtual console do not stop running when that console is not visible. They continue running when you have switched to a different virtual console.

3.2.4 The /etc/ttys File

The default configuration of FreeBSD will start up with eight virtual consoles. This is not a hardwired setting though, and you can easily customize your installation to boot with more or fewer virtual consoles. The number and settings of the virtual consoles are configured in the /etc/ttys file.
You can use the /etc/ttys file to configure the virtual consoles of FreeBSD. Each uncommented line in this file (lines that do not start with a # character) contains settings for a single terminal or virtual console. The default version of this file that ships with FreeBSD configures nine virtual consoles, and enables eight of them. They are the lines that start with ttyv:
# name  getty                           type    status          comments
#
ttyv0   "/usr/libexec/getty Pc"         cons25  on  secure
# Virtual terminals
ttyv1   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv2   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv3   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv4   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv5   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv6   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv7   "/usr/libexec/getty Pc"         cons25  on  secure
ttyv8   "/usr/X11R6/bin/xdm -nodaemon"  xterm   off secure
For a detailed description of every column in this file and all the options you can use to set things up for the virtual consoles, consult the ttys(5) manual page.

3.2.5 Single User Mode Console

A detailed description of what “single user mode” is can be found in Section 12.6.2. It is worth noting that there is only one console when you are running FreeBSD in single user mode. There are no virtual consoles available. The settings of the single user mode console can also be found in the /etc/ttys file. Look for the line that starts with console:
# name  getty                           type    status          comments
#
# If console is marked "insecure", then init will ask for the root password
# when going to single-user mode.
console none                            unknown off secure
Note: As the comments above the console line indicate, you can edit this line and change secure to insecure. If you do that, when FreeBSD boots into single user mode, it will still ask for the root password.
Be careful when changing this to insecure. If you ever forget the root password, booting into single user mode is a bit involved. It is still possible, but it might be a bit hard for someone who is not very comfortable with the FreeBSD booting process and the programs involved.

3.2.6 Changing Console Video Modes

The FreeBSD console default video mode may be adjusted to 1024x768, 1280x1024, or any other size supported by your graphics chip and monitor. To use a different video mode, you first must recompile your kernel and include two additional options:
options VESA
options SC_PIXEL_MODE
Once the kernel has been recompiled with these two options, you can then determine what video modes are supported by your hardware by using the vidcontrol(1) utility. To get a list of supported video modes issue the following:
# vidcontrol -i mode
The output of this command is a list of video modes that are supported by your hardware. You can then choose to use a new video mode by passing it to vidcontrol(1) in a root console:
# vidcontrol MODE_279
If the new video mode is acceptable, it can be permanently set on boot by setting it in the /etc/rc.conf file:
allscreens_flags="MODE_279"

Notes

This is what i386 means. Note that even if you are not running FreeBSD on an Intel 386 CPU, this is going to be i386. It is not the type of your processor, but the processor “architecture” that is shown here.
Startup scripts are programs that are run automatically by FreeBSD when booting. Their main function is to set things up for everything else to run, and start any services that you have configured to run in the background doing useful things.
A fairly technical and accurate description of all the details of the FreeBSD console and keyboard drivers can be found in the manual pages of syscons(4), atkbd(4), vidcontrol(1) and kbdcontrol(1). We will not expand on the details here, but the interested reader can always consult the manual pages for a more detailed and thorough explanation of how things work.

Permissions

FreeBSD, being a direct descendant of BSD UNIX®, is based on several key UNIX concepts. The first and most pronounced is that FreeBSD is a multi-user operating system. The system can handle several users all working simultaneously on completely unrelated tasks. The system is responsible for properly sharing and managing requests for hardware devices, peripherals, memory, and CPU time fairly to each user.
Because the system is capable of supporting multiple users, everything the system manages has a set of permissions governing who can read, write, and execute the resource. These permissions are stored as three octets broken into three pieces, one for the owner of the file, one for the group that the file belongs to, and one for everyone else. This numerical representation works like this:
Value
Permission
Directory Listing
0
No read, no write, no execute
---
1
No read, no write, execute
--x
2
No read, write, no execute
-w-
3
No read, write, execute
-wx
4
Read, no write, no execute
r--
5
Read, no write, execute
r-x
6
Read, write, no execute
rw-
7
Read, write, execute
rwx
You can use the -l command line argument to ls(1) to view a long directory listing that includes a column with information about a file's permissions for the owner, group, and everyone else. For example, a ls -l in an arbitrary directory may show:
% ls -l
total 530
-rw-r--r--  1 root  wheel     512 Sep  5 12:31 myfile
-rw-r--r--  1 root  wheel     512 Sep  5 12:31 otherfile
-rw-r--r--  1 root  wheel    7680 Sep  5 12:31 email.txt
...
Here is how the first column of ls -l is broken up:
-rw-r--r--
The first (leftmost) character tells if this file is a regular file, a directory, a special character device, a socket, or any other special pseudo-file device. In this case, the - indicates a regular file. The next three characters, rw- in this example, give the permissions for the owner of the file. The next three characters, r--, give the permissions for the group that the file belongs to. The final three characters, r--, give the permissions for the rest of the world. A dash means that the permission is turned off. In the case of this file, the permissions are set so the owner can read and write to the file, the group can read the file, and the rest of the world can only read the file. According to the table above, the permissions for this file would be 644, where each digit represents the three parts of the file's permission.
This is all well and good, but how does the system control permissions on devices? FreeBSD actually treats most hardware devices as a file that programs can open, read, and write data to just like any other file. These special device files are stored on the /dev directory.
Directories are also treated as files. They have read, write, and execute permissions. The executable bit for a directory has a slightly different meaning than that of files. When a directory is marked executable, it means it can be traversed into, that is, it is possible to “cd” (change directory) into it. This also means that within the directory it is possible to access files whose names are known (subject, of course, to the permissions on the files themselves).
In particular, in order to perform a directory listing, read permission must be set on the directory, whilst to delete a file that one knows the name of, it is necessary to have write and execute permissions to the directory containing the file.
There are more permission bits, but they are primarily used in special circumstances such as setuid binaries and sticky directories. If you want more information on file permissions and how to set them, be sure to look at the chmod(1) manual page.

3.3.1 Symbolic Permissions

Contributed by Tom Rhodes.
Symbolic permissions, sometimes referred to as symbolic expressions, use characters in place of octal values to assign permissions to files or directories. Symbolic expressions use the syntax of (who) (action) (permissions), where the following values are available:
Option
Letter
Represents
(who)
u
User
(who)
g
Group owner
(who)
o
Other
(who)
a
All (“world”)
(action)
+
Adding permissions
(action)
-
Removing permissions
(action)
=
Explicitly set permissions
(permissions)
r
Read
(permissions)
w
Write
(permissions)
x
Execute
(permissions)
t
Sticky bit
(permissions)
s
Set UID or GID
These values are used with the chmod(1) command just like before, but with letters. For an example, you could use the following command to block other users from accessing FILE:
% chmod go= FILE
A comma separated list can be provided when more than one set of changes to a file must be made. For example the following command will remove the group and “world” write permission on FILE, then it adds the execute permissions for everyone:
% chmod go-w,a+x FILE

3.3.2 FreeBSD File Flags

Contributed by Tom Rhodes.
In addition to file permissions discussed previously, FreeBSD supports the use of “file flags.” These flags add an additional level of security and control over files, but not directories.
These file flags add an additional level of control over files, helping to ensure that in some cases not even the root can remove or alter files.
File flags are altered by using the chflags(1) utility, using a simple interface. For example, to enable the system undeletable flag on the file file1, issue the following command:
# chflags sunlink file1
And to disable the system undeletable flag, simply issue the previous command with “no” in front of the sunlink. Observe:
# chflags nosunlink file1
To view the flags of this file, use the ls(1) command with the -lo flags:
# ls -lo file1
The output should look like the following:
-rw-r--r--  1 trhodes  trhodes  sunlnk 0 Mar  1 05:54 file1
Several flags may only added or removed to files by the root user. In other cases, the file owner may set these flags. It is recommended that administrators read over the chflags(1) and chflags(2) manual pages for more information.

3.3.3 The setuid, setgid, and sticky Permissions

Contributed by Tom Rhodes.
Other than the permissions already discussed, there are three other specific settings that all administrators should know about. They are the setuid, setgid and sticky permissions.
These settings are important for some UNIX operations as they provide functionality not normally granted to normal users. To understand them, the difference between the real user ID and effective user ID must also be noted.
The real user ID is the UID who owns or starts the process. The effective UID is the user ID the process runs as. As an example, the passwd(1) utility runs with the real user ID as the user changing their password; however, to manipulate the password database, it runs as the effective ID of the root user. This is what allows normal users to change their passwords without seeing a “Permission Denied” error.
Note: The nosuid mount(8) option will cause these binaries to silently fail. That is, they will fail to execute without ever alerting the user. That option is also not completely reliable as a nosuid wrapper may be able to circumvent it; according to the mount(8) manual page.
The setuid permission may be set by prefixing a permission set with the number four (4) as shown in the following example:
# chmod 4755 suidexample.sh
The permissions on the suidexample.sh file should now look like the following:
-rwsr-xr-x   1 trhodes  trhodes    63 Aug 29 06:36 suidexample.sh
It should be noticeable from this example that an s is now part of the permission set designated for the file owner, replacing the executable bit. This allows utilities which need elevated permissions, such as passwd.
To view this in real time, open two terminals. On one, start the passwd process as a normal user. While it waits for a new password, check the process table and look at the user information of the passwd command.
In terminal A:
Changing local password for trhodes
Old Password:
In terminal B:
# ps aux | grep passwd
trhodes  5232  0.0  0.2  3420  1608   0  R+    2:10AM   0:00.00 grep passwd
root     5211  0.0  0.2  3620  1724   2  I+    2:09AM   0:00.01 passwd
As stated above, the passwd is run by a normal user, but is using the effective UID of root.
The setgid permission performs the same function as the setuid permission; except that it alters the group settings. When an application or utility is ran with this setting, it will be granted the permissions based on the group that owns the file, not the user who started the process.
To set the setgid permission on a file, provide the chmod command with a leading two (2) as in the following example:
# chmod 2755 sgidexample.sh
The new setting may be viewed as before, notice the s is now in the field designated for the group permission settings:
-rwxr-sr-x   1 trhodes  trhodes    44 Aug 31 01:49 sgidexample.sh
Note: In these examples, even though the shell script in question is an executable file, it will not run with a different EUID or effective user ID. This is because shell scripts may not access the setuid(2) system calls.

The first two special permission bits we discussed (the setuid and setgid permission bits) may lower system security, by allowing for elevated permissions. There is a third special permission bit that can strengthen the security of a system: the sticky bit.
The sticky bit, when set on a directory, allows file deletion only by the file owner. This permission set is useful to prevent file deletion in public directories, such as /tmp, by users who do not own the file. To utilize this permission, prefix the permission with a one (1). For example:
# chmod 1777 /tmp
Now, it is possible to see the effect by using the ls command:
# ls -al / | grep tmp
drwxrwxrwt  10 root  wheel         512 Aug 31 01:49 tmp
The sticky bit permission is distinguishable from the t at the very end of the set.

Directory Structure

The FreeBSD directory hierarchy is fundamental to obtaining an overall understanding of the system. The most important concept to grasp is that of the root directory, “/”. This directory is the first one mounted at boot time and it contains the base system necessary to prepare the operating system for multi-user operation. The root directory also contains mount points for other file systems that are mounted during the transition to multi-user operation.
A mount point is a directory where additional file systems can be grafted onto a parent file system (usually the root file system). This is further described in Section 3.5. Standard mount points include /usr, /var, /tmp, /mnt, and /cdrom. These directories are usually referenced to entries in the file /etc/fstab. /etc/fstab is a table of various file systems and mount points for reference by the system. Most of the file systems in /etc/fstab are mounted automatically at boot time from the script rc(8) unless they contain the noauto option. Details can be found in Section 3.6.1.
A complete description of the file system hierarchy is available in hier(7). For now, a brief overview of the most common directories will suffice.
Directory
Description
/
Root directory of the file system.
/bin/
User utilities fundamental to both single-user and multi-user environments.
/boot/
Programs and configuration files used during operating system bootstrap.
/boot/defaults/
Default bootstrapping configuration files; see loader.conf(5).
/dev/
Device nodes; see intro(4).
/etc/
System configuration files and scripts.
/etc/defaults/
Default system configuration files; see rc(8).
/etc/mail/
Configuration files for mail transport agents such as sendmail(8).
/etc/namedb/
named configuration files; see named(8).
/etc/periodic/
Scripts that are run daily, weekly, and monthly, via cron(8); see periodic(8).
/etc/ppp/
ppp configuration files; see ppp(8).
/mnt/
Empty directory commonly used by system administrators as a temporary mount point.
/proc/
Process file system; see procfs(5), mount_procfs(8).
/rescue/
Statically linked programs for emergency recovery; see rescue(8).
/root/
Home directory for the root account.
/sbin/
System programs and administration utilities fundamental to both single-user and multi-user environments.
/tmp/
Temporary files. The contents of /tmp are usually NOT preserved across a system reboot. A memory-based file system is often mounted at /tmp. This can be automated using the tmpmfs-related variables of rc.conf(5) (or with an entry in /etc/fstab; see mdmfs(8)).
/usr/
The majority of user utilities and applications.
/usr/bin/
Common utilities, programming tools, and applications.
/usr/include/
Standard C include files.
/usr/lib/
Archive libraries.
/usr/libdata/
Miscellaneous utility data files.
/usr/libexec/
System daemons & system utilities (executed by other programs).
/usr/local/
Local executables, libraries, etc. Also used as the default destination for the FreeBSD ports framework. Within /usr/local, the general layout sketched out by hier(7) for /usr should be used. Exceptions are the man directory, which is directly under /usr/local rather than under /usr/local/share, and the ports documentation is in share/doc/port.
/usr/obj/
Architecture-specific target tree produced by building the /usr/src tree.
/usr/ports
The FreeBSD Ports Collection (optional).
/usr/sbin/
System daemons & system utilities (executed by users).
/usr/share/
Architecture-independent files.
/usr/src/
BSD and/or local source files.
/usr/X11R6/
X11R6 distribution executables, libraries, etc (optional).
/var/
Multi-purpose log, temporary, transient, and spool files. A memory-based file system is sometimes mounted at /var. This can be automated using the varmfs-related variables of rc.conf(5) (or with an entry in /etc/fstab; see mdmfs(8)).
/var/log/
Miscellaneous system log files.
/var/mail/
User mailbox files.
/var/spool/
Miscellaneous printer and mail system spooling directories.
/var/tmp/
Temporary files. The files are usually preserved across a system reboot, unless /var is a memory-based file system.
/var/yp
NIS maps.
The smallest unit of organization that FreeBSD uses to find files is the filename. Filenames are case-sensitive, which means that readme.txt and README.TXT are two separate files. FreeBSD does not use the extension (.txt) of a file to determine whether the file is a program, or a document, or some other form of data.
Files are stored in directories. A directory may contain no files, or it may contain many hundreds of files. A directory can also contain other directories, allowing you to build up a hierarchy of directories within one another. This makes it much easier to organize your data.
Files and directories are referenced by giving the file or directory name, followed by a forward slash, /, followed by any other directory names that are necessary. If you have directory foo, which contains directory bar, which contains the file readme.txt, then the full name, or path to the file is foo/bar/readme.txt.
Directories and files are stored in a file system. Each file system contains exactly one directory at the very top level, called the root directory for that file system. This root directory can then contain other directories.
So far this is probably similar to any other operating system you may have used. There are a few differences; for example, MS-DOS® uses \ to separate file and directory names, while Mac OS® uses :.
FreeBSD does not use drive letters, or other drive names in the path. You would not write c:/foo/bar/readme.txt on FreeBSD.
Instead, one file system is designated the root file system. The root file system's root directory is referred to as /. Every other file system is then mounted under the root file system. No matter how many disks you have on your FreeBSD system, every directory appears to be part of the same disk.
Suppose you have three file systems, called A, B, and C. Each file system has one root directory, which contains two other directories, called A1, A2 (and likewise B1, B2 and C1, C2).
Call A the root file system. If you used the ls command to view the contents of this directory you would see two subdirectories, A1 and A2. The directory tree looks like this:

A file system must be mounted on to a directory in another file system. So now suppose that you mount file system B on to the directory A1. The root directory of B replaces A1, and the directories in B appear accordingly:

Any files that are in the B1 or B2 directories can be reached with the path /A1/B1 or /A1/B2 as necessary. Any files that were in /A1 have been temporarily hidden. They will reappear if B is unmounted from A.
If B had been mounted on A2 then the diagram would look like this:

and the paths would be /A2/B1 and /A2/B2 respectively.
File systems can be mounted on top of one another. Continuing the last example, the C file system could be mounted on top of the B1 directory in the B file system, leading to this arrangement:

Or C could be mounted directly on to the A file system, under the A1 directory:

If you are familiar with MS-DOS, this is similar, although not identical, to the join command.
This is not normally something you need to concern yourself with. Typically you create file systems when installing FreeBSD and decide where to mount them, and then never change them unless you add a new disk.
It is entirely possible to have one large root file system, and not need to create any others. There are some drawbacks to this approach, and one advantage.
Benefits of Multiple File Systems
·         Different file systems can have different mount options. For example, with careful planning, the root file system can be mounted read-only, making it impossible for you to inadvertently delete or edit a critical file. Separating user-writable file systems, such as /home, from other file systems also allows them to be mounted nosuid; this option prevents the suid/guid bits on executables stored on the file system from taking effect, possibly improving security.
·         FreeBSD automatically optimizes the layout of files on a file system, depending on how the file system is being used. So a file system that contains many small files that are written frequently will have a different optimization to one that contains fewer, larger files. By having one big file system this optimization breaks down.
·         FreeBSD's file systems are very robust should you lose power. However, a power loss at a critical point could still damage the structure of the file system. By splitting your data over multiple file systems it is more likely that the system will still come up, making it easier for you to restore from backup as necessary.
Benefit of a Single File System
·         File systems are a fixed size. If you create a file system when you install FreeBSD and give it a specific size, you may later discover that you need to make the partition bigger. This is not easily accomplished without backing up, recreating the file system with the new size, and then restoring the backed up data.
Important: FreeBSD features the growfs(8) command, which makes it possible to increase the size of file system on the fly, removing this limitation.
File systems are contained in partitions. This does not have the same meaning as the common usage of the term partition (for example, MS-DOS partition), because of FreeBSD's UNIX® heritage. Each partition is identified by a letter from a through to h. Each partition can contain only one file system, which means that file systems are often described by either their typical mount point in the file system hierarchy, or the letter of the partition they are contained in.
FreeBSD also uses disk space for swap space. Swap space provides FreeBSD with virtual memory. This allows your computer to behave as though it has much more memory than it actually does. When FreeBSD runs out of memory it moves some of the data that is not currently being used to the swap space, and moves it back in (moving something else out) when it needs it.
Some partitions have certain conventions associated with them.
Partition
Convention
a
Normally contains the root file system
b
Normally contains swap space
c
Normally the same size as the enclosing slice. This allows utilities that need to work on the entire slice (for example, a bad block scanner) to work on the c partition. You would not normally create a file system on this partition.
d
Partition d used to have a special meaning associated with it, although that is now gone and d may work as any normal partition.
Each partition-that-contains-a-file-system is stored in what FreeBSD calls a slice. Slice is FreeBSD's term for what the common call partitions, and again, this is because of FreeBSD's UNIX background. Slices are numbered, starting at 1, through to 4.
Slice numbers follow the device name, prefixed with an s, starting at 1. So “da0s1” is the first slice on the first SCSI drive. There can only be four physical slices on a disk, but you can have logical slices inside physical slices of the appropriate type. These extended slices are numbered starting at 5, so “ad0s5” is the first extended slice on the first IDE disk. These devices are used by file systems that expect to occupy a slice.
Slices, “dangerously dedicated” physical drives, and other drives contain partitions, which are represented as letters from a to h. This letter is appended to the device name, so “da0a” is the a partition on the first da drive, which is “dangerously dedicated”. “ad1s3e” is the fifth partition in the third slice of the second IDE disk drive.
Finally, each disk on the system is identified. A disk name starts with a code that indicates the type of disk, and then a number, indicating which disk it is. Unlike slices, disk numbering starts at 0. Common codes that you will see are listed in Table 3-1.
When referring to a partition FreeBSD requires that you also name the slice and disk that contains the partition, and when referring to a slice you must also refer to the disk name. Thus, you refer to a partition by listing the disk name, s, the slice number, and then the partition letter. Examples are shown in Example 3-1.
Example 3-2 shows a conceptual model of the disk layout that should help make things clearer.
In order to install FreeBSD you must first configure the disk slices, then create partitions within the slice you will use for FreeBSD, and then create a file system (or swap space) in each partition, and decide where that file system will be mounted.
Table 3-1. Disk Device Codes
Code
Meaning
ad
ATAPI (IDE) disk
da
SCSI direct access disk
acd
ATAPI (IDE) CDROM
cd
SCSI CDROM
fd
Floppy disk
Example 3-1. Sample Disk, Slice, and Partition Names
Name
Meaning
ad0s1a
The first partition (a) on the first slice (s1) on the first IDE disk (ad0).
da1s2e
The fifth partition (e) on the second slice (s2) on the second SCSI disk (da1).
Example 3-2. Conceptual Model of a Disk
This diagram shows FreeBSD's view of the first IDE disk attached to the system. Assume that the disk is 4 GB in size, and contains two 2 GB slices (MS-DOS partitions). The first slice contains a MS-DOS disk, C:, and the second slice contains a FreeBSD installation. This example FreeBSD installation has three data partitions, and a swap partition.
The three partitions will each hold a file system. Partition a will be used for the root file system, e for the /var directory hierarchy, and f for the /usr directory hierarchy.





Mounting and Unmounting File Systems

The file system is best visualized as a tree, rooted, as it were, at /. /dev, /usr, and the other directories in the root directory are branches, which may have their own branches, such as /usr/local, and so on.
There are various reasons to house some of these directories on separate file systems. /var contains the directories log/, spool/, and various types of temporary files, and as such, may get filled up. Filling up the root file system is not a good idea, so splitting /var from / is often favorable.
Another common reason to contain certain directory trees on other file systems is if they are to be housed on separate physical disks, or are separate virtual disks, such as Network File System mounts, or CDROM drives.

3.6.1 The fstab File

During the boot process, file systems listed in /etc/fstab are automatically mounted (unless they are listed with the noauto option).
The /etc/fstab file contains a list of lines of the following format:
device       /mount-point fstype     options      dumpfreq     passno
device
A device name (which should exist), as explained in Section 18.2.
mount-point
A directory (which should exist), on which to mount the file system.
fstype
The file system type to pass to mount(8). The default FreeBSD file system is ufs.
options
Either rw for read-write file systems, or ro for read-only file systems, followed by any other options that may be needed. A common option is noauto for file systems not normally mounted during the boot sequence. Other options are listed in the mount(8) manual page.
dumpfreq
This is used by dump(8) to determine which file systems require dumping. If the field is missing, a value of zero is assumed.
passno
This determines the order in which file systems should be checked. File systems that should be skipped should have their passno set to zero. The root file system (which needs to be checked before everything else) should have its passno set to one, and other file systems' passno should be set to values greater than one. If more than one file systems have the same passno then fsck(8) will attempt to check file systems in parallel if possible.
Consult the fstab(5) manual page for more information on the format of the /etc/fstab file and the options it contains.

3.6.2 The mount Command

The mount(8) command is what is ultimately used to mount file systems.
In its most basic form, you use:
# mount device mountpoint
There are plenty of options, as mentioned in the mount(8) manual page, but the most common are:
Mount Options
-a
Mount all the file systems listed in /etc/fstab. Except those marked as “noauto”, excluded by the -t flag, or those that are already mounted.
-d
Do everything except for the actual mount system call. This option is useful in conjunction with the -v flag to determine what mount(8) is actually trying to do.
-f
Force the mount of an unclean file system (dangerous), or forces the revocation of write access when downgrading a file system's mount status from read-write to read-only.
-r
Mount the file system read-only. This is identical to using the ro (rdonly for FreeBSD versions older than 5.2) argument to the -o option.
-t fstype
Mount the given file system as the given file system type, or mount only file systems of the given type, if given the -a option.
“ufs” is the default file system type.
-u
Update mount options on the file system.
-v
Be verbose.
-w
Mount the file system read-write.
The -o option takes a comma-separated list of the options, including the following:
noexec
Do not allow execution of binaries on this file system. This is also a useful security option.
nosuid
Do not interpret setuid or setgid flags on the file system. This is also a useful security option.

3.6.3 The umount Command

The umount(8) command takes, as a parameter, one of a mountpoint, a device name, or the -a or -A option.
All forms take -f to force unmounting, and -v for verbosity. Be warned that -f is not generally a good idea. Forcibly unmounting file systems might crash the computer or damage data on the file system.
-a and -A are used to unmount all mounted file systems, possibly modified by the file system types listed after -t. -A, however, does not attempt to unmount the root file system.

Processes

FreeBSD is a multi-tasking operating system. This means that it seems as though more than one program is running at once. Each program running at any one time is called a process. Every command you run will start at least one new process, and there are a number of system processes that run all the time, keeping the system functional.
Each process is uniquely identified by a number called a process ID, or PID, and, like files, each process also has one owner and group. The owner and group information is used to determine what files and devices the process can open, using the file permissions discussed earlier. Most processes also have a parent process. The parent process is the process that started them. For example, if you are typing commands to the shell then the shell is a process, and any commands you run are also processes. Each process you run in this way will have your shell as its parent process. The exception to this is a special process called init(8). init is always the first process, so its PID is always 1. init is started automatically by the kernel when FreeBSD starts.
Two commands are particularly useful to see the processes on the system, ps(1) and top(1). The ps command is used to show a static list of the currently running processes, and can show their PID, how much memory they are using, the command line they were started with, and so on. The top command displays all the running processes, and updates the display every few seconds, so that you can interactively see what your computer is doing.
By default, ps only shows you the commands that are running and are owned by you. For example:
% ps
  PID  TT  STAT      TIME COMMAND
  298  p0  Ss     0:01.10 tcsh
 7078  p0  S      2:40.88 xemacs mdoc.xsl (xemacs-21.1.14)
37393  p0  I      0:03.11 xemacs freebsd.dsl (xemacs-21.1.14)
48630  p0  S      2:50.89 /usr/local/lib/netscape-linux/navigator-linux-4.77.bi
48730  p0  IW     0:00.00 (dns helper) (navigator-linux-)
72210  p0  R+     0:00.00 ps
  390  p1  Is     0:01.14 tcsh
 7059  p2  Is+    1:36.18 /usr/local/bin/mutt -y
 6688  p3  IWs    0:00.00 tcsh
10735  p4  IWs    0:00.00 tcsh
20256  p5  IWs    0:00.00 tcsh
  262  v0  IWs    0:00.00 -tcsh (tcsh)
  270  v0  IW+    0:00.00 /bin/sh /usr/X11R6/bin/startx -- -bpp 16
  280  v0  IW+    0:00.00 xinit /home/nik/.xinitrc -- -bpp 16
  284  v0  IW     0:00.00 /bin/sh /home/nik/.xinitrc
  285  v0  S      0:38.45 /usr/X11R6/bin/sawfish
As you can see in this example, the output from ps(1) is organized into a number of columns. PID is the process ID discussed earlier. PIDs are assigned starting from 1, go up to 99999, and wrap around back to the beginning when you run out (a PID is not reassigned if it is already in use). The TT column shows the tty the program is running on, and can safely be ignored for the moment. STAT shows the program's state, and again, can be safely ignored. TIME is the amount of time the program has been running on the CPU--this is usually not the elapsed time since you started the program, as most programs spend a lot of time waiting for things to happen before they need to spend time on the CPU. Finally, COMMAND is the command line that was used to run the program.
ps(1) supports a number of different options to change the information that is displayed. One of the most useful sets is auxww. a displays information about all the running processes, not just your own. u displays the username of the process' owner, as well as memory usage. x displays information about daemon processes, and ww causes ps(1) to display the full command line for each process, rather than truncating it once it gets too long to fit on the screen.
The output from top(1) is similar. A sample session looks like this:
% top
last pid: 72257;  load averages:  0.13,  0.09,  0.03    up 0+13:38:33  22:39:10
47 processes:  1 running, 46 sleeping
CPU states: 12.6% user,  0.0% nice,  7.8% system,  0.0% interrupt, 79.7% idle
Mem: 36M Active, 5256K Inact, 13M Wired, 6312K Cache, 15M Buf, 408K Free
Swap: 256M Total, 38M Used, 217M Free, 15% Inuse
 
  PID USERNAME PRI NICE  SIZE    RES STATE    TIME   WCPU    CPU COMMAND
72257 nik       28   0  1960K  1044K RUN      0:00 14.86%  1.42% top
 7078 nik        2   0 15280K 10960K select   2:54  0.88%  0.88% xemacs-21.1.14
  281 nik        2   0 18636K  7112K select   5:36  0.73%  0.73% XF86_SVGA
  296 nik        2   0  3240K  1644K select   0:12  0.05%  0.05% xterm
48630 nik        2   0 29816K  9148K select   3:18  0.00%  0.00% navigator-linu
  175 root       2   0   924K   252K select   1:41  0.00%  0.00% syslogd
 7059 nik        2   0  7260K  4644K poll     1:38  0.00%  0.00% mutt
...
The output is split into two sections. The header (the first five lines) shows the PID of the last process to run, the system load averages (which are a measure of how busy the system is), the system uptime (time since the last reboot) and the current time. The other figures in the header relate to how many processes are running (47 in this case), how much memory and swap space has been taken up, and how much time the system is spending in different CPU states.
Below that are a series of columns containing similar information to the output from ps(1). As before you can see the PID, the username, the amount of CPU time taken, and the command that was run. top(1) also defaults to showing you the amount of memory space taken by the process. This is split into two columns, one for total size, and one for resident size--total size is how much memory the application has needed, and the resident size is how much it is actually using at the moment. In this example you can see that Netscape® has required almost 30 MB of RAM, but is currently only using 9 MB.
top(1) automatically updates this display every two seconds; this can be changed with the s option.

Daemons, Signals, and Killing Processes

When you run an editor it is easy to control the editor, tell it to load files, and so on. You can do this because the editor provides facilities to do so, and because the editor is attached to a terminal. Some programs are not designed to be run with continuous user input, and so they disconnect from the terminal at the first opportunity. For example, a web server spends all day responding to web requests, it normally does not need any input from you. Programs that transport email from site to site are another example of this class of application.
We call these programs daemons. Daemons were characters in Greek mythology: neither good or evil, they were little attendant spirits that, by and large, did useful things for mankind, much like the web servers and mail servers of today do useful things. This is why the BSD mascot has, for a long time, been the cheerful-looking daemon with sneakers and a pitchfork.
There is a convention to name programs that normally run as daemons with a trailing “d”. BIND is the Berkeley Internet Name Domain, but the actual program that executes is called named; the Apache web server program is called httpd; the line printer spooling daemon is lpd and so on. This is a convention, not a hard and fast rule; for example, the main mail daemon for the Sendmail application is called sendmail, and not maild, as you might imagine.
Sometimes you will need to communicate with a daemon process. One way to do so is to send it (or any other running process), what is known as a signal. There are a number of different signals that you can send--some of them have a specific meaning, others are interpreted by the application, and the application's documentation will tell you how that application interprets signals. You can only send a signal to a process that you own. If you send a signal to someone else's process with kill(1) or kill(2), permission will be denied. The exception to this is the root user, who can send signals to everyone's processes.
FreeBSD will also send applications signals in some cases. If an application is badly written, and tries to access memory that it is not supposed to, FreeBSD sends the process the Segmentation Violation signal (SIGSEGV). If an application has used the alarm(3) system call to be alerted after a period of time has elapsed then it will be sent the Alarm signal (SIGALRM), and so on.
Two signals can be used to stop a process, SIGTERM and SIGKILL. SIGTERM is the polite way to kill a process; the process can catch the signal, realize that you want it to shut down, close any log files it may have open, and generally finish whatever it is doing at the time before shutting down. In some cases a process may even ignore SIGTERM if it is in the middle of some task that can not be interrupted.
SIGKILL can not be ignored by a process. This is the “I do not care what you are doing, stop right now” signal. If you send SIGKILL to a process then FreeBSD will stop that process there and then[1].
The other signals you might want to use are SIGHUP, SIGUSR1, and SIGUSR2. These are general purpose signals, and different applications will do different things when they are sent.
Suppose that you have changed your web server's configuration file--you would like to tell the web server to re-read its configuration. You could stop and restart httpd, but this would result in a brief outage period on your web server, which may be undesirable. Most daemons are written to respond to the SIGHUP signal by re-reading their configuration file. So instead of killing and restarting httpd you would send it the SIGHUP signal. Because there is no standard way to respond to these signals, different daemons will have different behavior, so be sure and read the documentation for the daemon in question.
Signals are sent using the kill(1) command, as this example shows.
Sending a Signal to a Process
This example shows how to send a signal to inetd(8). The inetd configuration file is /etc/inetd.conf, and inetd will re-read this configuration file when it is sent SIGHUP.
1.      Find the process ID of the process you want to send the signal to. Do this using ps(1) and grep(1). The grep(1) command is used to search through output, looking for the string you specify. This command is run as a normal user, and inetd(8) is run as root, so the ax options must be given to ps(1).
2.         % ps -ax | grep inetd
3.           198  ??  IWs    0:00.00 inetd -wW
So the inetd(8) PID is 198. In some cases the grep inetd command might also appear in this output. This is because of the way ps(1) has to find the list of running processes.
4.      Use kill(1) to send the signal. Because inetd(8) is being run by root you must use su(1) to become root first.
5.         % su
6.         Password:
7.         # /bin/kill -s HUP 198
In common with most UNIX® commands, kill(1) will not print any output if it is successful. If you send a signal to a process that you do not own then you will see “kill: PID: Operation not permitted”. If you mistype the PID you will either send the signal to the wrong process, which could be bad, or, if you are lucky, you will have sent the signal to a PID that is not currently in use, and you will see “kill: PID: No such process”.
Why Use /bin/kill?: Many shells provide the kill command as a built in command; that is, the shell will send the signal directly, rather than running /bin/kill. This can be very useful, but different shells have a different syntax for specifying the name of the signal to send. Rather than try to learn all of them, it can be simpler just to use the /bin/kill ... command directly.
Sending other signals is very similar, just substitute TERM or KILL in the command line as necessary.
Important: Killing random process on the system can be a bad idea. In particular, init(8), process ID 1, is very special. Running /bin/kill -s KILL 1 is a quick way to shutdown your system. Always double check the arguments you run kill(1) with before you press Return.

Notes

Not quite true--there are a few things that can not be interrupted. For example, if the process is trying to read from a file that is on another computer on the network, and the other computer has gone away for some reason (been turned off, or the network has a fault), then the process is said to be “uninterruptible”. Eventually the process will time out, typically after two minutes. As soon as this time out occurs the process will be killed.

hells

In FreeBSD, a lot of everyday work is done in a command line interface called a shell. A shell's main job is to take commands from the input channel and execute them. A lot of shells also have built in functions to help with everyday tasks such as file management, file globbing, command line editing, command macros, and environment variables. FreeBSD comes with a set of shells, such as sh, the Bourne Shell, and tcsh, the improved C-shell. Many other shells are available from the FreeBSD Ports Collection, such as zsh and bash.
Which shell do you use? It is really a matter of taste. If you are a C programmer you might feel more comfortable with a C-like shell such as tcsh. If you have come from Linux or are new to a UNIX® command line interface you might try bash. The point is that each shell has unique properties that may or may not work with your preferred working environment, and that you have a choice of what shell to use.
One common feature in a shell is filename completion. Given the typing of the first few letters of a command or filename, you can usually have the shell automatically complete the rest of the command or filename by hitting the Tab key on the keyboard. Here is an example. Suppose you have two files called foobar and foo.bar. You want to delete foo.bar. So what you would type on the keyboard is: rm fo[Tab].[Tab].
The shell would print out rm foo[BEEP].bar.
The [BEEP] is the console bell, which is the shell telling me it was unable to totally complete the filename because there is more than one match. Both foobar and foo.bar start with fo, but it was able to complete to foo. If you type in ., then hit Tab again, the shell would be able to fill in the rest of the filename for you.
Another feature of the shell is the use of environment variables. Environment variables are a variable/key pair stored in the shell's environment space. This space can be read by any program invoked by the shell, and thus contains a lot of program configuration. Here is a list of common environment variables and what they mean:
Variable
Description
USER
Current logged in user's name.
PATH
Colon-separated list of directories to search for binaries.
DISPLAY
Network name of the X11 display to connect to, if available.
SHELL
The current shell.
TERM
The name of the user's type of terminal. Used to determine the capabilities of the terminal.
TERMCAP
Database entry of the terminal escape codes to perform various terminal functions.
OSTYPE
Type of operating system. e.g., FreeBSD.
MACHTYPE
The CPU architecture that the system is running on.
EDITOR
The user's preferred text editor.
PAGER
The user's preferred text pager.
MANPATH
Colon-separated list of directories to search for manual pages.
Setting an environment variable differs somewhat from shell to shell. For example, in the C-Style shells such as tcsh and csh, you would use setenv to set environment variables. Under Bourne shells such as sh and bash, you would use export to set your current environment variables. For example, to set or modify the EDITOR environment variable, under csh or tcsh a command like this would set EDITOR to /usr/local/bin/emacs:
% setenv EDITOR /usr/local/bin/emacs
Under Bourne shells:
% export EDITOR="/usr/local/bin/emacs"
You can also make most shells expand the environment variable by placing a $ character in front of it on the command line. For example, echo $TERM would print out whatever $TERM is set to, because the shell expands $TERM and passes it on to echo.
Shells treat a lot of special characters, called meta-characters as special representations of data. The most common one is the * character, which represents any number of characters in a filename. These special meta-characters can be used to do filename globbing. For example, typing in echo * is almost the same as typing in ls because the shell takes all the files that match * and puts them on the command line for echo to see.
To prevent the shell from interpreting these special characters, they can be escaped from the shell by putting a backslash (\) character in front of them. echo $TERM prints whatever your terminal is set to. echo \$TERM prints $TERM as is.

3.9.1 Changing Your Shell

The easiest way to change your shell is to use the chsh command. Running chsh will place you into the editor that is in your EDITOR environment variable; if it is not set, you will be placed in vi. Change the “Shell:” line accordingly.
You can also give chsh the -s option; this will set your shell for you, without requiring you to enter an editor. For example, if you wanted to change your shell to bash, the following should do the trick:
% chsh -s /usr/local/bin/bash
Note: The shell that you wish to use must be present in the /etc/shells file. If you have installed a shell from the ports collection, then this should have been done for you already. If you installed the shell by hand, you must do this.
For example, if you installed bash by hand and placed it into /usr/local/bin, you would want to:
# echo "/usr/local/bin/bash" >> /etc/shells
Then rerun chsh.

Text Editors

A lot of configuration in FreeBSD is done by editing text files. Because of this, it would be a good idea to become familiar with a text editor. FreeBSD comes with a few as part of the base system, and many more are available in the Ports Collection.
The easiest and simplest editor to learn is an editor called ee, which stands for easy editor. To start ee, one would type at the command line ee filename where filename is the name of the file to be edited. For example, to edit /etc/rc.conf, type in ee /etc/rc.conf. Once inside of ee, all of the commands for manipulating the editor's functions are listed at the top of the display. The caret ^ character represents the Ctrl key on the keyboard, so ^e expands to the key combination Ctrl+e. To leave ee, hit the Esc key, then choose leave editor. The editor will prompt you to save any changes if the file has been modified.
FreeBSD also comes with more powerful text editors such as vi as part of the base system, while other editors, like Emacs and vim, are part of the FreeBSD Ports Collection (editors/emacs and editors/vim). These editors offer much more functionality and power at the expense of being a little more complicated to learn. However if you plan on doing a lot of text editing, learning a more powerful editor such as vim or Emacs will save you much more time in the long run.
Many applications which modify files or require typed input will automatically open a text editor. To alter the default editor used, set the EDITOR environment variable. See shells section for more details.

Devices and Device Nodes

A device is a term used mostly for hardware-related activities in a system, including disks, printers, graphics cards, and keyboards. When FreeBSD boots, the majority of what FreeBSD displays are devices being detected. You can look through the boot messages again by viewing /var/run/dmesg.boot.
For example, acd0 is the first IDE CDROM drive, while kbd0 represents the keyboard.
Most of these devices in a UNIX® operating system must be accessed through special files called device nodes, which are located in the /dev directory.

3.11.1 Creating Device Nodes

When adding a new device to your system, or compiling in support for additional devices, new device nodes must be created.

3.11.1.1 DEVFS (DEVice File System)

The device file system, or DEVFS, provides access to kernel's device namespace in the global file system namespace. Instead of having to create and modify device nodes, DEVFS maintains this particular file system for you.
See the devfs(5) manual page for more information.

Binary Formats

To understand why FreeBSD uses the elf(5) format, you must first know a little about the three currently “dominant” executable formats for UNIX®:
·         a.out(5)
The oldest and “classic” UNIX object format. It uses a short and compact header with a magic number at the beginning that is often used to characterize the format (see a.out(5) for more details). It contains three loaded segments: .text, .data, and .bss plus a symbol table and a string table.
·         COFF
The SVR3 object format. The header now comprises a section table, so you can have more than just .text, .data, and .bss sections.
·         elf(5)
The successor to COFF, featuring multiple sections and 32-bit or 64-bit possible values. One major drawback: ELF was also designed with the assumption that there would be only one ABI per system architecture. That assumption is actually quite incorrect, and not even in the commercial SYSV world (which has at least three ABIs: SVR4, Solaris, SCO) does it hold true.
FreeBSD tries to work around this problem somewhat by providing a utility for branding a known ELF executable with information about the ABI it is compliant with. See the manual page for brandelf(1) for more information.
FreeBSD comes from the “classic” camp and used the a.out(5) format, a technology tried and proven through many generations of BSD releases, until the beginning of the 3.X branch. Though it was possible to build and run native ELF binaries (and kernels) on a FreeBSD system for some time before that, FreeBSD initially resisted the “push” to switch to ELF as the default format. Why? Well, when the Linux camp made their painful transition to ELF, it was not so much to flee the a.out executable format as it was their inflexible jump-table based shared library mechanism, which made the construction of shared libraries very difficult for vendors and developers alike. Since the ELF tools available offered a solution to the shared library problem and were generally seen as “the way forward” anyway, the migration cost was accepted as necessary and the transition made. FreeBSD's shared library mechanism is based more closely on Sun's SunOS™ style shared library mechanism and, as such, is very easy to use.
So, why are there so many different formats?
Back in the dim, dark past, there was simple hardware. This simple hardware supported a simple, small system. a.out was completely adequate for the job of representing binaries on this simple system (a PDP-11). As people ported UNIX from this simple system, they retained the a.out format because it was sufficient for the early ports of UNIX to architectures like the Motorola 68k, VAXen, etc.
Then some bright hardware engineer decided that if he could force software to do some sleazy tricks, then he would be able to shave a few gates off the design and allow his CPU core to run faster. While it was made to work with this new kind of hardware (known these days as RISC), a.out was ill-suited for this hardware, so many formats were developed to get to a better performance from this hardware than the limited, simple a.out format could offer. Things like COFF, ECOFF, and a few obscure others were invented and their limitations explored before things seemed to settle on ELF.
In addition, program sizes were getting huge and disks (and physical memory) were still relatively small so the concept of a shared library was born. The VM system also became more sophisticated. While each one of these advancements was done using the a.out format, its usefulness was stretched more and more with each new feature. In addition, people wanted to dynamically load things at run time, or to junk parts of their program after the init code had run to save in core memory and swap space. Languages became more sophisticated and people wanted code called before main automatically. Lots of hacks were done to the a.out format to allow all of these things to happen, and they basically worked for a time. In time, a.out was not up to handling all these problems without an ever increasing overhead in code and complexity. While ELF solved many of these problems, it would be painful to switch from the system that basically worked. So ELF had to wait until it was more painful to remain with a.out than it was to migrate to ELF.
However, as time passed, the build tools that FreeBSD derived their build tools from (the assembler and loader especially) evolved in two parallel trees. The FreeBSD tree added shared libraries and fixed some bugs. The GNU folks that originally wrote these programs rewrote them and added simpler support for building cross compilers, plugging in different formats at will, and so on. Since many people wanted to build cross compilers targeting FreeBSD, they were out of luck since the older sources that FreeBSD had for as and ld were not up to the task. The new GNU tools chain (binutils) does support cross compiling, ELF, shared libraries, C++ extensions, etc. In addition, many vendors are releasing ELF binaries, and it is a good thing for FreeBSD to run them.
ELF is more expressive than a.out and allows more extensibility in the base system. The ELF tools are better maintained, and offer cross compilation support, which is important to many people. ELF may be a little slower than a.out, but trying to measure it can be difficult. There are also numerous details that are different between the two in how they map pages, handle init code, etc. None of these are very important, but they are differences. In time support for a.out will be moved out of the GENERIC kernel, and eventually removed from the kernel once the need to run legacy a.out programs is past.

For More Information

3.13.1 Manual Pages

The most comprehensive documentation on FreeBSD is in the form of manual pages. Nearly every program on the system comes with a short reference manual explaining the basic operation and various arguments. These manuals can be viewed with the man command. Use of the man command is simple:
% man command
command is the name of the command you wish to learn about. For example, to learn more about ls command type:
% man ls
The online manual is divided up into numbered sections:
1.      User commands.
2.      System calls and error numbers.
3.      Functions in the C libraries.
4.      Device drivers.
5.      File formats.
6.      Games and other diversions.
7.      Miscellaneous information.
8.      System maintenance and operation commands.
9.      Kernel developers.
In some cases, the same topic may appear in more than one section of the online manual. For example, there is a chmod user command and a chmod() system call. In this case, you can tell the man command which one you want by specifying the section:
% man 1 chmod
This will display the manual page for the user command chmod. References to a particular section of the online manual are traditionally placed in parenthesis in written documentation, so chmod(1) refers to the chmod user command and chmod(2) refers to the system call.
This is fine if you know the name of the command and simply wish to know how to use it, but what if you cannot recall the command name? You can use man to search for keywords in the command descriptions by using the -k switch:
% man -k mail
With this command you will be presented with a list of commands that have the keyword “mail” in their descriptions. This is actually functionally equivalent to using the apropos command.
So, you are looking at all those fancy commands in /usr/bin but do not have the faintest idea what most of them actually do? Simply do:
% cd /usr/bin
% man -f *
or
% cd /usr/bin
% whatis *
which does the same thing.

3.13.2 GNU Info Files

FreeBSD includes many applications and utilities produced by the Free Software Foundation (FSF). In addition to manual pages, these programs come with more extensive hypertext documents called info files which can be viewed with the info command or, if you installed emacs, the info mode of emacs.
To use the info(1) command, simply type:
% info
For a brief introduction, type h. For a quick command reference, type ?.

Synopsis

FreeBSD is bundled with a rich collection of system tools as part of the base system. However, there is only so much one can do before needing to install an additional third-party application to get real work done. FreeBSD provides two complementary technologies for installing third-party software on your system: the FreeBSD Ports Collection (for installing from source), and packages (for installing from pre-built binaries). Either method may be used to install the newest version of your favorite applications from local media or straight off the network.
After reading this chapter, you will know:
·         How to install third-party binary software packages.
·         How to build third-party software from source by using the ports collection.
·         How to remove previously installed packages or ports.
·         How to override the default values that the ports collection uses.
·         How to find the appropriate software package.
·         How to upgrade your applications.

Overview of Software Installation

If you have used a UNIX® system before you will know that the typical procedure for installing third-party software goes something like this:
1.      Download the software, which might be distributed in source code format, or as a binary.
2.      Unpack the software from its distribution format (typically a tarball compressed with compress(1), gzip(1), or bzip2(1)).
3.      Locate the documentation (perhaps an INSTALL or README file, or some files in a doc/ subdirectory) and read up on how to install the software.
4.      If the software was distributed in source format, compile it. This may involve editing a Makefile, or running a configure script, and other work.
5.      Test and install the software.
And that is only if everything goes well. If you are installing a software package that was not deliberately ported to FreeBSD you may even have to go in and edit the code to make it work properly.
Should you want to, you can continue to install software the “traditional” way with FreeBSD. However, FreeBSD provides two technologies which can save you a lot of effort: packages and ports. At the time of writing, over 19,000 third-party applications have been made available in this way.
For any given application, the FreeBSD package for that application is a single file which you must download. The package contains pre-compiled copies of all the commands for the application, as well as any configuration files or documentation. A downloaded package file can be manipulated with FreeBSD package management commands, such as pkg_add(1), pkg_delete(1), pkg_info(1), and so on. Installing a new application can be carried out with a single command.
A FreeBSD port for an application is a collection of files designed to automate the process of compiling an application from source code.
Remember that there are a number of steps you would normally carry out if you compiled a program yourself (downloading, unpacking, patching, compiling, installing). The files that make up a port contain all the necessary information to allow the system to do this for you. You run a handful of simple commands and the source code for the application is automatically downloaded, extracted, patched, compiled, and installed for you.
In fact, the ports system can also be used to generate packages which can later be manipulated with pkg_add and the other package management commands that will be introduced shortly.
Both packages and ports understand dependencies. Suppose you want to install an application that depends on a specific library being installed. Both the application and the library have been made available as FreeBSD ports and packages. If you use the pkg_add command or the ports system to add the application, both will notice that the library has not been installed, and automatically install the library first.
Given that the two technologies are quite similar, you might be wondering why FreeBSD bothers with both. Packages and ports both have their own strengths, and which one you use will depend on your own preference.
Package Benefits
·         A compressed package tarball is typically smaller than the compressed tarball containing the source code for the application.
·         Packages do not require any additional compilation. For large applications, such as Mozilla, KDE, or GNOME this can be important, particularly if you are on a slow system.
·         Packages do not require any understanding of the process involved in compiling software on FreeBSD.
Ports Benefits
·         Packages are normally compiled with conservative options, because they have to run on the maximum number of systems. By installing from the port, you can tweak the compilation options to (for example) generate code that is specific to a Pentium 4 or Athlon processor.
·         Some applications have compile-time options relating to what they can and cannot do. For example, Apache can be configured with a wide variety of different built-in options. By building from the port you do not have to accept the default options, and can set them yourself.
In some cases, multiple packages will exist for the same application to specify certain settings. For example, Ghostscript is available as a ghostscript package and a ghostscript-nox11 package, depending on whether or not you have installed an X11 server. This sort of rough tweaking is possible with packages, but rapidly becomes impossible if an application has more than one or two different compile-time options.
·         The licensing conditions of some software distributions forbid binary distribution. They must be distributed as source code.
·         Some people do not trust binary distributions. At least with source code, you can (in theory) read through it and look for potential problems yourself.
·         If you have local patches, you will need the source in order to apply them.
·         Some people like having code around, so they can read it if they get bored, hack it, borrow from it (license permitting, of course), and so on.
To keep track of updated ports, subscribe to the FreeBSD ports mailing list and the FreeBSD ports bugs mailing list.
Warning: Before installing any application, you should check http://vuxml.freebsd.org/ for security issues related to your application.
You can also install ports-mgmt/portaudit which will automatically check all installed applications for known vulnerabilities; a check will be also performed before any port build. Meanwhile, you can use the command portaudit -F -a after you have installed some packages.
The remainder of this chapter will explain how to use packages and ports to install and manage third-party software on FreeBSD.

Finding Your Application

Before you can install any applications you need to know what you want, and what the application is called.
FreeBSD's list of available applications is growing all the time. Fortunately, there are a number of ways to find what you want:
·         The FreeBSD web site maintains an up-to-date searchable list of all the available applications, at http://www.FreeBSD.org/ports/. The ports are divided into categories, and you may either search for an application by name (if you know it), or see all the applications available in a category.
·         Dan Langille maintains FreshPorts, at http://www.FreshPorts.org/. FreshPorts tracks changes to the applications in the ports tree as they happen, allows you to “watch” one or more ports, and can send you email when they are updated.
·         If you do not know the name of the application you want, try using a site like FreshMeat (http://www.freshmeat.net/) to find an application, then check back at the FreeBSD site to see if the application has been ported yet.
·         If you know the exact name of the port, but just need to find out which category it is in, you can use the whereis(1) command. Simply type whereis file, where file is the program you want to install. If it is found on your system, you will be told where it is, as follows:
·                # whereis lsof
·                lsof: /usr/ports/sysutils/lsof
This tells us that lsof (a system utility) can be found in the /usr/ports/sysutils/lsof directory.
·         Additionally, you can use a simple echo(1) statement to find where a port exists in the ports tree. For example:
·                # echo /usr/ports/*/*lsof*
·                /usr/ports/sysutils/lsof
Note that this will return any matched files downloaded into the /usr/ports/distfiles directory.
·         Yet another way to find a particular port is by using the Ports Collection's built-in search mechanism. To use the search feature, you will need to be in the /usr/ports directory. Once in that directory, run make search name=program-name where program-name is the name of the program you want to find. For example, if you were looking for lsof:
·                # cd /usr/ports
·                # make search name=lsof
·                Port:   lsof-4.56.4
·                Path:   /usr/ports/sysutils/lsof
·                Info:   Lists information about open files (similar to fstat(1))
·                Maint:  obrien@FreeBSD.org
·                Index:  sysutils
·                B-deps: 
·                R-deps:
The part of the output you want to pay particular attention to is the “Path:” line, since that tells you where to find the port. The other information provided is not needed in order to install the port, so it will not be covered here.
For more in-depth searching you can also use make search key=string where string is some text to search for. This searches port names, comments, descriptions and dependencies and can be used to find ports which relate to a particular subject if you do not know the name of the program you are looking for.
In both of these cases, the search string is case-insensitive. Searching for “LSOF” will yield the same results as searching for “lsof”.

Using the Packages System

Contributed by Chern Lee.
There are several different tools used to manage packages on FreeBSD:
·         The sysinstall utility can be invoked on a running system to install, delete, and list available and installed packages. For more information, see Section 2.10.11.
·         The package management command line tools, which are the subject of the rest of this section.

4.4.1 Installing a Package

You can use the pkg_add(1) utility to install a FreeBSD software package from a local file or from a server on the network.
Example 4-1. Downloading a Package Manually and Installing It Locally
# ftp -a ftp2.FreeBSD.org
Connected to ftp2.FreeBSD.org.
220 ftp2.FreeBSD.org FTP server (Version 6.00LS) ready.
331 Guest login ok, send your email address as password.
230-
230-     This machine is in Vienna, VA, USA, hosted by Verio.
230-         Questions? E-mail freebsd@vienna.verio.net.
230-
230-
230 Guest login ok, access restrictions apply.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /pub/FreeBSD/ports/packages/sysutils/
250 CWD command successful.
ftp> get lsof-4.56.4.tgz
local: lsof-4.56.4.tgz remote: lsof-4.56.4.tgz
200 PORT command successful.
150 Opening BINARY mode data connection for 'lsof-4.56.4.tgz' (92375 bytes).
100% |**************************************************| 92375       00:00 ETA
226 Transfer complete.
92375 bytes received in 5.60 seconds (16.11 KB/s)
ftp> exit
# pkg_add lsof-4.56.4.tgz
If you do not have a source of local packages (such as a FreeBSD CD-ROM set) then it will probably be easier to use the -r option to pkg_add(1). This will cause the utility to automatically determine the correct object format and release and then fetch and install the package from an FTP site.
# pkg_add -r lsof
The example above would download the correct package and add it without any further user intervention. If you want to specify an alternative FreeBSD Packages Mirror, instead of the main distribution site, you have to set the PACKAGESITE environment variable accordingly, to override the default settings. pkg_add(1) uses fetch(3) to download the files, which honors various environment variables, including FTP_PASSIVE_MODE, FTP_PROXY, and FTP_PASSWORD. You may need to set one or more of these if you are behind a firewall, or need to use an FTP/HTTP proxy. See fetch(3) for the complete list. Note that in the example above lsof is used instead of lsof-4.56.4. When the remote fetching feature is used, the version number of the package must be removed. pkg_add(1) will automatically fetch the latest version of the application.
Note: pkg_add(1) will download the latest version of your application if you are using FreeBSD-CURRENT or FreeBSD-STABLE. If you run a -RELEASE version, it will grab the version of the package that was built with your release. It is possible to change this behavior by overriding PACKAGESITE. For example, if you run a FreeBSD 5.4-RELEASE system, by default pkg_add(1) will try to fetch packages from ftp://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-5.4-release/Latest/. If you want to force pkg_add(1) to download FreeBSD 5-STABLE packages, set PACKAGESITE to ftp://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-5-stable/Latest/.
Package files are distributed in .tgz and .tbz formats. You can find them at ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/packages/, or on the FreeBSD CD-ROM distribution. Every CD on the FreeBSD 4-CD set (and the PowerPak, etc.) contains packages in the /packages directory. The layout of the packages is similar to that of the /usr/ports tree. Each category has its own directory, and every package can be found within the All directory.
The directory structure of the package system matches the ports layout; they work with each other to form the entire package/port system.

4.4.2 Managing Packages

pkg_info(1) is a utility that lists and describes the various packages installed.
# pkg_info
cvsup-16.1          A general network file distribution system optimized for CV
docbook-1.2         Meta-port for the different versions of the DocBook DTD
...
pkg_version(1) is a utility that summarizes the versions of all installed packages. It compares the package version to the current version found in the ports tree.
# pkg_version
cvsup                       =
docbook                     =
...
The symbols in the second column indicate the relative age of the installed version and the version available in the local ports tree.
Symbol
Meaning
=
The version of the installed package matches the one found in the local ports tree.
The installed version is older than the one available in the ports tree.
The installed version is newer than the one found in the local ports tree. (The local ports tree is probably out of date.)
?
The installed package cannot be found in the ports index. (This can happen, for instance, if an installed port is removed from the Ports Collection or renamed.)
*
There are multiple versions of the package.
!
The installed package exists in the index but for some reason, pkg_version was unable to compare the version number of the installed package with the corresponding entry in the index.

4.4.3 Deleting a Package

To remove a previously installed software package, use the pkg_delete(1) utility.
# pkg_delete xchat-1.7.1
Note that pkg_delete(1) requires the full package name and number; the above command would not work if xchat was given instead of xchat-1.7.1. It is, however, easy to use pkg_version(1) to find the version of the installed package. You could instead simply use a wildcard:
# pkg_delete xchat\*
in this case, all packages whose names start with xchat will be deleted.

4.4.4 Miscellaneous

All package information is stored within the /var/db/pkg directory. The installed file list and descriptions of each package can be found within files in this directory.

Using the Ports Collection

The following sections provide basic instructions on using the Ports Collection to install or remove programs from your system. The detailed description of available make targets and environment variables is available in ports(7).

4.5.1 Obtaining the Ports Collection

Before you can install ports, you must first obtain the Ports Collection--which is essentially a set of Makefiles, patches, and description files placed in /usr/ports.
When installing your FreeBSD system, sysinstall asked if you would like to install the Ports Collection. If you chose no, you can follow these instructions to obtain the ports collection:
CVSup Method
This is a quick method for getting and keeping your copy of the Ports Collection up to date using CVSup protocol. If you want to learn more about CVSup, see Using CVSup.
Note: The implementation of CVSup protocol included with the FreeBSD system is called csup. It first appeared in FreeBSD 6.2. Users of older FreeBSD releases can install it via the net/csup port/package.
Make sure /usr/ports is empty before you run csup for the first time! If you already have the Ports Collection present, obtained from another source, csup will not prune removed patch files.
1.      Run csup:
2.         # csup -L 2 -h cvsup.FreeBSD.org /usr/share/examples/cvsup/ports-supfile
Change cvsup.FreeBSD.org to a CVSup server near you. See CVSup Mirrors (Section A.6.7) for a complete listing of mirror sites.
Note: One may want to use his own ports-supfile, for example to avoid the need of passing the CVSup server on the command line.
1.      In this case, as root, copy /usr/share/examples/cvsup/ports-supfile to a new location, such as /root or your home directory.
2.      Edit ports-supfile.
3.      Change CHANGE_THIS.FreeBSD.org to a CVSup server near you. See CVSup Mirrors (Section A.6.7) for a complete listing of mirror sites.
4.      And now to run csup, use the following:
5.                      # csup -L 2 /root/ports-supfile
3.      Running the csup(1) command later will download and apply all the recent changes to your Ports Collection, except actually rebuilding the ports for your own system.
Portsnap Method
Portsnap is an alternative system for distributing the Ports Collection. Please refer to Using Portsnap for a detailed description of all Portsnap features.
1.      Download a compressed snapshot of the Ports Collection into /var/db/portsnap. You can disconnect from the Internet after this step, if you wish.
2.         # portsnap fetch
3.      If you are running Portsnap for the first time, extract the snapshot into /usr/ports:
4.         # portsnap extract
If you already have a populated /usr/ports and you are just updating, run the following command instead:
# portsnap update
Sysinstall Method
This method involves using sysinstall to install the Ports Collection from the installation media. Note that the old copy of Ports Collection from the date of the release will be installed. If you have Internet access, you should always use one of the methods mentioned above.
1.      As root, run sysinstall (/stand/sysinstall in FreeBSD versions older than 5.2) as shown below:
2.         # sysinstall
3.      Scroll down and select Configure, press Enter.
4.      Scroll down and select Distributions, press Enter.
5.      Scroll down to ports, press Space.
6.      Scroll up to Exit, press Enter.
7.      Select your desired installation media, such as CDROM, FTP, and so on.
8.      Scroll up to Exit and press Enter.
9.      Press X to exit sysinstall.

4.5.2 Installing Ports

The first thing that should be explained when it comes to the Ports Collection is what is actually meant by a “skeleton”. In a nutshell, a port skeleton is a minimal set of files that tell your FreeBSD system how to cleanly compile and install a program. Each port skeleton includes:
·         A Makefile. The Makefile contains various statements that specify how the application should be compiled and where it should be installed on your system.
·         A distinfo file. This file contains information about the files that must be downloaded to build the port, and their checksums (using md5(1) and sha256(1)), to verify that files have not been corrupted during the download.
·         A files directory. This directory contains patches to make the program compile and install on your FreeBSD system. Patches are basically small files that specify changes to particular files. They are in plain text format, and basically say “Remove line 10” or “Change line 26 to this ...”. Patches are also known as “diffs” because they are generated by the diff(1) program.
This directory may also contain other files used to build the port.
·         A pkg-descr file. This is a more detailed, often multiple-line, description of the program.
·         A pkg-plist file. This is a list of all the files that will be installed by the port. It also tells the ports system what files to remove upon deinstallation.
Some ports have other files, such as pkg-message. The ports system uses these files to handle special situations. If you want more details on these files, and on ports in general, check out the FreeBSD Porter's Handbook.
The port includes instructions on how to build source code, but does not include the actual source code. You can get the source code from a CD-ROM or from the Internet. Source code is distributed in whatever manner the software author desires. Frequently this is a tarred and gzipped file, but it might be compressed with some other tool or even uncompressed. The program source code, whatever form it comes in, is called a “distfile”. The two methods for installing a FreeBSD port are described below.
Note: You must be logged in as root to install ports.
Warning: Before installing any port, you should be sure to have an up-to-date Ports Collection and you should check http://vuxml.freebsd.org/ for security issues related to your port.
A security vulnerabilities check can be automatically done by portaudit before any new application installation. This tool can be found in the Ports Collection (ports-mgmt/portaudit). Consider running portaudit -F before installing a new port, to fetch the current vulnerabilities database. A security audit and an update of the database will be performed during the daily security system check. For more information read the portaudit(1) and periodic(8) manual pages.
The Ports Collection makes an assumption that you have a working Internet connection. If you do not, you will need to put a copy of the distfile into /usr/ports/distfiles manually.
To begin, change to the directory for the port you want to install:
# cd /usr/ports/sysutils/lsof
Once inside the lsof directory, you will see the port skeleton. The next step is to compile, or “build”, the port. This is done by simply typing make at the prompt. Once you have done so, you should see something like this:
# make
>> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/.
>> Attempting to fetch from ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/.
===>  Extracting for lsof-4.57
...
[extraction output snipped]
...
>> Checksum OK for lsof_4.57D.freebsd.tar.gz.
===>  Patching for lsof-4.57
===>  Applying FreeBSD patches for lsof-4.57
===>  Configuring for lsof-4.57
...
[configure output snipped]
...
===>  Building for lsof-4.57
...
[compilation output snipped]
...
#
Notice that once the compile is complete you are returned to your prompt. The next step is to install the port. In order to install it, you simply need to tack one word onto the make command, and that word is install:
# make install
===>  Installing for lsof-4.57
...
[installation output snipped]
...
===>   Generating temporary packing list
===>   Compressing manual pages for lsof-4.57
===>   Registering installation for lsof-4.57
===>  SECURITY NOTE:
      This port has installed the following binaries which execute with
      increased privileges.
#
Once you are returned to your prompt, you should be able to run the application you just installed. Since lsof is a program that runs with increased privileges, a security warning is shown. During the building and installation of ports, you should take heed of any other warnings that may appear.
It is a good idea to delete the working subdirectory, which contains all the temporary files used during compilation. Not only does it consume valuable disk space, but it would also cause problems later when upgrading to the newer version of the port.
# make clean
===>  Cleaning for lsof-4.57
#
Note: You can save two extra steps by just running make install clean instead of make, make install and make clean as three separate steps.
Note: Some shells keep a cache of the commands that are available in the directories listed in the PATH environment variable, to speed up lookup operations for the executable file of these commands. If you are using one of these shells, you might have to use the rehash command after installing a port, before the newly installed commands can be used. This command will work for shells like tcsh. Use the hash -r command for shells like sh. Look at the documentation for your shell for more information.
Some third-party DVD-ROM products such as the FreeBSD Toolkit from the FreeBSD Mall contain distfiles. They can be used with the Ports Collection. Mount the DVD-ROM on /cdrom. If you use a different mount point, set CD_MOUNTPTS make variable. The needed distfiles will be automatically used if they are present on the disk.
Note: Please be aware that the licenses of a few ports do not allow for inclusion on the CD-ROM. This could be because a registration form needs to be filled out before downloading or redistribution is not allowed, or for another reason. If you wish to install a port not included on the CD-ROM, you will need to be online in order to do so.
The ports system uses fetch(1) to download the files, which honors various environment variables, including FTP_PASSIVE_MODE, FTP_PROXY, and FTP_PASSWORD. You may need to set one or more of these if you are behind a firewall, or need to use an FTP/HTTP proxy. See fetch(3) for the complete list.
For users which cannot be connected all the time, the make fetch option is provided. Just run this command at the top level directory (/usr/ports) and the required files will be downloaded for you. This command will also work in the lower level categories, for example: /usr/ports/net. Note that if a port depends on libraries or other ports this will not fetch the distfiles of those ports too. Replace fetch with fetch-recursive if you want to fetch all the dependencies of a port too.
Note: You can build all the ports in a category or as a whole by running make in the top level directory, just like the aforementioned make fetch method. This is dangerous, however, as some ports cannot co-exist. In other cases, some ports can install two different files with the same filename.
In some rare cases, users may need to acquire the tarballs from a site other than the MASTER_SITES (the location where files are downloaded from). You can override the MASTER_SITES option with the following command:
# cd /usr/ports/directory
# make MASTER_SITE_OVERRIDE= \
ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/ fetch
In this example we change the MASTER_SITES option to ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/.
Note: Some ports allow (or even require) you to provide build options which can enable/disable parts of the application which are unneeded, certain security options, and other customizations. A few which come to mind are www/mozilla, security/gpgme, and mail/sylpheed-claws. A message will be displayed when options such as these are available.

4.5.2.1 Overriding the Default Ports Directories

Sometimes it is useful (or mandatory) to use a different working and target directory. The WRKDIRPREFIX and PREFIX variables can override the default directories. For example:
# make WRKDIRPREFIX=/usr/home/example/ports install
will compile the port in /usr/home/example/ports and install everything under /usr/local.
# make PREFIX=/usr/home/example/local install
will compile it in /usr/ports and install it in /usr/home/example/local.
And of course,
# make WRKDIRPREFIX=../ports PREFIX=../local install
will combine the two (it is too long to completely write on this page, but it should give you the general idea).
Alternatively, these variables can also be set as part of your environment. Read the manual page for your shell for instructions on doing so.

4.5.2.2 Dealing with imake

Some ports that use imake (a part of the X Window System) do not work well with PREFIX, and will insist on installing under /usr/X11R6. Similarly, some Perl ports ignore PREFIX and install in the Perl tree. Making these ports respect PREFIX is a difficult or impossible job.

4.5.2.3 Reconfiguring Ports

When building certain ports, you may be presented with a ncurses-based menu from which you can select certain build options. It is not uncommon for users to wish to revisit this menu to add, remove, or change these options after a port has been built. There are many ways to do this. One option is to go into the directory containing the port and type make config, which will simply present the menu again with the same options selected. Another option is to use make showconfig, which will show you all the configuration options for the port. Yet another option is to execute make rmconfig which will remove all selected options and allow you to start over. All of these options, and others, are explained in great detail in the manual page for ports(7).

4.5.3 Removing Installed Ports

Now that you know how to install ports, you are probably wondering how to remove them, just in case you install one and later on decide that you installed the wrong port. We will remove our previous example (which was lsof for those of you not paying attention). Ports are being removed exactly the same as the packages (discussed in the Packages section), using the pkg_delete(1) command:
# pkg_delete lsof-4.57

4.5.4 Upgrading Ports

First, list outdated ports that have a newer version available in the Ports Collection with the pkg_version(1) command:
# pkg_version -v

4.5.4.1 /usr/ports/UPDATING

Once you have updated your Ports Collection, before attempting a port upgrade, you should check /usr/ports/UPDATING. This file describes various issues and additional steps users may encounter and need to perform when updating a port, including such things as file format changes, changes in locations of configuration files, or other such incompatibilities with previous versions.
If UPDATING contradicts something you read here, UPDATING takes precedence.

4.5.4.2 Upgrading Ports using Portupgrade

The portupgrade utility is designed to easily upgrade installed ports. It is available from the ports-mgmt/portupgrade port. Install it like any other port, using the make install clean command:
# cd /usr/ports/ports-mgmt/portupgrade
# make install clean
Scan the list of installed ports with the pkgdb -F command and fix all the inconsistencies it reports. It is a good idea to do this regularly, before every upgrade.
When you run portupgrade -a, portupgrade will begin to upgrade all the outdated ports installed on your system. Use the -i flag if you want to be asked for confirmation of every individual upgrade.
# portupgrade -ai
If you want to upgrade only a certain application, not all available ports, use portupgrade pkgname. Include the -R flag if portupgrade should first upgrade all the ports required by the given application.
# portupgrade -R firefox
To use packages instead of ports for installation, provide -P flag. With this option portupgrade searches the local directories listed in PKG_PATH, or fetches packages from remote site if it is not found locally. If packages can not be found locally or fetched remotely, portupgrade will use ports. To avoid using ports, specify -PP.
# portupgrade -PP gnome2
To just fetch distfiles (or packages, if -P is specified) without building or installing anything, use -F. For further information see portupgrade(1).

4.5.4.3 Upgrading Ports using Portmanager

Portmanager is another utility for easy upgrading of installed ports. It is available from the ports-mgmt/portmanager port:
# cd /usr/ports/ports-mgmt/portmanager
# make install clean
All the installed ports can be upgraded using this simple command:
# portmanager -u
You can add the -ui flag to get asked for confirmation of every step Portmanager will perform. Portmanager can also be used to install new ports on the system. Unlike the usual make install clean command, it will upgrade all the dependencies prior to building and installing the selected port.
# portmanager x11/gnome2
If there are any problems regarding the dependencies for the selected port, you can use Portmanager to rebuild all of them in the correct order. Once finished, the problematic port will be rebuilt too.
# portmanager graphics/gimp -f
For further information see portmanager(1).

4.5.4.4 Upgrading Ports using Portmaster

Portmaster is another utility for upgrading installed ports. Portmaster was designed make use of the tools found in the “base” system (it does not depend upon other ports) and uses the information in /var/db/pkg/ to determine which ports to upgrade. It is available from the ports-mgmt/portmaster port:
# cd /usr/ports/ports-mgmt/portmaster
# make install clean
Portmaster groups ports into four categories:
·         Root ports (no dependencies, not depended on)
·         Trunk ports (no dependencies, are depended on)
·         Branch ports (have dependencies, are depended on)
·         Leaf ports (have dependencies, not depended on)
You can list all the installed ports and search for updates using the -L option:
# portmaster -L
===>>> Root ports (No dependencies, not depended on)
===>>> ispell-3.2.06_18
===>>> screen-4.0.3
        ===>>> New version available: screen-4.0.3_1
===>>> tcpflow-0.21_1
===>>> 7 root ports
...
===>>> Branch ports (Have dependencies, are depended on)
===>>> apache-2.2.3
        ===>>> New version available: apache-2.2.8
...
===>>> Leaf ports (Have dependencies, not depended on)
===>>> automake-1.9.6_2
===>>> bash-3.1.17
        ===>>> New version available: bash-3.2.33
...
===>>> 32 leaf ports
 
===>>> 137 total installed ports
        ===>>> 83 have new versions available
All the installed ports can be upgraded using this simple command:
# portmaster -a
Note: By default, Portmaster will make a backup package before deleting the existing port. If the installation of the new version is successful, Portmaster will delete the backup. Using the -b will instruct Portmaster not to automatically delete the backup. Adding the -i option will start Portmaster in interactive mode, prompting you before upgrading each port.
If you encounter errors during the upgrade process, you can use the -f option to upgrade/rebuild all ports:
# portmaster -af
You can also use Portmaster to install new ports on the system, upgrading all dependencies before building and installing the new port:
# portmaster shells/bash
Please see portmaster(8) for more information.

4.5.5 Ports and Disk Space

Using the Ports Collection will use up disk space over time. After building and installing software from the ports, you should always remember to clean up the temporary work directories using the make clean command. You can sweep the whole Ports Collection with the following command:
# portsclean -C
You will accumulate a lot of old source distribution files in the distfiles directory over time. You can remove them by hand, or you can use the following command to delete all the distfiles that are no longer referenced by any ports:
# portsclean -D
Or to remove all distfiles not referenced by any port currently installed on your system:
# portsclean -DD
Note: The portsclean utility is part of the portupgrade suite.
Do not forget to remove the installed ports once you no longer need them. A nice tool to help automate this task is available from the ports-mgmt/pkg_cutleaves port.

Post-installation Activities

After installing a new application you will normally want to read any documentation it may have included, edit any configuration files that are required, ensure that the application starts at boot time (if it is a daemon), and so on.
The exact steps you need to take to configure each application will obviously be different. However, if you have just installed a new application and are wondering “What now?” these tips might help:
·         Use pkg_info(1) to find out which files were installed, and where. For example, if you have just installed FooPackage version 1.0.0, then this command
·                # pkg_info -L foopackage-1.0.0 | less
will show all the files installed by the package. Pay special attention to files in man/ directories, which will be manual pages, etc/ directories, which will be configuration files, and doc/, which will be more comprehensive documentation.
If you are not sure which version of the application was just installed, a command like this
# pkg_info | grep -i foopackage
will find all the installed packages that have foopackage in the package name. Replace foopackage in your command line as necessary.
·         Once you have identified where the application's manual pages have been installed, review them using man(1). Similarly, look over the sample configuration files, and any additional documentation that may have been provided.
·         If the application has a web site, check it for additional documentation, frequently asked questions, and so forth. If you are not sure of the web site address it may be listed in the output from
·                # pkg_info foopackage-1.0.0
A WWW: line, if present, should provide a URL for the application's web site.
·         Ports that should start at boot (such as Internet servers) will usually install a sample script in /usr/local/etc/rc.d. You should review this script for correctness and edit or rename it if needed. See Starting Services for more information.

Dealing with Broken Ports

If you come across a port that does not work for you, there are a few things you can do, including:
1.      Find out if there is a fix pending for the port in the Problem Report database. If so, you may be able to use the proposed fix.
2.      Ask the maintainer of the port for help. Type make maintainer or read the Makefile to find the maintainer's email address. Remember to include the name and version of the port (send the $FreeBSD: line from the Makefile) and the output leading up to the error when you email the maintainer.
Note: Some ports are not maintained by an individual but instead by a mailing list. Many, but not all, of these addresses look like <freebsd-listname@FreeBSD.org>. Please take this into account when phrasing your questions.
In particular, ports shown as maintained by <ports@FreeBSD.org> are actually not maintained by anyone. Fixes and support, if any, come from the general community who subscribe to that mailing list. More volunteers are always needed!
If you do not get a response, you can use send-pr(1) to submit a bug report (see Writing FreeBSD Problem Reports).
3.      Fix it! The Porter's Handbook includes detailed information on the “Ports” infrastructure so that you can fix the occasional broken port or even submit your own!
4.      Grab the package from an FTP site near you. The “master” package collection is on ftp.FreeBSD.org in the packages directory, but be sure to check your local mirror first! These are more likely to work than trying to compile from source and are a lot faster as well. Use the pkg_add(1) program to install the package on your system.

he X Window System

Table of Contents
Updated for X.Org's X11 server by Ken Tom and Marc Fonvieille.

5.1 Synopsis

FreeBSD uses X11 to provide users with a powerful graphical user interface. X11 is a freely available version of the X Window System that is implemented in both Xorg and XFree86 (and other software packages not discussed here). FreeBSD versions up to and including FreeBSD 5.2.1-RELEASE will find the default installation to be XFree86, the X11 server released by The XFree86 Project, Inc. As of FreeBSD 5.3-RELEASE, the default and official flavor of X11 was changed to Xorg, the X11 server developed by the X.Org Foundation under a license very similar to the one used by FreeBSD. Commercial X servers for FreeBSD are also available.
This chapter will cover the installation and configuration of X11 with emphasis on Xorg 7.3 release. For information about configuring XFree86 (i.e. on older releases of FreeBSD where XFree86 was the default X11 distribution) or previous releases of Xorg, it is always possible to refer to archived versions of the FreeBSD Handbook at http://docs.FreeBSD.org/doc/.
For more information on the video hardware that X11 supports, check the Xorg web site.
After reading this chapter, you will know:
·         The various components of the X Window System, and how they interoperate.
·         How to install and configure X11.
·         How to install and use different window managers.
·         How to use TrueType® fonts in X11.
·         How to set up your system for graphical logins (XDM).
Before reading this chapter, you should:
·         Know how to install additional third-party software (Chapter 4).

Understanding X

Using X for the first time can be somewhat of a shock to someone familiar with other graphical environments, such as Microsoft® Windows® or Mac OS®.
While it is not necessary to understand all of the details of various X components and how they interact, some basic knowledge makes it possible to take advantage of X's strengths.

5.2.1 Why X?

X is not the first window system written for UNIX®, but it is the most popular of them. X's original development team had worked on another window system prior to writing X. That system's name was “W” (for “Window”). X was just the next letter in the Roman alphabet.
X can be called “X”, “X Window System”, “X11”, and a number of other terms. You may find that using the term “X Windows” to describe X11 can be offensive to some people; for a bit more insight on this, see X(7).

5.2.2 The X Client/Server Model

X was designed from the beginning to be network-centric, and adopts a “client-server” model.
In the X model, the “X server” runs on the computer that has the keyboard, monitor, and mouse attached. The server's responsibility includes tasks such as managing the display, handling input from the keyboard and mouse, and other input or output devices (i.e. a “tablet” can be used as an input device, and a video projector may be an alternative output device). Each X application (such as XTerm, or Netscape®) is a “client”. A client sends messages to the server such as “Please draw a window at these coordinates”, and the server sends back messages such as “The user just clicked on the OK button”.
In a home or small office environment, the X server and the X clients commonly run on the same computer. However, it is perfectly possible to run the X server on a less powerful desktop computer, and run X applications (the clients) on, say, the powerful and expensive machine that serves the office. In this scenario the communication between the X client and server takes place over the network.
This confuses some people, because the X terminology is exactly backward to what they expect. They expect the “X server” to be the big powerful machine down the hall, and the “X client” to be the machine on their desk.
It is important to remember that the X server is the machine with the monitor and keyboard, and the X clients are the programs that display the windows.
There is nothing in the protocol that forces the client and server machines to be running the same operating system, or even to be running on the same type of computer. It is certainly possible to run an X server on Microsoft Windows or Apple's Mac OS, and there are various free and commercial applications available that do exactly that.

5.2.3 The Window Manager

The X design philosophy is much like the UNIX design philosophy, “tools, not policy”. This means that X does not try to dictate how a task is to be accomplished. Instead, tools are provided to the user, and it is the user's responsibility to decide how to use those tools.
This philosophy extends to X not dictating what windows should look like on screen, how to move them around with the mouse, what keystrokes should be used to move between windows (i.e., Alt+Tab, in the case of Microsoft Windows), what the title bars on each window should look like, whether or not they have close buttons on them, and so on.
Instead, X delegates this responsibility to an application called a “Window Manager”. There are dozens of window managers available for X: AfterStep, Blackbox, ctwm, Enlightenment, fvwm, Sawfish, twm, Window Maker, and more. Each of these window managers provides a different look and feel; some of them support “virtual desktops”; some of them allow customized keystrokes to manage the desktop; some have a “Start” button or similar device; some are “themeable”, allowing a complete change of look-and-feel by applying a new theme. These window managers, and many more, are available in the x11-wm category of the Ports Collection.
In addition, the KDE and GNOME desktop environments both have their own window managers which integrate with the desktop.
Each window manager also has a different configuration mechanism; some expect configuration file written by hand, others feature GUI tools for most of the configuration tasks; at least one (Sawfish) has a configuration file written in a dialect of the Lisp language.
Focus Policy: Another feature the window manager is responsible for is the mouse “focus policy”. Every windowing system needs some means of choosing a window to be actively receiving keystrokes, and should visibly indicate which window is active as well.
A familiar focus policy is called “click-to-focus”. This is the model utilized by Microsoft Windows, in which a window becomes active upon receiving a mouse click.
X does not support any particular focus policy. Instead, the window manager controls which window has the focus at any one time. Different window managers will support different focus methods. All of them support click to focus, and the majority of them support several others.
The most popular focus policies are:
focus-follows-mouse
The window that is under the mouse pointer is the window that has the focus. This may not necessarily be the window that is on top of all the other windows. The focus is changed by pointing at another window, there is no need to click in it as well.
sloppy-focus
This policy is a small extension to focus-follows-mouse. With focus-follows-mouse, if the mouse is moved over the root window (or background) then no window has the focus, and keystrokes are simply lost. With sloppy-focus, focus is only changed when the cursor enters a new window, and not when exiting the current window.
click-to-focus
The active window is selected by mouse click. The window may then be “raised”, and appear in front of all other windows. All keystrokes will now be directed to this window, even if the cursor is moved to another window.
Many window managers support other policies, as well as variations on these. Be sure to consult the documentation for the window manager itself.

5.2.4 Widgets

The X approach of providing tools and not policy extends to the widgets seen on screen in each application.
“Widget” is a term for all the items in the user interface that can be clicked or manipulated in some way; buttons, check boxes, radio buttons, icons, lists, and so on. Microsoft Windows calls these “controls”.
Microsoft Windows and Apple's Mac OS both have a very rigid widget policy. Application developers are supposed to ensure that their applications share a common look and feel. With X, it was not considered sensible to mandate a particular graphical style, or set of widgets to adhere to.
As a result, do not expect X applications to have a common look and feel. There are several popular widget sets and variations, including the original Athena widget set from MIT, Motif® (on which the widget set in Microsoft Windows was modeled, all bevelled edges and three shades of grey), OpenLook, and others.
Most newer X applications today will use a modern-looking widget set, either Qt, used by KDE, or GTK+, used by the GNOME project. In this respect, there is some convergence in look-and-feel of the UNIX desktop, which certainly makes things easier for the

nstalling X11

Xorg is the default X11 implementation for FreeBSD. Xorg is the X server of the open source X Window System implementation released by the X.Org Foundation. Xorg is based on the code of XFree86™ 4.4RC2 and X11R6.6. The version of Xorg currently available in the FreeBSD Ports Collection is 7.3.
To build and install Xorg from the Ports Collection:
# cd /usr/ports/x11/xorg
# make install clean
Note: To build Xorg in its entirety, be sure to have at least 4 GB of free space available.
Alternatively, X11 can be installed directly from packages. Binary packages to use with pkg_add(1) tool are also available for X11. When the remote fetching feature of pkg_add(1) is used, the version number of the package must be removed. pkg_add(1) will automatically fetch the latest version of the application.
So to fetch and install the package of Xorg, simply type:
# pkg_add -r xorg
Note: The examples above will install the complete X11 distribution including the servers, clients, fonts etc. Separate packages and ports of X11 are also available.
The rest of this chapter will explain how to configure X11, and how to set up a productive desktop environment.

X11 Configuration

Contributed by Christopher Shumway.

5.4.1 Before Starting

Before configuration of X11 the following information about the target system is needed:
·         Monitor specifications
·         Video Adapter chipset
·         Video Adapter memory
The specifications for the monitor are used by X11 to determine the resolution and refresh rate to run at. These specifications can usually be obtained from the documentation that came with the monitor or from the manufacturer's website. There are two ranges of numbers that are needed, the horizontal scan rate and the vertical synchronization rate.
The video adapter's chipset defines what driver module X11 uses to talk to the graphics hardware. With most chipsets, this can be automatically determined, but it is still useful to know in case the automatic detection does not work correctly.
Video memory on the graphic adapter determines the resolution and color depth which the system can run at. This is important to know so the user knows the limitations of the system.

5.4.2 Configuring X11

As of version 7.3, Xorg can often work without any configuration file by simply typing at prompt:
% startx
If this does not work, or if the default configuration is not acceptable, then X11 must be configured manually.
Note: Desktop environments like GNOME, KDE or XFce have tools allowing the user to easily set the screen parameters such as the resolution. So if the default configuration is not acceptable and you planned to install a desktop environment then just continue with the installation of the desktop environment and use the appropriate screen settings tool.
Configuration of X11 is a multi-step process. The first step is to build an initial configuration file. As the super user, simply run:
# Xorg -configure
This will generate an X11 configuration skeleton file in the /root directory called xorg.conf.new (whether you su(1) or do a direct login affects the inherited supervisor $HOME directory variable). The X11 program will attempt to probe the graphics hardware on the system and write a configuration file to load the proper drivers for the detected hardware on the target system.
The next step is to test the existing configuration to verify that Xorg can work with the graphics hardware on the target system. To perform this task, type:
# Xorg -config xorg.conf.new
If a black and grey grid and an X mouse cursor appear, the configuration was successful. To exit the test, just press Ctrl+Alt+Backspace simultaneously.
Note: If the mouse does not work, you will need to first configure it before proceeding. See Section 2.10.10 in the FreeBSD install chapter.
Next, tune the xorg.conf.new configuration file to taste. Open the file in a text editor such as emacs(1) or ee(1). First, add the frequencies for the target system's monitor. These are usually expressed as a horizontal and vertical synchronization rate. These values are added to the xorg.conf.new file under the "Monitor" section:
Section "Monitor"
        Identifier   "Monitor0"
        VendorName   "Monitor Vendor"
        ModelName    "Monitor Model"
        HorizSync    30-107
        VertRefresh  48-120
EndSection
The HorizSync and VertRefresh keywords may be missing in the configuration file. If they are, they need to be added, with the correct horizontal synchronization rate placed after the HorizSync keyword and the vertical synchronization rate after the VertRefresh keyword. In the example above the target monitor's rates were entered.
X allows DPMS (Energy Star) features to be used with capable monitors. The xset(1) program controls the time-outs and can force standby, suspend, or off modes. If you wish to enable DPMS features for your monitor, you must add the following line to the monitor section:
        Option       "DPMS"
While the xorg.conf.new configuration file is still open in an editor, select the default resolution and color depth desired. This is defined in the "Screen" section:
Section "Screen"
        Identifier "Screen0"
        Device     "Card0"
        Monitor    "Monitor0"
        DefaultDepth 24
        SubSection "Display"
                Viewport  0 0
                Depth     24
                Modes     "1024x768"
        EndSubSection
EndSection
The DefaultDepth keyword describes the color depth to run at by default. This can be overridden with the -depth command line switch to Xorg(1). The Modes keyword describes the resolution to run at for the given color depth. Note that only VESA standard modes are supported as defined by the target system's graphics hardware. In the example above, the default color depth is twenty-four bits per pixel. At this color depth, the accepted resolution is 1024 by 768 pixels.
Finally, write the configuration file and test it using the test mode given above.
Note: One of the tools available to assist you during troubleshooting process are the X11 log files, which contain information on each device that the X11 server attaches to. Xorg log file names are in the format of /var/log/Xorg.0.log. The exact name of the log can vary from Xorg.0.log to Xorg.8.log and so forth.
If all is well, the configuration file needs to be installed in a common location where Xorg(1) can find it. This is typically /etc/X11/xorg.conf or /usr/local/etc/X11/xorg.conf.
# cp xorg.conf.new /etc/X11/xorg.conf
The X11 configuration process is now complete. Xorg may be now started with the startx(1) utility. The X11 server may also be started with the use of xdm(1).
Note: There is also a graphical configuration tool, xorgcfg(1), which comes with the X11 distribution. It allows you to interactively define your configuration by choosing the appropriate drivers and settings. This program can be invoked from the console, by typing the command xorgcfg -textmode. For more details, refer to the xorgcfg(1) manual page.
Alternatively, there is also a tool called xorgconfig(1). This program is a console utility that is less user friendly, but it may work in situations where the other tools do not.

5.4.3 Advanced Configuration Topics

5.4.3.1 Configuration with Intel® i810 Graphics Chipsets

Configuration with Intel® i810 integrated chipsets requires the agpgart AGP programming interface for X11 to drive the card. See the agp(4) driver manual page for more information.
This will allow configuration of the hardware as any other graphics board. Note on systems without the agp(4) driver compiled in the kernel, trying to load the module with kldload(8) will not work. This driver has to be in the kernel at boot time through being compiled in or using /boot/loader.conf.

5.4.3.2 Adding a Widescreen Flatpanel to the Mix

This section assumes a bit of advanced configuration knowledge. If attempts to use the standard configuration tools above have not resulted in a working configuration, there is information enough in the log files to be of use in getting the setup working. Use of a text editor will be necessary.
Current widescreen (WSXGA, WSXGA+, WUXGA, WXGA, WXGA+, et.al.) formats support 16:10 and 10:9 formats or aspect ratios that can be problematic. Examples of some common screen resolutions for 16:10 aspect ratios are:
·         2560x1600
·         1920x1200
·         1680x1050
·         1440x900
·         1280x800
At some point, it will be as easy as adding one of these resolutions as a possible Mode in the Section "Screen" as such:
Section "Screen"
Identifier "Screen0"
Device     "Card0"
Monitor    "Monitor0"
DefaultDepth 24
SubSection "Display"
    Viewport  0 0
    Depth     24
    Modes     "1680x1050"
EndSubSection
EndSection
Xorg is smart enough to pull the resolution information from the widescreen via I2C/DDC information so it knows what the monitor can handle as far as frequencies and resolutions.
If those ModeLines do not exist in the drivers, one might need to give Xorg a little hint. Using /var/log/Xorg.0.log one can extract enough information to manually create a ModeLine that will work. Simply look for information resembling this:
(II) MGA(0): Supported additional Video Mode:
(II) MGA(0): clock: 146.2 MHz   Image Size:  433 x 271 mm
(II) MGA(0): h_active: 1680  h_sync: 1784  h_sync_end 1960 h_blank_end 2240 h_border: 0
(II) MGA(0): v_active: 1050  v_sync: 1053  v_sync_end 1059 v_blanking: 1089 v_border: 0
(II) MGA(0): Ranges: V min: 48  V max: 85 Hz, H min: 30  H max: 94 kHz, PixClock max 170 MHz
This information is called EDID information. Creating a ModeLine from this is just a matter of putting the numbers in the correct order:
ModeLine <name> <clock> <4 horiz. timings> <4 vert. timings>
So that the ModeLine in Section "Monitor" for this example would look like this:
Section "Monitor"
Identifier      "Monitor1"
VendorName      "Bigname"
ModelName       "BestModel"
ModeLine        "1680x1050" 146.2 1680 1784 1960 2240 1050 1053 1059 1089
Option          "DPMS"
EndSection
Now having completed these simple editing steps, X should start on your new widescreen monitor.

Using Fonts in X11

Contributed by Murray Stokely.

5.5.1 Type1 Fonts

The default fonts that ship with X11 are less than ideal for typical desktop publishing applications. Large presentation fonts show up jagged and unprofessional looking, and small fonts in Netscape® are almost completely unintelligible. However, there are several free, high quality Type1 (PostScript®) fonts available which can be readily used with X11. For instance, the URW font collection (x11-fonts/urwfonts) includes high quality versions of standard type1 fonts (Times Roman®, Helvetica®, Palatino® and others). The Freefonts collection (x11-fonts/freefonts) includes many more fonts, but most of them are intended for use in graphics software such as the Gimp, and are not complete enough to serve as screen fonts. In addition, X11 can be configured to use TrueType® fonts with a minimum of effort. For more details on this, see the X(7) manual page or the section on TrueType fonts.
To install the above Type1 font collections from the ports collection, run the following commands:
# cd /usr/ports/x11-fonts/urwfonts
# make install clean
And likewise with the freefont or other collections. To have the X server detect these fonts, add an appropriate line to the X server configuration file (/etc/X11/xorg.conf), which reads:
FontPath "/usr/local/lib/X11/fonts/URW/"
Alternatively, at the command line in the X session run:
% xset fp+ /usr/local/lib/X11/fonts/URW
% xset fp rehash
This will work but will be lost when the X session is closed, unless it is added to the startup file (~/.xinitrc for a normal startx session, or ~/.xsession when logging in through a graphical login manager like XDM). A third way is to use the new /usr/local/etc/fonts/local.conf file: see the section on anti-aliasing.

5.5.2 TrueType® Fonts

Xorg has built in support for rendering TrueType fonts. There are two different modules that can enable this functionality. The freetype module is used in this example because it is more consistent with the other font rendering back-ends. To enable the freetype module just add the following line to the "Module" section of the /etc/X11/xorg.conf file.
Load  "freetype"
Now make a directory for the TrueType fonts (for example, /usr/local/lib/X11/fonts/TrueType) and copy all of the TrueType fonts into this directory. Keep in mind that TrueType fonts cannot be directly taken from a Macintosh®; they must be in UNIX®/MS-DOS®/Windows® format for use by X11. Once the files have been copied into this directory, use ttmkfdir to create a fonts.dir file, so that the X font renderer knows that these new files have been installed. ttmkfdir is available from the FreeBSD Ports Collection as x11-fonts/ttmkfdir.
# cd /usr/local/lib/X11/fonts/TrueType
# ttmkfdir -o fonts.dir
Now add the TrueType directory to the font path. This is just the same as described above for Type1 fonts, that is, use
% xset fp+ /usr/local/lib/X11/fonts/TrueType
% xset fp rehash
or add a FontPath line to the xorg.conf file.
That's it. Now Netscape, Gimp, StarOffice, and all of the other X applications should now recognize the installed TrueType fonts. Extremely small fonts (as with text in a high resolution display on a web page) and extremely large fonts (within StarOffice) will look much better now.

5.5.3 Anti-Aliased Fonts

Updated by Joe Marcus Clarke.
Anti-aliasing has been available in X11 since XFree86 4.0.2. However, font configuration was cumbersome before the introduction of XFree86 4.3.0. Beginning with XFree86 4.3.0, all fonts in X11 that are found in /usr/local/lib/X11/fonts/ and ~/.fonts/ are automatically made available for anti-aliasing to Xft-aware applications. Not all applications are Xft-aware, but many have received Xft support. Examples of Xft-aware applications include Qt 2.3 and higher (the toolkit for the KDE desktop), GTK+ 2.0 and higher (the toolkit for the GNOME desktop), and Mozilla 1.2 and higher.
In order to control which fonts are anti-aliased, or to configure anti-aliasing properties, create (or edit, if it already exists) the file /usr/local/etc/fonts/local.conf. Several advanced features of the Xft font system can be tuned using this file; this section describes only some simple possibilities. For more details, please see fonts-conf(5).
This file must be in XML format. Pay careful attention to case, and make sure all tags are properly closed. The file begins with the usual XML header followed by a DOCTYPE definition, and then the <fontconfig> tag:
      <?xml version="1.0"?>
      <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
      <fontconfig>
   
As previously stated, all fonts in /usr/local/lib/X11/fonts/ as well as ~/.fonts/ are already made available to Xft-aware applications. If you wish to add another directory outside of these two directory trees, add a line similar to the following to /usr/local/etc/fonts/local.conf:
<dir>/path/to/my/fonts</dir>
After adding new fonts, and especially new font directories, you should run the following command to rebuild the font caches:
# fc-cache -f
Anti-aliasing makes borders slightly fuzzy, which makes very small text more readable and removes “staircases” from large text, but can cause eyestrain if applied to normal text. To exclude font sizes smaller than 14 point from anti-aliasing, include these lines:
        <match target="font">
            <test name="size" compare="less">
                <double>14</double>
            </test>
            <edit name="antialias" mode="assign">
                <bool>false</bool>
            </edit>
        </match>
        <match target="font">
            <test name="pixelsize" compare="less" qual="any">
                <double>14</double>
            </test>
            <edit mode="assign" name="antialias">
                <bool>false</bool>
            </edit>
        </match>
Spacing for some monospaced fonts may also be inappropriate with anti-aliasing. This seems to be an issue with KDE, in particular. One possible fix for this is to force the spacing for such fonts to be 100. Add the following lines:
       <match target="pattern" name="family">
           <test qual="any" name="family">
               <string>fixed</string>
           </test>
           <edit name="family" mode="assign">
               <string>mono</string>
           </edit>
        </match>
        <match target="pattern" name="family">
            <test qual="any" name="family">
                <string>console</string>
            </test>
            <edit name="family" mode="assign">
                <string>mono</string>
            </edit>
        </match>
(this aliases the other common names for fixed fonts as "mono"), and then add:
         <match target="pattern" name="family">
             <test qual="any" name="family">
                 <string>mono</string>
             </test>
             <edit name="spacing" mode="assign">
                 <int>100</int>
             </edit>
         </match>     
Certain fonts, such as Helvetica, may have a problem when anti-aliased. Usually this manifests itself as a font that seems cut in half vertically. At worst, it may cause applications such as Mozilla to crash. To avoid this, consider adding the following to local.conf:
         <match target="pattern" name="family">
             <test qual="any" name="family">
                 <string>Helvetica</string>
             </test>
             <edit name="family" mode="assign">
                 <string>sans-serif</string>
             </edit>
         </match>       
Once you have finished editing local.conf make sure you end the file with the </fontconfig> tag. Not doing this will cause your changes to be ignored.
The default font set that comes with X11 is not very desirable when it comes to anti-aliasing. A much better set of default fonts can be found in the x11-fonts/bitstream-vera port. This port will install a /usr/local/etc/fonts/local.conf file if one does not exist already. If the file does exist, the port will create a /usr/local/etc/fonts/local.conf-vera file. Merge the contents of this file into /usr/local/etc/fonts/local.conf, and the Bitstream fonts will automatically replace the default X11 Serif, Sans Serif, and Monospaced fonts.
Finally, users can add their own settings via their personal .fonts.conf files. To do this, each user should simply create a ~/.fonts.conf. This file must also be in XML format.
One last point: with an LCD screen, sub-pixel sampling may be desired. This basically treats the (horizontally separated) red, green and blue components separately to improve the horizontal resolution; the results can be dramatic. To enable this, add the line somewhere in the local.conf file:
         <match target="font">
             <test qual="all" name="rgba">
                 <const>unknown</const>
             </test>
             <edit name="rgba" mode="assign">
                 <const>rgb</const>
             </edit>
         </match>
      
Note: Depending on the sort of display, rgb may need to be changed to bgr, vrgb or vbgr: experiment and see which works best.
Anti-aliasing should be enabled the next time the X server is started. However, programs must know how to take advantage of it. At present, the Qt toolkit does, so the entire KDE environment can use anti-aliased fonts. GTK+ and GNOME can also be made to use anti-aliasing via the “Font” capplet (see Section 5.7.1.3 for details). By default, Mozilla 1.2 and greater will automatically use anti-aliasing. To disable this, rebuild Mozilla with the -DWITHOUT_XFT flag.

The X Display Manager

Contributed by Seth Kingsley.

5.6.1 Overview

The X Display Manager (XDM) is an optional part of the X Window System that is used for login session management. This is useful for several types of situations, including minimal “X Terminals”, desktops, and large network display servers. Since the X Window System is network and protocol independent, there are a wide variety of possible configurations for running X clients and servers on different machines connected by a network. XDM provides a graphical interface for choosing which display server to connect to, and entering authorization information such as a login and password combination.
Think of XDM as providing the same functionality to the user as the getty(8) utility (see Section 26.3.2 for details). That is, it performs system logins to the display being connected to and then runs a session manager on behalf of the user (usually an X window manager). XDM then waits for this program to exit, signaling that the user is done and should be logged out of the display. At this point, XDM can display the login and display chooser screens for the next user to login.

5.6.2 Using XDM

The XDM daemon program is located in /usr/local/bin/xdm. This program can be run at any time as root and it will start managing the X display on the local machine. If XDM is to be run every time the machine boots up, a convenient way to do this is by adding an entry to /etc/ttys. For more information about the format and usage of this file, see Section 26.3.2.1. There is a line in the default /etc/ttys file for running the XDM daemon on a virtual terminal:
ttyv8   "/usr/local/bin/xdm -nodaemon"  xterm   off secure
By default this entry is disabled; in order to enable it change field 5 from off to on and restart init(8) using the directions in Section 26.3.2.2. The first field, the name of the terminal this program will manage, is ttyv8. This means that XDM will start running on the 9th virtual terminal.

5.6.3 Configuring XDM

The XDM configuration directory is located in /usr/local/lib/X11/xdm. In this directory there are several files used to change the behavior and appearance of XDM. Typically these files will be found:
File
Description
Xaccess
Client authorization ruleset.
Xresources
Default X resource values.
Xservers
List of remote and local displays to manage.
Xsession
Default session script for logins.
Xsetup_*
Script to launch applications before the login interface.
xdm-config
Global configuration for all displays running on this machine.
xdm-errors
Errors generated by the server program.
xdm-pid
The process ID of the currently running XDM.
Also in this directory are a few scripts and programs used to set up the desktop when XDM is running. The purpose of each of these files will be briefly described. The exact syntax and usage of all of these files is described in xdm(1).
The default configuration is a simple rectangular login window with the hostname of the machine displayed at the top in a large font and “Login:” and “Password:” prompts below. This is a good starting point for changing the look and feel of XDM screens.

5.6.3.1 Xaccess

The protocol for connecting to XDM-controlled displays is called the X Display Manager Connection Protocol (XDMCP). This file is a ruleset for controlling XDMCP connections from remote machines. It is ignored unless the xdm-config is changed to listen for remote connections. By default, it does not allow any clients to connect.

5.6.3.2 Xresources

This is an application-defaults file for the display chooser and login screens. In it, the appearance of the login program can be modified. The format is identical to the app-defaults file described in the X11 documentation.

5.6.3.3 Xservers

This is a list of the remote displays the chooser should provide as choices.

5.6.3.4 Xsession

This is the default session script for XDM to run after a user has logged in. Normally each user will have a customized session script in ~/.xsession that overrides this script.

5.6.3.5 Xsetup_*

These will be run automatically before displaying the chooser or login interfaces. There is a script for each display being used, named Xsetup_ followed by the local display number (for instance Xsetup_0). Typically these scripts will run one or two programs in the background such as xconsole.

5.6.3.6 xdm-config

This contains settings in the form of app-defaults that are applicable to every display that this installation manages.

5.6.3.7 xdm-errors

This contains the output of the X servers that XDM is trying to run. If a display that XDM is trying to start hangs for some reason, this is a good place to look for error messages. These messages are also written to the user's ~/.xsession-errors file on a per-session basis.

5.6.4 Running a Network Display Server

In order for other clients to connect to the display server, you must edit the access control rules and enable the connection listener. By default these are set to conservative values. To make XDM listen for connections, first comment out a line in the xdm-config file:
! SECURITY: do not listen for XDMCP or Chooser requests
! Comment out this line if you want to manage X terminals with xdm
DisplayManager.requestPort:     0
and then restart XDM. Remember that comments in app-defaults files begin with a “!” character, not the usual “#”. More strict access controls may be desired -- look at the example entries in Xaccess, and refer to the xdm(1) manual page for further information.

5.6.5 Replacements for XDM

Several replacements for the default XDM program exist. One of them, kdm (bundled with KDE) is described later in this chapter. The kdm display manager offers many visual improvements and cosmetic frills, as well as the functionality to allow users to choose their window manager of choice at login time.

Desktop Environments

Contributed by Valentino Vaschetto.
This section describes the different desktop environments available for X on FreeBSD. A “desktop environment” can mean anything ranging from a simple window manager to a complete suite of desktop applications, such as KDE or GNOME.

5.7.1 GNOME

5.7.1.1 About GNOME

GNOME is a user-friendly desktop environment that enables users to easily use and configure their computers. GNOME includes a panel (for starting applications and displaying status), a desktop (where data and applications can be placed), a set of standard desktop tools and applications, and a set of conventions that make it easy for applications to cooperate and be consistent with each other. Users of other operating systems or environments should feel right at home using the powerful graphics-driven environment that GNOME provides. More information regarding GNOME on FreeBSD can be found on the FreeBSD GNOME Project's web site. The web site also contains fairly comprehensive FAQs about installing, configuring, and managing GNOME.

5.7.1.2 Installing GNOME

The software can be easily installed from a package or the Ports Collection:
To install the GNOME package from the network, simply type:
# pkg_add -r gnome2
To build GNOME from source, use the ports tree:
# cd /usr/ports/x11/gnome2
# make install clean
Once GNOME is installed, the X server must be told to start GNOME instead of a default window manager.
The easiest way to start GNOME is with GDM, the GNOME Display Manager. GDM, which is installed as a part of the GNOME desktop (but is disabled by default), can be enabled by adding gdm_enable="YES" to /etc/rc.conf. Once you have rebooted, GNOME will start automatically once you log in -- no further configuration is necessary.
GNOME may also be started from the command-line by properly configuring a file named .xinitrc. If a custom .xinitrc is already in place, simply replace the line that starts the current window manager with one that starts /usr/local/bin/gnome-session instead. If nothing special has been done to the configuration file, then it is enough simply to type:
% echo "/usr/local/bin/gnome-session" > ~/.xinitrc
Next, type startx, and the GNOME desktop environment will be started.
Note: If an older display manager, like XDM, is being used, this will not work. Instead, create an executable .xsession file with the same command in it. To do this, edit the file and replace the existing window manager command with /usr/local/bin/gnome-session:
% echo "#!/bin/sh" > ~/.xsession
% echo "/usr/local/bin/gnome-session" >> ~/.xsession
% chmod +x ~/.xsession
Yet another option is to configure the display manager to allow choosing the window manager at login time; the section on KDE details explains how to do this for kdm, the display manager of KDE.

5.7.1.3 Anti-aliased Fonts with GNOME

X11 supports anti-aliasing via its “RENDER” extension. GTK+ 2.0 and greater (the toolkit used by GNOME) can make use of this functionality. Configuring anti-aliasing is described in Section 5.5.3. So, with up-to-date software, anti-aliasing is possible within the GNOME desktop. Just go to Applications->Desktop Preferences->Font, and select either Best shapes, Best contrast, or Subpixel smoothing (LCDs). For a GTK+ application that is not part of the GNOME desktop, set the environment variable GDK_USE_XFT to 1 before launching the program.

5.7.2 KDE

5.7.2.1 About KDE

KDE is an easy to use contemporary desktop environment. Some of the things that KDE brings to the user are:
·         A beautiful contemporary desktop
·         A desktop exhibiting complete network transparency
·         An integrated help system allowing for convenient, consistent access to help on the use of the KDE desktop and its applications
·         Consistent look and feel of all KDE applications
·         Standardized menu and toolbars, keybindings, color-schemes, etc.
·         Internationalization: KDE is available in more than 40 languages
·         Centralized, consistent, dialog-driven desktop configuration
·         A great number of useful KDE applications
KDE comes with a web browser called Konqueror, which is a solid competitor to other existing web browsers on UNIX® systems. More information on KDE can be found on the KDE website. For FreeBSD specific information and resources on KDE, consult the KDE on FreeBSD team's website.
There are two versions of KDE available on FreeBSD. Version 3 has been around for a long time, and is very mature. Version 4, the next generation, is also available in the Ports Collection. They can even be installed side by side.

5.7.2.2 Installing KDE

Just as with GNOME or any other desktop environment, the software can be easily installed from a package or the Ports Collection:
To install the KDE3 package from the network, simply type:
# pkg_add -r kde
To install the KDE4 package from the network, simply type:
# pkg_add -r kde4
pkg_add(1) will automatically fetch the latest version of the application.
To build KDE3 from source, use the ports tree:
# cd /usr/ports/x11/kde3
# make install clean
To build KDE4 from source, use the ports tree:
# cd /usr/ports/x11/kde4
# make install clean
After KDE has been installed, the X server must be told to launch this application instead of the default window manager. This is accomplished by editing the .xinitrc file:
For KDE3:
% echo "exec startkde" > ~/.xinitrc
For KDE4:
% echo "exec /usr/local/kde4/bin/startkde" > ~/.xinitrc
Now, whenever the X Window System is invoked with startx, KDE will be the desktop.
If a display manager such as XDM is being used, the configuration is slightly different. Edit the .xsession file instead. Instructions for kdm are described later in this chapter.

5.7.3 More Details on KDE

Now that KDE is installed on the system, most things can be discovered through the help pages, or just by pointing and clicking at various menus. Windows® or Mac® users will feel quite at home.
The best reference for KDE is the on-line documentation. KDE comes with its own web browser, Konqueror, dozens of useful applications, and extensive documentation. The remainder of this section discusses the technical items that are difficult to learn by random exploration.

5.7.3.1 The KDE Display Manager

An administrator of a multi-user system may wish to have a graphical login screen to welcome users. XDM can be used, as described earlier. However, KDE includes an alternative, kdm, which is designed to look more attractive and include more login-time options. In particular, users can easily choose (via a menu) which desktop environment (KDE, GNOME, or something else) to run after logging on.
To enable kdm, the ttyv8 entry in /etc/ttys has to be adapted. The line should look as follows:
For KDE3:
ttyv8 "/usr/local/bin/kdm -nodaemon" xterm on secure
For KDE4:
ttyv8 "/usr/local/kde4/bin/kdm -nodaemon" xterm on secure

5.7.4 XFce

5.7.4.1 About XFce

XFce is a desktop environment based on the GTK+ toolkit used by GNOME, but is much more lightweight and meant for those who want a simple, efficient desktop which is nevertheless easy to use and configure. Visually, it looks very much like CDE, found on commercial UNIX systems. Some of XFce's features are:
·         A simple, easy-to-handle desktop
·         Fully configurable via mouse, with drag and drop, etc.
·         Main panel similar to CDE, with menus, applets and applications launchers
·         Integrated window manager, file manager, sound manager, GNOME compliance module, and more
·         Themeable (since it uses GTK+)
·         Fast, light and efficient: ideal for older/slower machines or machines with memory limitations
More information on XFce can be found on the XFce website.

5.7.4.2 Installing XFce

A binary package for XFce exists (at the time of writing). To install, simply type:
# pkg_add -r xfce4
Alternatively, to build from source, use the ports collection:
# cd /usr/ports/x11-wm/xfce4
# make install clean
Now, tell the X server to launch XFce the next time X is started. Simply type this:
% echo "/usr/local/bin/startxfce4" > ~/.xinitrc
The next time X is started, XFce will be the desktop. As before, if a display manager like XDM is being used, create an .xsession, as described in the section on GNOME, but with the /usr/local/bin/startxfce4 command; or, configure the display manager to allow choosing a desktop at login time, as explained in the section on kdm.

Common Tasks

Now that the basics have been covered, this part of the FreeBSD Handbook will discuss some frequently used features of FreeBSD. These chapters:
·         Introduce you to popular and useful desktop applications: browsers, productivity tools, document viewers, etc.
·         Introduce you to a number of multimedia tools available for FreeBSD.
·         Explain the process of building a customized FreeBSD kernel, to enable extra functionality on your system.
·         Describe the print system in detail, both for desktop and network-connected printer setups.
·         Show you how to run Linux applications on your FreeBSD system.
Some of these chapters recommend that you do some prior reading, and this is noted in the synopsis at the beginning of each chapter.
Table of Contents

Desktop Applications

Table of Contents
6.5 Finance
6.6 Summary
Contributed by Christophe Juniet.

6.1 Synopsis

FreeBSD can run a wide variety of desktop applications, such as browsers and word processors. Most of these are available as packages or can be automatically built from the ports collection. Many new users expect to find these kinds of applications on their desktop. This chapter will show you how to install some popular desktop applications effortlessly, either from their packages or from the Ports Collection.
Note that when installing programs from the ports, they are compiled from source. This can take a very long time, depending on what you are compiling and the processing power of your machine(s). If building from source takes a prohibitively long amount of time for you, you can install most of the programs of the Ports Collection from pre-built packages.
As FreeBSD features Linux binary compatibility, many applications originally developed for Linux are available for your desktop. It is strongly recommended that you read Chapter 10 before installing any of the Linux applications. Many of the ports using the Linux binary compatibility start with “linux-”. Remember this when you search for a particular port, for instance with whereis(1). In the following text, it is assumed that you have enabled Linux binary compatibility before installing any of the Linux applications.
Here are the categories covered by this chapter:
·         Browsers (such as Firefox, Opera, Konqueror)
·         Productivity (such as KOffice, AbiWord, The GIMP, OpenOffice.org)
·         Document Viewers (such as Acrobat Reader®, gv, Xpdf, GQview)
·         Finance (such as GnuCash, Gnumeric, Abacus)
Before reading this chapter, you should:
·         Know how to install additional third-party software (Chapter 4).
·         Know how to install additional Linux software (Chapter 10).
For information on how to get a multimedia environment, read Chapter 7. If you want to set up and use electronic mail, please refer to Chapter 28.

Browsers

FreeBSD does not come with a particular browser pre-installed. Instead, the www directory of the Ports Collection contains a lot of browsers ready to be installed. If you do not have time to compile everything (this can take a very long time in some cases) many of them are available as packages.
KDE and GNOME already provide HTML browsers. Please refer to Section 5.7 for more information on how to set up these complete desktops.
If you are looking for light-weight browsers, you should investigate the Ports Collection for www/dillo, www/links, or www/w3m.
This section covers these applications:
Application Name
Resources Needed
Installation from Ports
Major Dependencies
Firefox
medium
heavy
Gtk+
Opera
light
light
FreeBSD and Linux versions available. The Linux version depends on the Linux Binary Compatibility and linux-openmotif.
Konqueror
medium
heavy
KDE Libraries

6.2.1 Firefox

Firefox is a modern, free, open-source stable browser that is fully ported to FreeBSD: it features a very standards-compliant HTML display engine, tabbed browsing, popup blocking, extensions, improved security, and more. Firefox is based on the Mozilla codebase.
Install the package by typing:
# pkg_add -r firefox
This will install Firefox 2.X, if you want to run Firefox 3.X, use instead:
# pkg_add -r firefox3
You can also use the Ports Collection if you prefer to compile from source code:
# cd /usr/ports/www/firefox
# make install clean
For Firefox 3.X, in the previous command replace firefox with firefox3.

6.2.2 Firefox and Java™ Plugin

Note: In this section and in the next one, we assume you have already installed Firefox.
The FreeBSD Foundation has a license with Sun Microsystems to distribute FreeBSD binaries for the Java Runtime Environment (JRE™) and Java Development Kit (JDK™). Binary packages for FreeBSD are available on the FreeBSD Foundation web site.
To add Java™ support to Firefox, you first have to install the java/javavmwrapper port. Then, download the Diablo JRE package from http://www.freebsdfoundation.org/downloads/java.shtml, and install it with pkg_add(1).
Start your browser, enter about:plugins in the location bar and press Enter. A page listing the installed plugins will be displayed; the Java plugin should be listed there now. If it is not, as root, run the following command:
# ln -s /usr/local/diablo-jre1.6.0/plugin/i386/ns7/libjavaplugin_oji.so \
  /usr/local/lib/browser_plugins/
Then relaunch your browser.

6.2.3 Firefox and Macromedia® Flash™ Plugin

Macromedia® Flash™ plugin is not available for FreeBSD. However, a software layer (wrapper) for running the Linux version of the plugin exists. This wrapper also supports Adobe® Acrobat® plugin, RealPlayer® plugin and more.
Warning: The following text covers the installation of Flash 9.X on recent -STABLE branch, FreeBSD 7.1-RELEASE and above. If you run an older version of FreeBSD or encounter issues, you should install www/linux-flashplugin7 and skip the linprocfs(5) part.
Install the www/nspluginwrapper port. This port requires emulators/linux_base which is a large port.
The next step is to install the www/linux-flashplugin9 port. Once this port is installed, the plugin must be installed by each user with nspluginwrapper:
% nspluginwrapper -v -a -i
The Linux® process file system, linprocfs(5) has to be mounted on /usr/compat/linux/proc, if one wants to play Flash animations. This can be done via the following command:
# mount -t linprocfs linproc /usr/compat/linux/proc
This point can be automated at boot time with the addition of the matching line in /etc/fstab:
linproc    /usr/compat/linux/proc  linprocfs   rw  0   0
Then, start your browser, enter about:plugins in the location bar and press Enter. A list should appear with all the currently available plugins.

6.2.4 Firefox and Swfdec Flash Plugin

Swfdec is the library for decoding and rendering Flash animations. And Swfdec-Mozilla is a plugin for Firefox browsers that uses the Swfdec library for playing SWF files. It is still in heavy development.
If you cannot or do not want to compile it, just install the package from the network:
# pkg_add -r swfdec-plugin
If the package is not available, you can compile and install it from the Ports Collection:
# cd /usr/ports/www/swfdec-plugin
# make install clean
Then, restart your browser for this plugin taking effect.

6.2.5 Opera

Opera is a full-featured and standards-compliant browser. It also comes with a built-in mail and news reader, an IRC client, an RSS/Atom feeds reader and much more. Despite this, Opera is relatively lightweight and very fast. It comes in two flavors: a “native” FreeBSD version and a version that runs under Linux emulation.
To browse the Web with the FreeBSD version of Opera, install the package:
# pkg_add -r opera
Some FTP sites do not have all the packages, but Opera can still be obtained through the Ports Collection by typing:
# cd /usr/ports/www/opera
# make install clean
To install the Linux version of Opera, substitute linux-opera in place of opera in the examples above. The Linux version is useful in situations requiring the use of plug-ins that are only available for Linux, such as Adobe Acrobat Reader®. In all other respects, the FreeBSD and Linux versions should be functionally identical.

6.2.6 Konqueror

Konqueror is part of KDE but it can also be used outside of KDE by installing x11/kdebase3. Konqueror is much more than a browser, it is also a file manager and a multimedia viewer.
There is also a set of plugins available for Konqueror, available in misc/konq-plugins.
Konqueror also supports Flash; a “How To” guide for getting Flash support on Konqueror is available at http://freebsd.kde.org/howtos/konqueror-flash.php.

roductivity

When it comes to productivity, new users often look for a good office suite or a friendly word processor. While some desktop environments like KDE already provide an office suite, there is no default productivity package. FreeBSD can provide all that is needed, regardless of your desktop environment.
This section covers these applications:
Application Name
Resources Needed
Installation from Ports
Major Dependencies
KOffice
light
heavy
KDE
AbiWord
light
light
Gtk+ or GNOME
The Gimp
light
heavy
Gtk+
OpenOffice.org
heavy
huge
JDK™ 1.4, Mozilla

6.3.1 KOffice

The KDE community has provided its desktop environment with an office suite which can be used outside KDE. It includes the four standard components that can be found in other office suites. KWord is the word processor, KSpread is the spreadsheet program, KPresenter manages slide presentations, and Kontour lets you draw graphical documents.
Before installing the latest KOffice, make sure you have an up-to-date version of KDE.
To install KOffice as a package, issue the following command:
# pkg_add -r koffice
If the package is not available, you can use the ports collection. For instance, to install KOffice for KDE3, do:
# cd /usr/ports/editors/koffice-kde3
# make install clean

6.3.2 AbiWord

AbiWord is a free word processing program similar in look and feel to Microsoft® Word. It is suitable for typing papers, letters, reports, memos, and so forth. It is very fast, contains many features, and is very user-friendly.
AbiWord can import or export many file formats, including some proprietary ones like Microsoft's .doc.
AbiWord is available as a package. You can install it by:
# pkg_add -r abiword
If the package is not available, it can be compiled from the Ports Collection. The Ports Collection should be more up to date. It can be done as follows:
# cd /usr/ports/editors/abiword
# make install clean

6.3.3 The GIMP

For image authoring or picture retouching, The GIMP is a very sophisticated image manipulation program. It can be used as a simple paint program or as a quality photo retouching suite. It supports a large number of plug-ins and features a scripting interface. The GIMP can read and write a wide range of file formats. It supports interfaces with scanners and tablets.
You can install the package by issuing this command:
# pkg_add -r gimp
If your FTP site does not have this package, you can use the Ports Collection. The graphics directory of the Ports Collection also contains The Gimp Manual. Here is how to get them installed:
# cd /usr/ports/graphics/gimp
# make install clean
# cd /usr/ports/graphics/gimp-manual-pdf
# make install clean
Note: The graphics directory of the Ports Collection holds the development version of The GIMP in graphics/gimp-devel. An HTML version of The Gimp Manual is available from graphics/gimp-manual-html.

6.3.4 OpenOffice.org

OpenOffice.org includes all of the mandatory applications in a complete office productivity suite: a word processor, a spreadsheet, a presentation manager, and a drawing program. Its user interface is very similar to other office suites, and it can import and export in various popular file formats. It is available in a number of different languages -- internationalization has been extended to interfaces, spell checkers, and dictionaries.
The word processor of OpenOffice.org uses a native XML file format for increased portability and flexibility. The spreadsheet program features a macro language and it can be interfaced with external databases. OpenOffice.org is already stable and runs natively on Windows®, Solaris™, Linux, FreeBSD, and Mac OS® X. More information about OpenOffice.org can be found on the OpenOffice.org web site. For FreeBSD specific information, and to directly download packages, use the FreeBSD OpenOffice.org Porting Team's web site.
To install OpenOffice.org, do:
# pkg_add -r openoffice.org
Note: When running a -RELEASE version of FreeBSD, this should work. Otherwise, you should look on the FreeBSD OpenOffice.org Porting Team's web site to download and install the appropriate package using pkg_add(1). Both the current release and development version are available for download at this location.
Once the package is installed, you just have to type the following command to run OpenOffice.org:
% openoffice.org
Note: During the first launch, you will be asked some questions and a .openoffice.org2 folder will be created in your home directory.
If the OpenOffice.org packages are not available, you still have the option to compile the port. However, you must bear in mind that it requires a lot of disk space and a fairly long time to compile.
# cd /usr/ports/editors/openoffice.org-2
# make install clean
Note: If you want to build a localized version, replace the previous command line with the following:
# make LOCALIZED_LANG=your_language install clean
You have to replace your_language with the correct language ISO-code. A list of supported language codes is available in the files/Makefile.localized file, located in the port directory.
Once this is done, OpenOffice.org can be launched with the command:
% openoffice.org

Document Viewers

Some new document formats have gained popularity since the advent of UNIX®; the standard viewers they require may not be available in the base system. We will see how to install such viewers in this section.
This section covers these applications:
Application Name
Resources Needed
Installation from Ports
Major Dependencies
Acrobat Reader®
light
light
Linux Binary Compatibility
gv
light
light
Xaw3d
Xpdf
light
light
FreeType
GQview
light
light
Gtk+ or GNOME

6.4.1 Acrobat Reader®

Many documents are now distributed as PDF files, which stands for “Portable Document Format”. One of the recommended viewers for these types of files is Acrobat Reader, released by Adobe for Linux. As FreeBSD can run Linux binaries, it is also available for FreeBSD.
To install Acrobat Reader 7 from the Ports collection, do:
# cd /usr/ports/print/acroread7
# make install clean
A package is not available due to licencing restrictions.

6.4.2 gv

gv is a PostScript® and PDF viewer. It is originally based on ghostview but it has a nicer look thanks to the Xaw3d library. It is fast and its interface is clean. gv has many features, such as orientation, paper size, scale, and anti-aliasing. Almost any operation can be done with either the keyboard or the mouse.
To install gv as a package, do:
# pkg_add -r gv
If you cannot get the package, you can use the Ports collection:
# cd /usr/ports/print/gv
# make install clean

6.4.3 Xpdf

If you want a small FreeBSD PDF viewer, Xpdf is a light-weight and efficient viewer. It requires very few resources and is very stable. It uses the standard X fonts and does not require Motif® or any other X toolkit.
To install the Xpdf package, issue this command:
# pkg_add -r xpdf
If the package is not available or you prefer to use the Ports Collection, do:
# cd /usr/ports/graphics/xpdf
# make install clean
Once the installation is complete, you can launch Xpdf and use the right mouse button to activate the menu.

6.4.4 GQview

GQview is an image manager. You can view a file with a single click, launch an external editor, get thumbnail previews, and much more. It also features a slideshow mode and some basic file operations. You can manage image collections and easily find duplicates. GQview can do full screen viewing and supports internationalization.
If you want to install the GQview package, do:
# pkg_add -r gqview
If the package is not available or you prefer to use the Ports Collection, do:
# cd /usr/ports/graphics/gqview
# make install clean

Finance

If, for any reason, you would like to manage your personal finances on your FreeBSD Desktop, there are some powerful and easy-to-use applications ready to be installed. Some of them are compatible with widespread file formats, such as the formats used by Quicken® and Excel to store documents.
This section covers these programs:
Application Name
Resources Needed
Installation from Ports
Major Dependencies
GnuCash
light
heavy
GNOME
Gnumeric
light
heavy
GNOME
Abacus
light
light
Tcl/Tk
KMyMoney
light
heavy
KDE

6.5.1 GnuCash

GnuCash is part of the GNOME effort to provide user-friendly, yet powerful, applications to end-users. With GnuCash, you can keep track of your income and expenses, your bank accounts, and your stocks. It features an intuitive interface while remaining very professional.
GnuCash provides a smart register, a hierarchical system of accounts, and many keyboard accelerators and auto-completion methods. It can split a single transaction into several more detailed pieces. GnuCash can import and merge Quicken QIF files. It also handles most international date and currency formats.
To install GnuCash on your system, do:
# pkg_add -r gnucash
If the package is not available, you can use the ports collection:
# cd /usr/ports/finance/gnucash
# make install clean

6.5.2 Gnumeric

Gnumeric is a spreadsheet program, part of the GNOME desktop environment. It features convenient automatic “guessing” of user input according to the cell format with an autofill system for many sequences. It can import files in a number of popular formats like those of Excel, Lotus 1-2-3, or Quattro Pro. Gnumeric supports graphs through the math/guppi graphing program. It has a large number of built-in functions and allows all of the usual cell formats such as number, currency, date, time, and much more.
To install Gnumeric as a package, do:
# pkg_add -r gnumeric
If the package is not available, you can use the ports collection by doing:
# cd /usr/ports/math/gnumeric
# make install clean

6.5.3 Abacus

Abacus is a small and easy to use spreadsheet program. It includes many built-in functions useful in several domains such as statistics, finances, and mathematics. It can import and export the Excel file format. Abacus can produce PostScript® output.
To install Abacus as a package, do:
# pkg_add -r abacus
If the package is not available, you can use the ports collection by doing:
# cd /usr/ports/deskutils/abacus
# make install clean

6.5.4 KMyMoney

KMyMoney is a personal finance manager built for KDE. KMyMoney intends to provide and incorporate all the important features found in commercial personal finance manager applications. It also highlights ease-of-use and proper double-entry accounting among its features. KMyMoney imports from standard Quicken Interchange Format (QIF) files, tracks investments, handles multiple currencies, and provides a wealth of reports. OFX import capabilities are also available through a separate plugin.
To install KMyMoney as a package, do:
# pkg_add -r kmymoney2
If the package is not available, you can use the Ports Collection by doing:
# cd /usr/ports/finance/kmymoney2
# make install clean
Useful UNIX Commands

The following is a list of some of the more important and useful commands in Unix (specific to HPUX) Remember that the man command can be used to find out the specific usage of any of these.

Essentials

  • logout - use this command when you are done, always
  • man - use the command `man command' to learn about a command.

Files and Directories

  • cd - change directory
  • pwd - print working directory (the directory you are in)
  • rmdir - remove directory (the directory must be empty)
  • mkdir - make a new directory
  • rm - remove a file or directory
  • ls - list files
  • cp - copy a file
  • mv - move a file (destroys original)
  • less - view a text file
  • cat - prints the contents of a file on the screen
  • touch - create an empty file
  • gzip and gunzip - compression and decompression
  • tar - Tape Archive. Now used to store directory trees as a single file for easy transfer. See the -xvf and -cvf options in the man page
  • uudecode and uuencode decode and encode binary files as text files for transfers

System Status

  • date - displays the current date and time
  • du - displays amount of disk space in 512byte blocks ('du -s' gives the summary for a directory)
  • quota -v - displays disk limits and amount currently in use
  • ps - display running processes ('ps -ef | grep username' gives all the processes owned by a particular user)
  • who - display who is on the machine and where they are connected from
  • w - display who is on and what they are doing
  • who - display who is on and where they are on from
  • jobs - display the jobs you own
  • uptime - display time, number of users, and load averages
  • loads - a local script to show the loads on all the HP700s
  • renice - alters priority of a running process. You should renice long simulations so you don't hog processor cycles.

Networking

  • ssh - open an encrypted connection with a host
  • telnet - open a remote session with host
  • rlogin - open a remote session with host
  • ftp - File Transfer Protocol: send and receive files betweem machines
  • ping - check to make sure a remote machine is reachable
  • finger - show information about a person on a system or whoever is on the remote system WWW version

Editors and Text Processors

  • vi - Visual Editor. Great stuff
  • pico - Easy to use editor
  • grep - searches a file for a pattern
  • a2ps - (not currently availible) allows the formatting of text into a multi-columned format

Printing

Please see the printing one-pager for details about printing.
  • lp - print the specified file. Common usage: 'lp -dhpps file.ps'
  • lpstat - tells you infomation about current print jobs
  • cancel - cancel a print job. Common usage: 'cancel hpps-###'

An A-Z Index of the Bash command line for Linux.

  alias    Create an alias
  apropos  Search Help manual pages (man -k)
  apt-get  Search for and install software packages (Debian)
  aspell   Spell Checker
  awk      Find and Replace text, database sort/validate/index
b
  bash     GNU Bourne-Again SHell 
  bc       Arbitrary precision calculator language 
  bg       Send to background
  break    Exit from a loop
  builtin  Run a shell builtin
  bzip2    Compress or decompress named file(s)
c
  cal      Display a calendar
  case     Conditionally perform a command
  cat      Display the contents of a file
  cd       Change Directory
  cfdisk   Partition table manipulator for Linux
  chgrp    Change group ownership
  chmod    Change access permissions
  chown    Change file owner and group
  chroot   Run a command with a different root directory
  chkconfig System services (runlevel)
  cksum    Print CRC checksum and byte counts
  clear    Clear terminal screen
  cmp      Compare two files
  comm     Compare two sorted files line by line
  command  Run a command - ignoring shell functions
  continue Resume the next iteration of a loop
  cp       Copy one or more files to another location
  cron     Daemon to execute scheduled commands
  crontab  Schedule a command to run at a later time
  csplit   Split a file into context-determined pieces
  cut      Divide a file into several parts
d
  date     Display or change the date & time
  dc       Desk Calculator
  dd       Convert and copy a file, write disk headers, boot records
  ddrescue Data recovery tool
  declare  Declare variables and give them attributes
  df       Display free disk space
  diff     Display the differences between two files
  diff3    Show differences among three files
  dig      DNS lookup
  dir      Briefly list directory contents
  dircolors Colour setup for `ls'
  dirname  Convert a full pathname to just a path
  dirs     Display list of remembered directories
  dmesg    Print kernel & driver messages 
  du       Estimate file space usage
e
  echo     Display message on screen
  egrep    Search file(s) for lines that match an extended expression
  eject    Eject removable media
  enable   Enable and disable builtin shell commands
  env      Environment variables
  ethtool  Ethernet card settings
  eval     Evaluate several commands/arguments
  exec     Execute a command
  exit     Exit the shell
  expect   Automate arbitrary applications accessed over a terminal
  expand   Convert tabs to spaces
  export   Set an environment variable
  expr     Evaluate expressions
f
  false    Do nothing, unsuccessfully
  fdformat Low-level format a floppy disk
  fdisk    Partition table manipulator for Linux
  fg       Send job to foreground 
  fgrep    Search file(s) for lines that match a fixed string
  file     Determine file type
  find     Search for files that meet a desired criteria
  fmt      Reformat paragraph text
  fold     Wrap text to fit a specified width.
  for      Expand words, and execute commands
  format   Format disks or tapes
  free     Display memory usage
  fsck     File system consistency check and repair
  ftp      File Transfer Protocol
  function Define Function Macros
  fuser    Identify/kill the process that is accessing a file
g
  gawk     Find and Replace text within file(s)
  getopts  Parse positional parameters
  grep     Search file(s) for lines that match a given pattern
  groups   Print group names a user is in
  gzip     Compress or decompress named file(s)
h
  hash     Remember the full pathname of a name argument
  head     Output the first part of file(s)
  history  Command History
  hostname Print or set system name
i
  id       Print user and group id's
  if       Conditionally perform a command
  ifconfig Configure a network interface
  ifdown   Stop a network interface 
  ifup     Start a network interface up
  import   Capture an X server screen and save the image to file
  install  Copy files and set attributes
j
  join     Join lines on a common field
k
  kill     Stop a process from running
  killall  Kill processes by name
l
  less     Display output one screen at a time
  let      Perform arithmetic on shell variables
  ln       Make links between files
  local    Create variables
  locate   Find files
  logname  Print current login name
  logout   Exit a login shell
  look     Display lines beginning with a given string
  lpc      Line printer control program
  lpr      Off line print
  lprint   Print a file
  lprintd  Abort a print job
  lprintq  List the print queue
  lprm     Remove jobs from the print queue
  ls       List information about file(s)
  lsof     List open files
m
  make     Recompile a group of programs
  man      Help manual
  mkdir    Create new folder(s)
  mkfifo   Make FIFOs (named pipes)
  mkisofs  Create an hybrid ISO9660/JOLIET/HFS filesystem
  mknod    Make block or character special files
  more     Display output one screen at a time
  mount    Mount a file system
  mtools   Manipulate MS-DOS files
  mv       Move or rename files or directories
  mmv      Mass Move and rename (files)
n
  netstat  Networking information
  nice     Set the priority of a command or job
  nl       Number lines and write files
  nohup    Run a command immune to hangups
  nslookup Query Internet name servers interactively
o
  open     Open a file in its default application
  op       Operator access 
p
  passwd   Modify a user password
  paste    Merge lines of files
  pathchk  Check file name portability
  ping     Test a network connection
  popd     Restore the previous value of the current directory
  pr       Prepare files for printing
  printcap Printer capability database
  printenv Print environment variables
  printf   Format and print data
  ps       Process status
  pushd    Save and then change the current directory
  pwd      Print Working Directory
q
  quota    Display disk usage and limits
  quotacheck Scan a file system for disk usage
  quotactl Set disk quotas
r
  ram      ram disk device
  rcp      Copy files between two machines
  read     read a line from standard input
  readonly Mark variables/functions as readonly
  reboot   Reboot the system
  renice   Alter priority of running processes 
  remsync  Synchronize remote files via email
  return   Exit a shell function
  rev      Reverse lines of a file
  rm       Remove files
  rmdir    Remove folder(s)
  rsync    Remote file copy (Synchronize file trees)
s
  screen   Multiplex terminal, run remote shells via ssh
  scp      Secure copy (remote file copy)
  sdiff    Merge two files interactively
  sed      Stream Editor
  select   Accept keyboard input
  seq      Print numeric sequences
  set      Manipulate shell variables and functions
  sftp     Secure File Transfer Program
  shift    Shift positional parameters
  shopt    Shell Options
  shutdown Shutdown or restart linux
  sleep    Delay for a specified time
  slocate  Find files
  sort     Sort text files
  source   Run commands from a file `.'
  split    Split a file into fixed-size pieces
  ssh      Secure Shell client (remote login program)
  strace   Trace system calls and signals
  su       Substitute user identity
  sudo     Execute a command as another user
  sum      Print a checksum for a file
  symlink  Make a new name for a file
  sync     Synchronize data on disk with memory
t
  tail     Output the last part of files
  tar      Tape ARchiver
  tee      Redirect output to multiple files
  test     Evaluate a conditional expression
  time     Measure Program running time
  times    User and system times
  touch    Change file timestamps
  top      List processes running on the system
  traceroute Trace Route to Host
  trap     Run a command when a signal is set(bourne)
  tr       Translate, squeeze, and/or delete characters
  true     Do nothing, successfully
  tsort    Topological sort
  tty      Print filename of terminal on stdin
  type     Describe a command
u
  ulimit   Limit user resources
  umask    Users file creation mask
  umount   Unmount a device
  unalias  Remove an alias
  uname    Print system information
  unexpand Convert spaces to tabs
  uniq     Uniquify files
  units    Convert units from one scale to another
  unset    Remove variable or function names
  unshar   Unpack shell archive scripts
  until    Execute commands (until error)
  useradd  Create new user account
  usermod  Modify user account
  users    List users currently logged in
  uuencode Encode a binary file 
  uudecode Decode a file created by uuencode
v
  v        Verbosely list directory contents (`ls -l -b')
  vdir     Verbosely list directory contents (`ls -l -b')
  vi       Text Editor
  vmstat   Report virtual memory statistics
w
  watch    Execute/display a program periodically
  wc       Print byte, word, and line counts
  whereis  Report all known instances of a command    
  which    Locate a program file in the user's path. 
  while    Execute commands
  who      Print all usernames currently logged in
  whoami   Print the current user id and name (`id -un')
  Wget     Retrieve web pages or files via HTTP, HTTPS or FTP
  write    Send a message to another user 
x
  xargs    Execute utility, passing constructed argument list(s)
  yes      Print a string until interrupted
  .        Run a command script in the current shell
  ###      Comment / Remark
__________
vi  pronounced as " vee eye " is a unix editor available on  almost all the unix  operating systems , solaris , bsd ,aix , hpux etc.
This document is a quick reference to vi editor and will be of help if your are new to unix , learning unix  or just refreshing your vi knowledge after a few years.
 
____________
In order to work correctly the vi need correct terminal type (TERM) setting  .The TERM setting depends on the type of terminal you have . Commonly used TERM types are vt100 , vt220 and ansi .  In most cases vt100 will  work  fine . In case vi is not able to understand the TERM you have given, it starts in open mode giving you a line by line display .
Generally TERM is taken from .profile or /etc/profile  but can be set at the command line as :
$TERM=vt100
$export TERM    
echo $TERM will display the current TERM set.
Create new file or Open existing file in vi
__________________________________
vi without any file name will open a  new file where you can enter the text and edit but while coming out you will be asked to enter a valid file name to save the text.
vi with a  file name as argument will open that file for editing  if the file already exists it opens it otherwise it creates a new file by the argument.
Example :  $vi  testfile
Creates or opens the existing file called testfile

____________
 vi operates in following  two modes :
i. ) Command Mode : After a file is opened it is opened  in command mode ,that is , input from the keyboard will be treated as vi commands  and you will not see the words you are typing on the screen .

 ii.) Insert Mode: To enter the text you have to put vi in insert  by pressing 'i' or 'a'  after which you can add the text and whatever is being type will be seen on the screen. . To switch between these mode Esc key is used .   Esc i  (text mode)  Esc (command mode) 
 
______________________
 You can exit vi in different ways :
1.) Quit without saving : If you don't want to save the work :q  will take you out without saving your editing in vi.
2.) Write & quit : . Simple :w saves the current file but don't exit. For save and quit  :wq is used in vi. 
3.) Forced Quite : An ! (Exclamation sign at the end of  exit commands ( :q! , :wq! )  causes a forced quit from vi  after ignoring editing (for :q!)  or writing (for :wq!) all the changes..


 
 








 
 
Offering Unix , Solaris & IT Training Services throughout the UK.




vi commands , quick reference
Moving Cursor in File
Left   
h
Right
i
Up
k
Down
j
Line
Beginning
^ or B
end
$
Sentance :
 Next sentance
)
Previous sentance
(
Paragraph
Next
}
Previous 
{
file
Go to end of file
:$
on chacter forword
:w
One word forword
:W
go to a line number
:line_number
display file info .
^g
Inserting and appending text :
inserts text to the left of  cursor
i
nserts in the beginning of line 
I
appends text to right of cursor
a
appends to the end of line
A
Adding new line
add a new line below the current line
o
adds a new line above the current line.
O
deleting the text :
deletes text above the text
x
deletes text character on  right of cursor
X
deletes line 20
20d
deletes current line
dd
 delete till end of current line.
D
Replacing a character & word
 replace the character above the cursor.
r
replces characters until Esc is pressed.
R
replaces the word from cursor to the end indicated by $ sign .
cw
 replaces till end of line.
C
Substitute
subistutes current charcater.
s
substitutes entire line.
S
Undo the last change
undo last change.
u
undo changes to the current line.
U
Copy and pasting lines
copys the current line into buffer.
yy
copies 5 lines from the current line.
5yy
pastes the current buffer.
p
Searching
Searches for the word name in the file
:/name  
n continues search forward.
n
N searches backwards
N
Saving
saves the text does not quit.
:w
 saves & quit the editor .
:wq!
save
ZZ
Quit without saving
q!


Search & Replace
s/<search-string>/<replace-string>/g  .
Repeating  last command
.
Recovering a unsaved vi file.
vi -r  filename 
 
7. Next Steps :
vi editor is all time favorite for the unix admins and there are some books available if you are interested in exploring the more powers of vi editor. The editor is also covered in system administration books so you may find a chapter devoted to vi editor in most of the unix system administration books.
You can purchase unix administration or vi editor books from amazon.com online using the display panel below.
his document aims to provide a core set of Unix commands to get you around.

General Unix Information

There are numerous flavours of Unix; AIX (IBM), Solaris (SUN), Xenix, Linux etc. all of which conform to the Posix standard. Unix often comes with a number of command shells, e.g. Bourne Shell ($ prompt) or C shell (% prompt with enhancements on the Bourne Shell) or Korn shell ($ prompt) which have slightly differing command syntax, although principally they are the same. The shell interprets the commands that you type. The 'Tea Sea Shell' (tcsh) is often used in the Linux environment and contains useful attributes such as 'up-arrow' and 'down-arrow' recall of previous command entries, and the use of the TAB key to complete commands (much like the Cisco IOS!). The shell interprets commands with the operating system kernel. The beauty of Unix is that hundreds of people can access one box at once and each one can run a number of programs, a separate shell opens for each log on that occurs, and each user can have a completely different 'environment' setup, different colours, priviledges, file and directory access and different shell.

Unix file names can be up to 14 characters long and include the _ and the . characters.

Every Unix command or filename is case sensitive, unlike DOS, this is the most common error to be aware of. Commands leave 'notes' for programs (such as printing) that are 'buried' in the Unix system. This is so that one user does not hog one program, many people can access it. Such a program is called a 'daemon'.

Below is a diagram illustrating a typical Unix file system structure on a box:

unix file system

An Absolute Pathname starts from the root directory e.g. /user/bin. The Relative pathname points to a file or directory that is relative to the position that you are in within the directory tree and this does not start with a /.

Information about users is kept in the passwd file which sits in the /etc directory along with the other configuration files. For each user there are seven fields separated by colons:
  • User name (first part of e-mail address).
  • Encrypted password.
  • UID (Users ID) needed by the Unix system. User names can change without changing user permissions.
  • GID (Group ID). A user may be a member of several groups each having different permissions.
  • Comment containing more detail on the user if desired.
  • User's home directory.
  • The shell to be used by the particular user.
Nowadays, when you are confronted with a Unix box you will come across an X-windows interface. To get to a command line interface, grab the three button mouse click the right button and select programs clicking on it with the left mouse button. In the list of programs that appear select either shell or command to open a command line window (much like a DOS box in Microsoft Windows). You normally need to click on the title bar or border before you can type in the window, sometimes the X-window interface has been set up such that the mouse moving over the window is enough to highlight the box. Any number of these command line windows can be opened. Resizing them is achieved by selecting the bottom right hand corner with the left mouse button (a circle appears) and dragging the window edges to the required size. Minimising a window is achieved by 'left-clicking' on the top left corner of the menu bar and selecting close. Selecting quit closes the window.

Keystrokes


Ctrl-c


Stops a command or program that is currently being executed.

Ctrl-d


Removes you from the current environment, this will log you out of the system if you are at a shell prompt.

Ctrl-h, Del


Deletes the last character typed and moves back one space. Unlike DOS, the backspace key does not work!

Ctrl-q


Resumes the command that was halted by Ctrl-s.

Ctrl-s


Temporarily halts the current command being executed, e.g. scrolling of text on the screen.

Ctrl-u, Ctrl-x, @


Cancels what you have just typed, so that you can start again.

Ctrl-x


Deletes the current line of text being entered.

Directory Commands


cd


Stands for change directory, e.g.
cd /user/dave
takes you to dave's personal directory. The first / refers to root. Root is the equivalent of / in DOS. Typing cd without a path takes you back to your home directory, i.e. where you arrive when you first log on. Typing cd.. takes you up one directory, whereas typing cd ../user/dave, takes you up one directory and then right down to the '/user/dave' directory.

df -k


Stands for disk free, gives you the amount of space available on the disk that you are currently on.

mkdir


Means 'make directory', e.g.
mkdir user
creates a directory called user in the directory you are in when you issue the command.

pwd


Stands for print working directory and prints the directory that you are in to the screen.

rmdir


Means remove directory e.g.
rmdir user
removes the directory user provided that it is empty!

System Commands


-&


Using this switch after a command causes it to operate in the background, allowing you to continue using the same command line window without having to open another one.

*


This wildcard character matches any number of characters and is useful in searches, e.g.
g*
matches all files beginning with 'g'.

?


This wildcard character matches any single character, e.g.
g??
matches all three character files beginning with 'g'.


Redirect output from a program to a file, e.g.
ls -l > listing
redirects the listing of ls -l into a file called 'listing'.


Redirect output from a file into a program, e.g.
mail john < hello
redirects the greeting letter called 'hello' to John, rather than you having to type it.

|


Pipe output from one program to another, e.g.
who | wc -l
gives a count of the users on the system.

>> 


This append adds the input to an existing file without overwriting the original, e.g.
postcript >> letter
adds the contents of 'postscript' to an existing file called 'letter'.

chsh


Means change shell and changes the shell that the user is using. The user will be prompted for a password since the 'passwd' file is being changed, then the user will need to type the path to the shell e.g. /bin/bash.

echo


This 'echoes' arguments to the screen, e.g.
echo $SHELL
displays the value of the environment variable SHELL. This could return /bin/tcsh (Linux often uses this shell) or /bin/bash. echo $PATH displays the current path.

env


The environment gives you the variables set up for the particular user that issues the command.

exit, Ctrl-d


Logs you out.

jobs


This lists the jobs running under the current shell in 'job ID' order. You can type bg %jobid to put a particular job running in the background. Ctrl Z also suspends a job. Typing fg %jobid brings the job back to the foreground.

kill


This kills a process e.g.
kill 5173
kill the process which has been given the temporary number 5173. This process number is found by using the ps command. Do not use kill 1 as this kills the system scheduler! If a process refuses to die you can type kill -KILL [PID] to stop a process immediately without any tidying up on exitting. Finally, kill -HUP [PID] tells the process that an event has occurred, or a configuration file change has occurred and needs to br reread.

man


The manual command is very useful for finding out comprehensive information on an individual command e.g.
man cd
gives all the information on the command cd. Typing man -k mail lists the Unix commands that relate to the word mail.

passwd


Allows you or the administrator to change passwords.

printenv


The 'prints the environment' variables to the screen.

ps


The process status command shows the programs currently running. ps -a shows all the processes being run by all users. An example is the following:
ps -ef | grep erpcd
where '-ef' gets the process number and pipes it to grep which filters on the following word, in this case for the program 'erpcd'.

The following information is shown:
  • PID Process ID.
  • TTY Each shell opened has a 'character special' called a 'tty' (held in '/dev').
  • STAT State, either 'S', sleeping, or 'R', running.
  • TIME CPU time that the process is taking up.
  • COMMAND The command running.
Typing ps x shows all the processes relating to X windows, whereas ps ax shows all the processes being run by everybody. Typing ps ux gives even more information such as the user.

setenv


The command set environment variable, sets aside a small amount of memory to hold paths etc. e.g.
setenv GUI /usr/utility/gui_r4
sets a variable 'GUI' with the path that follows to the actual program. This program can now be run by typing 'GUI'.
setenv DISPLAY :0.0
sets an X window session locally.
These settings are commonly setup permanently in the user's .profile (located in the '/etc' directory). This can be edited with any text editor.

The following are common environment variables:
  • SHELL The current shell.
  • HOME The current user's home directory.
  • HOSTNAME The name of the computer.
  • DISPLAY The X display that the applications are to use.
  • LD_LIBRARY_PATH The search path for libraries.
  • PATH The search path for applications.
If you wish to append directories to the path then type setenv PATH ${PATH} : /search/here. In order to use it then you need to cache the new path by typing rehash.

The DISPLAY variable is made up of three parts 'hostname : displaynumber : screennumber'. The hostname is the computer, whilst the other variables are '0' unless several machines are connected. X windows looks to this variable to find out where to send the X Windows traffic.

set path


Sets a path where regularly used programs or data are found e.g.
set path=($path /usr/utility/gui_r4/bin)
sets the path '/usr/utility/gui_r4/bin'.

Some commands used to set the environment come from the C shell. In order to check which shell you are running type echo $SHELL, if this does not return '/sbin/csh' then you type /bin/csh.

su


The command switch user switches the login user to another user, e.g.
su root
switches to the 'root' login.

top


Gives a constantly updating view of the top 20 processes (a real time version of 'ps'), i.e. those that are using the CPU the most.

who


This displays the users currently logged on the system.

whoami


Displays who you are currently logged on as. (e.g. 'root', a user etc.)

xhost +


Opens an X window for a program to run in. After issuing this you would then run the program (e.g. Netscape).

File Commands


.


The dot is not a command as such. If a file is spelled with a dot at the beginning, Unix treats it as a hidden file. Configuration files are often preceded with a dot.

cat


The concatenate command displays a file, e.g.
cat bankletter
displays the contents of 'bankletter' on the screen.
cat > newletter
takes whatever you type and redirects it into the file 'newletter', Ctrl-d gets you out of it.
cat >> existing
takes whatever you type and appends it to an existing file called 'existing' funnily enough.

chmod


The command change mode changes the mode or permissions, of a file or a directory. When you do an ls -l you will see in the first column, a line of 10 characters looking something like 'drwx-w-rw-'. The 'd' means 'directory' (you could have '-' for file, 'l' for link to a file, 'b' for a 'block special', 'c' for a 'character special', 'p' for a 'named pipe', or 's' for 'socket'). The next three characters refer to the permissions of the login user, in this case the user has read, write and execute access to the directory. The next three characters refer to the permissions of the group and the final three characters refer to the permissions of all users. The chmod command can be used in various ways as shown by the following examples:
  • chmod go-rwx newletter removes read, write and execute permissions for users in the group (g), and all other users, for the file 'newletter'. Using a '+' instead of '-' adds the permissions. You can also use 'o' for others, or 'u' for user.
  • chmod 766 newletter causes the file 'newletter' to have read, write and execute permissions for the user, read and write permissions for the group members and read and write permissions for all other users. Why? Well, the 7 represents 111(binary) and 6 represents 110(binary) for each set of three 'rwx's. 'r' being set is given binary 1, 'x' being not set is given binary 0. Read permission is '4', write permission is '2', execute permission is '1' and no permissions is given with '0'.
  • chmod 700 dirname results in drwx------ for the directory which restricts access to everyone bar the owner.
  • chmod 664 filename gives -rw-rw-r-- that allows you and your group to read and edit the file but all others can only read the file.
  • chmod 600 filename gives -rwx------ creates a private file that only you can see and edit.
You can change permissions for groups of files with one command by using wildcards such as *.

chown


Use this to change ownership of a file e.g.
chown dave myfile
changes the ownership of the file 'myfile' to dave. This can only be carried out by the owner of the original file. A way around this is for the recipient to copy the file, then the copied file becomes their own.

chgrp


Use this to change group ownership of a file.

compress


This compresses a file e.g.
compress myfile
results in a file called 'myfile.Z'. The command uncompress can be used to uncompress the file.

cp


The copy command copies files from one directory to another, or to the same directory with a different name, e.g.
cp bankletter /user/dave/bankletter1
copies the file 'bankletter' from the directory that you are currently in, to the '/user/dave' directory with a new name 'bankletter1'.

file


This returns information on the content of a file, e.g.
file myletter
might return 'ASCII' to say that Unix guesses that 'myletter' contains ASCII.

find


This finds a file or directory, e.g.
find / -name na -print &
this finds a file with name 'na' starting the search from the 'root' and printing the result to the shell window, whilst still allowing you to carry on using it.

grep


This stands for global regular expression and print and is a search utility, e.g.
grep "325 Victory"
searches the current directory for files containing the text '325 Victory'.

gzip


GNU zip compresses files to create a '**.gz' file.

head


This command followed by a filename, displays the first ten lines of that file.

less


This is a way of displaying a file, it will give a percentage of file so far displayed at the bottom of the screen, and you can progress through reading the file by pressing the space bar.

ln


The command link, links files and directories, e.g.
ln -s/export/home/fred usr/fred
creates a copy of 'fred' in the '/export/home/' directory in the 'usr/fred' directory. A 'hard link' is like a Windows 'shortcut', there can be a number of them, with different names and they take up little space. A 'soft link' is identified with the '-s' switch and creates a copy of the file elsewhere.

lp (for System V) or lpr (for BSD)


The command line printer, prints a file, e.g.
lp newletter
prints 'newletter'.

lpstat -a all


The line printer stats command checks the printer queue in System V Unix.

ls


This lists the contents of the current directory, e.g.
ls /etc
lists the files and sub-directories of the current directory.
  • ls -l gives a long list of directories including file sizes, permissions, type etc. Using the -a switch causes 'all' files to be listed including those hidden files starting with ..
  • ls -c lists files by creation time.
  • ls -p marks directories with a slash at the end of the name.
  • ls -x displays the list in rows across the screen.
  • Using ls | more is useful for large directories as it stops the screen scrolling, you press the 'Return' key to advance one line at a time, or press the space bar to advance one page at a time.
You can type 'ls' and then define one or more directories for it to list.

more


This is another way of displaying a file, it will give a percentage of file so far displayed at the bottom of the screen, and you can progress through reading the file by pressing the space bar. Whilst in more, if you type v you will be taken straight to the vi editor.

mv


This moves a file from one directory to another or renames it in the same directory, e.g.
mv bankletter bankletter1
renames 'bankletter' to 'bankletter1'.

pg filename


Displays the content of the file one page at a time. You advance pages by pressing 'Return'. Option -l displays one more line, option n moves you to the page number specified by n and options +n and -n moves you forward or backward the number of pages specified by n.

rm


'remove' a file, e.g.
rm oldletter
removes the file 'oldletter'. Using rm -rf recursively removes all files and directories below the one that you are in. Using rm -i gives you the option of cancelling or confirming the command.

sort


Sorts the contents of a file, e.g.:
sort -o outfile infile

The contents of 'infile' are sorted in alphabetical order and fed into a new file called 'outfile', as defined by the switch '-o'.

tail


This means the tail end, this dynamically displays the file that is being written to in real time, e.g.
tail -f logfile
shows the file 'logfile' which is being written to.

tar


The command tape archive is an file archiving command. It creates a single uncompressed archive file from several, ideal for sending data over networks. Often files are archived, and then compressed using 'gzip'. E.g.
tar -tvf tarfile
displays the contents of a tarfile.
tar -xvf tarfile
extracts the contents of a tarfile. 'x' is extract, 'v' means 'verbose' and 'f' means the file.
tar -xvf tarfile target
extracts the file target from the tarfile.

touch


This just creates an empty file for appending to later on e.g.
touch log
creates an empty file called 'log' that needs to be available for another program to write to it perhaps.

uncompress


This command uncompresses a 'gzip' file, e.g.
uncompress myfile.gz
uncompresses the file 'myfile.gz'.

wc


The command word count counts the words in a particular file, e.g.
wc letter
counts the number of words in the file 'letter'

Simple Scripting


A Unix script is the equivalent of the DOS batch file. Using vi, the following could be typed into a file called 'new_script':
echo These users are on the system
who
echo Here is a detailed listing of the directory you are in
ls -al
The command chmod u+x new_script makes the script file executable by the logged in user.

Networking Commands


arp


Displays the 'Address Resolution Protocol' table e.g.
arp -a
displays all arp entries for all connected devices.
arp -d <ip address>
deletes the arp entry for that particular IP address.

ftp


The command file transfer protocol attaches you to another IP device e.g.
ftp 141.205.15.154
attaches you to the device with address 141.205.15.154. You are normally presented with a login and password screen.

Commands that are used in FTP are:
  • dir - directory listing.
  • quit - quit from ftp.
  • cd - change directory.
  • get or mget - get a file (or multiple files).
  • put or mput - put a file (or multiple files).
  • bin - sets up your system to receive binary files.
  • hash - displays hashes whilst files are being transferred.
  • lcd - local change directory changes the directory on your local machine to which you are sending and receiving files. This is useful as it saves you having to quit ftp to carry out the directory change.
The Hosts file can be found in the directory '/etc'.

netstat


This stands for network statistics, e.g.
netstat -r
displays the routing table of the Unix box.
netstat -a
displays alll network information.

Unix uses routed to listen to RIP in order to discover the Default Gateway.

ping


Ping an IP device e.g.
ping 141.205.51.26

rlogin


This works like telnet, e.g.
rlogin 141.205.52.16
takes you to another Unix machine only. To quit you press 'return', '~', .' and 'return' again.

telnet


Ctrl-6 and then Ctrl-] gets you to the telnet> prompt where typing close gets you out of telnet.

vstat


This displays CPU utilisation and gives a list of processes and their share of CPU utilisation, e.g.
vstat 10
displays the CPU utilisation every 10 seconds.

ifconfig


This displays the IP configuration of the box, e.g.
ifconfig -a
displays all IP configuration.

If you want to look at the routing process you can type:
ps -ef type grep routed
to send the 'routed' information to a file.

snoop


This command captures the network packets in a readable format, e.g.
snoop -p 23
captures all IP traffic using port 23 (Telnet). use Ctrl-C to stop the snoop.

Useful vi commands


Each command needs to be preceded by pressing the escape key!

i
insert mode.
<esc>
leave insert mode and go into command mode.
a
append characters to the end of the line.
o
open a line below your cursor.
O
open a line above.
<shift>g
go to the bottom of the file.
r
replace the letter that you are on with the one you type next.
x
erase the character that you are on.
dd
delete the line that you are on. A number before dd deletes that number of lines.
yy
copy the line you are on. A number before yy copies that number of lines.
p
paste the line you are on below you.
P
paste the line you are on above you.
:wq
write and quit the file that you are editing.
:wq!
write and quit the file that you are editing, even if it is designated as read only!
:w!
write to a read only file.
:q
quit.
:q!
discard any editing and quit.
/
this takes you to the bottom of the window where you can type a string and return to perform a search in the file.

(The character ! is often referred to as pling)

You can use vedit which is vi with more user friendly additions and also ed, or emacs.


No comments:

Post a Comment