
An Absolute Beginners Guide to FreeBSD
This is An Absolute Beginners Guide to Setting up a FreeBSD Server.
If you have any questions that you don't see addressed by this page. Please send any comments to fbsd-book@vmunix.com
When you boot FreeBSD, you'll see the hardware detection messages scroll by on the screen, followed by some information that indicates what daemons are being started. When everything is said and done, you'll be left with ``The Login Screen''. It will resemble the following:

If you've never used a multi-user operating system before (DOS, Windows, and Macintosh are all single user.) the concept of ``logging in'' will be new to you. FreeBSD is waiting for a login - a login is simply the name that you supply to FreeBSD to identify yourself to the operating system. FreeBSD keeps track of which names are permitted to log in or access the system, and only allows valid users to have access. When you successfully log in, you'll be presented with an interface to the operating system - the Unix Environment covered later in this chapter. Generally login names reflect a person's real name (for example, on my home system my login name is mark on a larger server at the University, my login name is mmayo - to distinguish me from all the other Mark's on the system). Login names must be unique on a particular system.
To gain access to the system, you type in your login name. If you are
booting FreeBSD for the first time, the only account that is valid is that
of the SuperUser: ``root''. Type root at the Login: prompt and press ENTER If you specified a password for root during the installation
you'll be asked for it now; otherwise the system will immediately log you in as
root. If you are new to Unix, you would be wise indeed to read the section
entitled The SuperUser presented later in this chapter.
Things to remember:
FreeBSD login names and passwords are case-sensitive. Root is different than root, ROOT, and rooT. With out strong reasons, it is suggested that you use all lowercase for your user names, keeping them to 8 characters.
The root login does not restrict you in any way. You are god of your OS. With one simple command, you could wipe out everything! Cool huh? For this reason, use the root login only when necessary. Avoid experimenting with commands when you are root.
System administration is another term you will see often. A system administrator is the actual person who sets up and maintains the FreeBSD (or any other) system. You are the system administrator of your home PC, but your roles (fortunately) are far less consuming than the system administrator of a large public server. System administration generally requires SuperUser privileges.
One of the most useful things about FreeBSD is that you can ``log in'' from just about anywhere! FreeBSD uses the notions of ttys - ``terminals'' that can attach to a virtual terminal (ttyv - such as your screen), a pseudo terminal (ttyp - such as a network connections from another Unix or Windows computer), and serial terminals in case you happen to have an old DEC vt100 terminal device lying around :-) Terminals, combined with the concept of logging in make FreeBSD/Unix a dream to administer remotely! Another side-effect of the multi-user ttys is that YOU can be logged into your own system as many times as you want. If you've just started FreeBSD for the first time, then it may not be obvious too you how to get multiple logins going at the same time. It's easy, just hit ALT-F2 to switch to ``virtual console 2'' and you'll see another login screen. By default, FreeBSD ships with 3 virtual consoles (ttyv0-2), reachable with ALT-F1,ALT-F2,ALT-F3. Go ahead and try switching between consoles! Having multiple sessions (logins) going can be extremely useful. For example, say you want to search for the word ``foobar'' in the entire /usr/src source tree! Depending on the speed of your disk and CPU, this could take quite a while - say 2 minutes - so instead of waiting around doing nothing while the command grinds away, you can switch to another console and continue working on whatever you want.
Logging Out!: Logging Out!When you're done doing whatever you were doing it's time to log out! You can either type logout or exit. Once you've logged out, you'll see the Login: prompt again, indicating your system is ready for more abuse :-) |
Note: The term ``account'' is synonymous with ``login'' Some people use the phrase account to encapsulate a user's entire set of rights and possessions on the system (his login permissions, files, home directory, mail account, etc.), and use the term login to refer to the user's ``account name'' At any rate, the two terms can be used interchangeably and their exact meaning varies depending on who you talk to :-) For clarity, we will always append ``name'' after account or login to indicate we're only talking about the user's login name... |
In FreeBSD (and just about all other UNIX system) the superuser login name is root. No mater how large or small the system may be, if you can login as the user root, the system is wide open for you to do whatever you please. Obviously, letting anyone log in as root is a Bad Thing(tm). Remember, root can do *anything*. Only you should know root's password.
To prevent unauthorized access, the root login should always have a secure password. By default, in FreeBSD, root does not have a password; it's up to you to change it. The trick is picking a good password. It's been known for quite some time that people tend to pick passwords that they can easily remember (we're humans, after all): their birthday, their home town, an item on their desk, and so on. The problem with these passwords is that they are easy to break, either through guessing or by more sophisticated methods of attack.
So picking a ``good'' password is important if your machine will be connected
to a network (a LAN, or the Internet). The best passwords combine a mixture
of Upper and Lower case characters, as well as numbers. A fine example would
be: 34DodgesU, or Beezl891. It's even better if
there are no ``real words'' in your password, since one common form of attack is
to exhaustively try common combinations of words and numbers against your
password entry. Personally, I think a password should be pronounceable, since
it minimizes the risk that you might forget it. Try memorizing
S8t4WxlT43Dc23HiiU2 and you'll see what I mean...
Finally, perhaps the best approach to password privacy is to never write your password down, and to change it every so often. There are system administrators that change their (and root's) password several times per week. Many people recommend a more reasonable time frame of 2 to 3 months.
Also remember that you should only be the SuperUser while you are performing System Administration tasks. For 'normal' activities, it's always a better idea to do them as a mere mortal since mere mortals can't wipe out the entire operating system so easily.
To ``become'' the SuperUser while you're a normal user, you need the command su. su stands for ``switch user'' and you can use it to su to any other user on the system. If you just type 'su' by itself, it is assumed that you want to su to root - the SuperUser.
Warning: In FreeBSD, you must be in the wheel group to su to root. |
So go ahead and put yourself in there now. If you have no idea what I'm talking about, just wait until we get to the section ``Creating/Adding Users'' and it will all be explained.
The Unix environment, for the most part, involves two aspects:
The Shell
The User Environment
I can't really tell you exactly what the Unix environment is - but I can tell you what most people believe contribute to "it". Really, the Unix environment is just FreeBSD's interface onto itself. Huh? Think of it this way: You need to get things done, and the operating system is willing and ready to perform these tasks. But you need a way to talk to FreeBSD before you can go anywhere. The "Unix Environment" is the collective services, features, and ideologies that represent the interface to the operating system. If you want to work with FreeBSD (or any Unix for that matter), you need to use the Unix environment to get your work done. As we will see, the Unix environment not only ``lets'' us get our jobs done, but influences how we approach and solve problems. I'll talk about this philosophy of design after a brief introduction to ``The Shell'' We will end up this section with a hands on walk through of how to use the shell, and the rest of the Unix Environment.
The most visible part of the Unix system -- the part that prompts you for commands and appears to do your bidding is the shell. A shell is a user interface: it talks to the Operating System for you and grants each logged in user system resources (like processor time, disk storage, memory, etc.). The primary purposes of the shell are to provide prompting, command interpretation and execution, and error reporting. You tell the shell what you want done by typing in commands after the prompt. It's very similar to the command interpreter under DOS, but infinitely more powerful and functional. Instead of presenting users with "C:>", most Unix shells present you with "$" or "%". Naturally, the prompt can be changed - more on that later!
There are many different Unix "Shells" available. By default, most people will be using the ``C Shell (csh)'' under FreeBSD. You can tell if you are using this shell if your command prompt has an "%" in it. The C Shell is thus called because it is modeled after the C language syntax. For added functionality (including command line editing and an interactive ``history'' mechanism), you can use "tcsh", which has become quite popular and has very similar syntax to the csh. Also available are the Bourne Shell (sh) and the Bourne Again Shell (bash). bash is the default on Linux systems, whereas tcsh or csh are generally the defaults on BSD systems. Finally, there is also the Korn Shell (ksh) written at Bell Labs - ksh has become a de facto standard on System V based UNIXes. There is a free implementation called "pdksh" (public domain ksh). Many people consider the tcsh the most feature rich, but that doesn't mean it's right for you. The only way to know for sure is to try them all out and see which one turns your crank. tcsh, bash, and pdksh are all available in the FreeBSD Packages Collection.
Note: Although you spend most of your time in what's known as the ``interactive shell'', most shells also provide a high-level programming language (scripting language) that is typically used to automate mundane day to day tasks. That being said, Perl is quickly replacing this use of shell languages in many sysadmin circles, and for this reason we will only look at the shell from an interactive stand-point. If you do want to get into shell programming, there are many books available on the subject! |
Before we get started with how to maneuver around your system with a shell, we need to get several things out of the way first. When the powers that be created Unix, they had a certain idea, or goal, about how it should work. This is loosely referred to as the "Unix philosophy". It can be summed up in the simple phrase: "Small is beautiful". The philosophy has grown from the UNIX community instead of being forced upon it. According to Mike Gancarz, in ``The UNIX Philosophy'', there are 9 major ``tenets'' of the Unix philosophy. Several are significant to your understanding of how to make the most out of your FreeBSD system:
Small is beautiful
Make each program do one thing well
Make every program a filter
The idea here is that small programs are simple, and therefore easier to understand. A natural side effect is that a program should only do one thing - if you want more than one thing done, you just connect different individual programs to accomplish your task. That's where filters (also referred to as pipelines) come into play. Every program is a filter in the sense that it produces an output given an input. The output of one program can be fed into the input of another program, and vice verse. You will see what I mean once we get to some concrete examples in the next section.
Personally, I use tcsh as my shell, and for this reason we'll focus on this shell in particular as we walk through several examples of how to take full advantage of your shell. tcsh is a modified version of the C Shell (csh). It's fully backward compatible with csh, but it has many new features that make user interaction much easier and more efficient. The biggest areas of improvement are command-line editing, and history navigation. For me, these make life on the command-line much more enjoyable. Everything we'll say about input and output redirection, pipelines, prompts, and job control is identical to the standard csh. If you don't have tcsh installed on your system (just try typing 'tcsh' to find out) then you can add it from the Package Collection.
Pipes and Filters
tcsh provides a mechanism to connect programs together - the filter mentioned above. It is called "the pipe", to represent the directional flow of data "through the pipe". With tcsh, the pipe is represented with the "|" character. Before we can use a pipe, we need to know two UNIX commands: who, and wc. who gives a list of each user that is currently logged onto the system, while wc counts words. Let's say we want to find out how many users are on the system. Here's how you ask:
vinyl % who | wc -l 8 vinyl %
Whoah! There's quite a bit going on here. First of all, you should recognize that the
"vinyl %" is my interactive prompt (I've made a simple modification to
the prompt to let me know what machine I'm on - in this case ``vinyl''). It's begging me for commands, and I fed it the
command "who | wc -l". The answer was 8. Let's take a closer look at what happened.
Normally, the command who by itself would perform like this:
vinyl % who mark ttyp0 Jun 20 18:04 (131.111.238.108) dlow ttyp1 Jun 9 10:53 (204.27.111.42) chrisc ttyp3 Jun 20 17:46 (208.8.24.42) gabor ttyp4 Jun 19 13:10 (206.222.78.163) . . . vinyl %
Notice that there is one user per line. We want to take this result and do something
with it. In this particular case, we want to count the number of lines, which will be
the equivalent of counting the number of users currently online! So what we do is ``pipe''
the result of the who command into the wc command. wc sees the
output of the who command as its input, and processes it. The final thing to note is that
we gave the wc command a "command line option"; the -l part. "-l" simply tells
wc to count lines instead of words.
The example above demonstrates quite a bit about the UNIX command line. If you're
confused, the best way to learn is by picking a few commands and experimenting. One
of the most common uses of the pipe on the command line is with the programs "more",
or "less". Anytime a program gives an output result that is more than a page long, the
best way to view it one page at a time is to pipe the result into more. For
example: last | more. This lets me see the output of the command last (
which outputs a list of all the users who have "last" logged onto the system) one page
at a time. Quite useful indeed.
Command Completion
Command completion exists in the tcsh to make your life easier. With command completion, the shell is able to determine which files you are interested in on it's own. It will become clear what I mean once you see the following example. Remember the the cd command "changes directories". Lets say you have the following directories and files in the current working directory:
vinyl % ls -F News/ bin/ code/ games/ mail/ resume.ascii test/
If you want to change directories from the current working directory to the test subdirectory, you would enter the command:
vinyl % cd test
With command completion, you can save on your typing by recognizing that the directory name 'test' is uniquely identified by the its first letter, 't'. So all you need to type is:
vinyl % cd t<tab>
The <tab> is of course the actual tab key found on your keyboard. When you hit the tab key tcsh will fill in the rest of the filename for you! This can be a real help with longer file names and makes moving around on the command-line much quicker. Instead of the Tab key, and the standard csh uses the ESC key.
Wildcards
Wildcards allow tcsh to match more than one file at a time. It supports the three standard file wildcards supported by pretty much all shells:
* matches any character or any number of characters
? matches any single character
[...] matches any single character contained within the brackets
The * wildcard can be used to do the same thing as command-completion above. If you entered a command like
cd t*
and only one subdirectory in the current working directory begins with the letter t, this command would behave the same as if you had used command-line completion by pressing the Tab key. The * matches any number of characters, so in this case it will match anything behind the leading t character, such as 'test' from the example above.
A more common use of wildcards is for working on multiple files however. Often the ls is used as follows:
vinyl % ls -l *.html
This will show you all the files that end with '.html' -- your web pages. The best way to get a feel for wildcards is to just play around with various combinations. Look at the results and figure out how the command behaved. After a very short period of experimentation I'm sure you will have wildcards mastered!
Command History
Coming soon...
Sprucing up TCSH's default behavior
Both csh and tcsh read configuration files when they start - .cshrc and .tcshrc respectively. tcsh will also read the .cshrc file if no .tcshrc file is present. You can customize the behavior of the shell, and set environment variables in the startup files. Remember that you need to use ls -a to see files that start with a period!
Skip ahead to the Sample Configuration section for an example of a typical .tcshrc and .cshrc config file.
Changing passwords in FreeBSD is quite simple. It is accomplished with the command passwd. When you type in the command you get:

Note that my first two attempts at a new password were not accepted. First, I picked a password that was too small, and then I picked a password that was all lower-case letters. Both passwords are easy to guess or crack, so FreeBSD refused to let me choose such weak passwords. My third attempt was more than 6 characters, and was a mix of upper and lower-case letters - that one worked. After you enter a suitable password you are asked to retype it to make sure you didn't make a mistake. That's it!
If you are root, you can change anyone's password (although you can never
learn what someone else's password is) by typing passwd fred (for example). As
root, you can also insist on an insecure password by ignoring the warnings.
If you're on your home machine, and it's never connected to the Internet,
this is probably fine.
The other related command you might want to investigate is chpass. It will let you set things for your login like what shell you want to run, your full name, and what your Office and Home phone numbers are!
Finally, if you ever want to make changes to the password file directly, you use the simple command vipw. Note that this stands for ``vi password'' which implies that you can get around in vi. (In case you were wondering, vi is an excellent text editor that is pretty much the standard on all UNIX Operating Systems - we explain it in full later on in the book.) It's a good idea to see what the password file looks like, and after you're comfortable in vi please try this command out. As with all the FreeBSD password utilities, if you do make a change, vipw automatically rebuilds the password database for you.
Adding users in FreeBSD is simple, since there is a handy little system command that does all of the work for you: adduser. Adding a user to a system involves setting up a few things, and before we step through the adduser command, it would be best for you to know exactly what has to change on your FreeBSD system for a user to ``exist''
First of all, FreeBSD must know what users are allowed to login, and what their passwords
are so it can validate the login requests. All of this information is stored in the
``password file''. A plain text version of this file, suitable for human consumption,
is located in /etc/passwd. Go ahead and take a look at this file (type cat /etc/passwd).
You should see a bunch of lines that look a lot like:
dirk:*:1061:1061:Dirk Jessberger:/home/dirk:/usr/local/bin/tcsh
This line contains most of the information needed about the user named ``dirk'' The line is separated into separate pieces of information by the : character. The first part should be obvious - it's the ``user name''. The next two numbers are the ``user id'' and the ``group id''. Every user in FreeBSD belongs to a primary group (and as we will see below, can belong to many secondary groups). The next section is dirk's full name, ``Dirk Jessberger'' The last two sections say where dirk's ``home directory'' is (the location on the file system that belongs to him), and what shell FreeBSD should start for him when he logs in.
Information about what other groups dirk belongs to, as well as the name of his primary group (group pid 1061) is stored in the ``group file'', which you can view by typing
cat /etc/group. You see lines like this:
dirk:*:1061:dirk geeks:*:1023:mark,tburgess,sarahs,jgturner,pwardrop,gabor,karen
The format is similar to the password file, and in this case the first section of the line says that the group called ``dirk'' is known by the ``group id'' 1061, and the user ``dirk'' belongs to this group. In other words, the user dirk belongs to his own group: dirk. This is the default - users have their own groups. You'll see in the second line, however, a group called ``geeks'' with ``gid'' (group id) 1023, and a whole bunch of people who belongs to the group ``geeks'' A little later, when we deal with file permissions, we will see why this type of situation might be desirable.
Finally, when a user logs in, they need a ``home directory''. A home directory is owned by the user, and is ``where you put your stuff''. Your home directory also holds your ``dot files'', which are config files (such as .cshrc) that tell various programs how to behave. In FreeBSD, user home directories are usually located in /home.
Now that we've seen what FreeBSD needs to know in order to host a user, lets take a look at the program adduser. Adduser checks the passwd, group, and shell databases (which are built from the text files mentioned above) when adding users to make sure everything is valid. Adduser also creates a passwd/group entry for the user, a HOME-directory, dotfiles and sends the new user a welcome message! As you can see, the adduser program does pretty much everything for you, and saves a lot of time compared to the old method of manually setting up an account for a new user.
Adduser is an interactive program, meaning that by default you start it up with no arguments (command-line options) and it asks you questions, which you answer, until it has all the info needed to add the user to your system.

Start in Late July.
Start in Late July.
FreeBSD is a sophisticated, multi-user, multi-tasking operating system. It's not DOS. You can't simply ``power off'' with the power button on your PC without first telling FreeBSD that you wish to shutdown. If you want to know why, continue reading. Otherwise, just skip ahead to the next paragraph for the necessary commands. When your FreeBSD system is up and running, it goes through a lot of measures to optimize performance. One of the biggest ways to optimize performance is through the use of ``buffers''. When you read or write to the disk, data is moved into and out of buffers. You can think of buffers as buckets. Lets say I have a hose running, and it only reaches so far. Unfortunately, I'm still 5 meters away from my pool, and I need to get water into the pool. Obviously, it's not very efficient for my to take one glass of water at a time from the hose over to the pool. I'd use a bucket - or more precisely, I'd place the bucket under the hose, and when the bucket fills up I'd dump it into the pool. Get it? The FreeBSD equivalent is: it's slow to write to the disk, so I want to minimize the trips I have to make to the disk. So when I'm writing a file, I place the information I need into buffers until I have enough to make it worth while to go and visit that slow old hard-disk. Then I can write everything out at once. Since buffers are stored in RAM, if you just cut the power to your computer, there is a chance that some of the information you thought you wrote to the disk never actually made it there! And that would be crappy. As it turns out, FreeBSD (actually, the filesystem FreeBSD uses) ``syncs'' data from the buffers out to the hard disk every 30 seconds - but even then there is no guarantee that sync will move it to the disk right away. If the system is under low load (a measure of how busy the CPU is - usually very low on a workstation, but it could be quite high on a multi-user server) then it's unlikely that data will be sitting in a buffer for more than a couple of seconds. So, you need to tell FreeBSD to take whatever is sitting buffers and dump it to the disk. Hence, the shutdown command. It slices, it dices, it sync's, and it notifies! What more could you ask for?
To shutdown FreeBSD, you use the shutdown command! Depending on what arguments you give it, it will behave differently. For example:
shutdown -h now
Tells FreeBSD to shutdown, halt the CPU, and do it now.
shutdown -r now
Tells FreeBSD to shutdown, and then reboot the system. Do it now.
reboot
Does the same as shutdown -r now.
When you do the shutdown command, a broadcast message will be sent to all users
currently logged in that the system is going down. Instead of ``now'' you could say
shutdown -h +5 which will bring the system down in 5 minutes. Every
minute FreeBSD will send a broadcast message to all users warning of the impending
shutdown. If you just want to kick everyone off, shutdown -k now will
do so, and will also prevent anyone else from logging in!

In summary, using the FreeBSD shutdown command is not only the ``safe'' way to stop your system, but also the ``friendly way'' :-)
Start in Late July.
The FreeBSD manual pages (or manpages as they are usually called) describe nearly every user command, administrative command, library call, and file format used on the system, plus some other informative tidbits (like a built-in ASCII table). Each page includes a summary, lists all the options available with the command, and generally goes into detail about the command and refers you to related pages.
Man page information is only available on actual commands, not to aliases you set up in your .cshrc file.
To access the Manual Pages, type in man command at the Unix Command Prompt with out the quotes. Substitute the word ``command'' with the command that you are interested in learning about. For Example:
/usr/home/chris> man ls
The above command will generate a manual page explaining the use ofls. While you are viewing the man page, you can control the screen the same way you would control a more session.
Pressing these keys will result in the following:
"q" Will quit the current man session.
"b" Causes Man to go back to the previous screen.
"j" Causes Man to go forward one line.
"k" Causes Man to go back one line.>
"<SPACE>" Causes Man to go forward one page.
Note: In order to access the online man pages, you must have the man distribution installed on your system. |
man searches all the directories specified in the MANPATH environment variable to find the page you requested.
You can adjust the MANPATH search string to include custom man page directories so you can add your own man pages. Man Pages are divided up in to Nine sections. The first of these contains all of the user commands.
Almost every install option in the FreeBSD sysinstall includes the manual pages. So unless you have selected the custom install and didn't choose the man pages, you should have access to them on your system
When you are new to UNIX, especially when converting from a non-UNIX operating system like DOS or MacOS, figuring out which commands do what you want can be very frustrating. Most of the help features built in to UNIX are designed to help you figure out how to fully use a command AFTER you have discovered it.
One of the first commands that you have to learn is ls ls is a command to ``list'' or display all of the files that are present in your current path. In order to comprehend all the things that ls can show us, we need to first understand how UNIX treats files.
There four main types of files:
Regular files
Directories
Devices and Special files
Linked files
Regular files include programs, data files, pictures, binary executables, etc... These account for almost all of the files that will exist on your system.
Directories are files that allow you to contain other files in them. On windows and the Mac systems they are called "folders." All regular files are contained inside a directory. Directories can be contained in side of other directories, these are called sub-directories. Dos users will notice that the directory separator in UNIX is the opposite direction from that in DOS. For more information on directories see the man pages on pwd cd mkdir and rmdir
Devices are accessed through special files that are contained in the /dev directory. These files allow you to control various parts of the computer, such as your modem or floppy disk. Device drivers access these files to give instructions to things like your mouse or sound card.
Also reference the man pages for: MAKEDEV, mknod, and
Link files are a lot like pointers in C. Links come in two types: real links and symbolic links. Real links are actually another name for the file. A real link has a few limitations, it can't be linked to a directory and it must exist on the same physical disk and disk partition. However symbolic links, or symlinks as some people call them, don't have this limitation. A link to a file allows you to move the file from it's original place, and still allow programs that depend on it to find it. A Symlink can be thought of as a shortcut to a file. ``Shortcuts'' in Win95 work exactly the same way. Deleting the symlink doesn't delete the actual file, just the shortcut to it. For more information on links, see the man page for ln
Every file has certain properties. These consist of:
X|Kind of File| v|Read Write|X|Number of Links| v|Execute |v| | | | | |time| | v|permission|v|Owner| Group | Size |month|day|year|File Name| crw-rw-rw- 1 root wheel 2, 12 Aug 28 1996 zero brw-r----- 1 root operator 0, 7 Aug 28 1996 wd0h brw-r----- 1 root operator 0, 146 Aug 28 1996 wd0s3 brw-r----- 1 root operator 0, 682 Aug 28 1996 wd0s4 -r-xr-xr-x 1 bin bin 45056 Feb 28 04:05 cat -r-xr-xr-x 1 bin bin 53248 Feb 28 04:05 chmod -rw-rw-r-- 1 chrisc bsdbook 416 Mar 24 23:09 .cshrc -rw-rw-r-- 1 chrisc bsdbook 420 Mar 24 23:09 .login -rw-rw-r-- 1 chrisc bsdbook 0 Mar 10 13:38 help drwx------ 2 chrisc bsdbook 512 Apr 3 01:46 mail drwxrwxr-x 5 chrisc bsdbook 512 Mar 24 23:22 public_html lrwxr-xr-x 1 root wheel 11 Mar 23 22:56 sys -> usr/src/sys
Files that begin with a ``.'' are hidden files, and are not usually displayed by ls and by default they are not acted upon by most other programs.
There are two special files that exist in every directory: . and .. these refer to the ``current'' and ``previous'' directories respectively.
Most of the work you do as a system administrator involves text files. Either you are checking a config file, or viewing a log file. To accomplish the job of displaying text files, Unix provides several different tools.
cat is short for concatenate. If you don't redirect it elsewhere, cat will display a file to the standard output, namely the monitor you are looking at. cat displays the file as fast as possible, and will not stop for page breaks. For Example:
cat myfile
will display the contents of ``myfile'' to the screen. However, if that file happens to be a non-text file, such as a binary file, you would see all of the binary escape codes; it might not be a pretty sight.
If more than one filename is specified, cat will display them one after the other, in order, without pausing, as though they were one file. By using the redirection operator > you can concatenate two or more files together and create a third file that contains all the information from the other files.
For example:
cat file1 file2 file3 > file4
In this example, file4 would contain all the information from files 1,2, and 3. It would not matter if this was a text file or a binary file, because the information would never be shown to the screen.
But what if while we are using cat we have a file that is too long to fit on one screen, and we really want to read what it says as it scrolls past us at sub-light speed.
Over the years people have come up with several programs to make the display pause for page breaks and let us read the page before showing us a new one. One of the first and more popular ways is more Two other commands, less and page have recently become popular. Currently, less is just a hacked version of more Fundamentally more works just like cat except more pauses for page breaks and prompts you to press a key before it continues.
However, it isn't always easy to tell which key to press, because it doesn't tell you. It just sits there with a ``stdin'' in the lower left hand corner. It assumes that we have read the man page, and know that ``stdin'' stands for ``standard input device'' ie. the Keyboard. It is waiting for us to press the SPACE BAR to go one page forward, the ENTER key to go one line forward, b to go back a page, or q to quit out of it.
By using the pipe operator | we can route all the information through more and make any program that is displaying text to the screen slow down and pause at page breaks. For example, a ls -lR / will display all the files in our computer. This would be very hard to read, and we wouldn't get much out of it. But if we do a ls -lR / | more this will show us the directory listings one page at a time and also allow us to stop by pressing q
Sometimes, however, we have a real long file and we are only concerned with the last page of information. It would be silly to use cat or more and wait all the way through 1500 pages of text just to see the last page. Log files are a prime example of a situation like this. For example, you are checking the log for the web server and you want to see who the last few visitors are, or you want to check the last couple of messages in /var/log/messages. In situations like these, it is a good idea to use tail.
If we wanted to see the last messages in /var/log/messages, type:
tail /var/log/messages
The last page of the message file will be displayed on your screen.
head works just like tail except it show only the first page full.
Also reference the man pages for: more less head page tail cat
For more information on these commands, see the man pages for: more, less, head, page, tail, and cat
Files, they seem to be everywhere; in FreeBSD this is very true. Except they never seem to be where you want them. There are basically three things that you can do with files: Create them, Use them, and Remove them. The rest of the time is spent moving them around from place to place in your file system. To move files around in a file system, you have to have places to move them to and from. These places are called directories. Directories are really files that contain other files. Files can be placed inside a directory and moved from one directory to another.
Directories can be placed inside each other. A directory contained inside of another directory is called a ``sub-directory''.
To move a file or directory, we need to use the mv. mv requires that you have at least to parameters when moving files. (Some people call these ``arguments''.)
The first parameter is the source file, or the name of the file you want to move. The second parameter is the destination, or the place you want to move the file to. If you specify a directory as the destination, it will place that file in the directory specified. If you give it a file name as the destination, it will rename your file. If you move a directory using mv, it will rename the directory.
For Example:
mv /usr/local/junk.html /usr/local/www/ mv /usr/local/html /usr/local/www mv /usr/local/junk.html /usr/local/www/index.html mv /usr/local/junk.html /usr/local/www/ mv junk.html junk.htm mv /usr/local/html/* /usr/local/www/
Security is a big issue in today's ``anyone-can-access-your-site'' Internet environment. For the new-to UNIX user and even system administrator, keeping access permissions set properly on files can be a major chore.
Permissions to a file allow a user to read, write, or execute a particular file based on whether he is the owner of the file, a member of the group that owns the file, or an ordinary user that is trying to use the file. UNIX lets you set the accessibility permissions for each of the three categories. Individual file permissions are set with the command chmod. The command umask is used to set the default permissions that a file gets when it is created. chmod and umask use a number and column scheme to represent the particular permissions and the category that they apply to.
There are four columns and four numbers (if you count zero). The columns represent the categories that the permissions apply to and the numbers are the read and write permissions.
The Columns: |column1 | column2 | column3 | column4| |Special | Owner | Group | Others | The Numbers: In Column 1 only: 4 |Set User ID On Execution. 2 |Set Group ID On Execution. 1 |Set the Sticky Bit. 0 |Remove all Special options In Columns 2-4 4 |Grant Read Permissions 2 |Grant Write Permissions 1 |Grant Execute Permissions 0 |Remove all permissions from column
If you specify less than four digits when setting permissions it will
assume that you are starting from column 4 and work backwards, in other
words chmod 22 file will set the read permissions of file to ``write'' for
group and others. chmod 24 file and chmod 0024 file are exactly the
same. This however will remove all permissions from the user, a better one to use would be chmod 644 file for standard files and chmod 755 file for executable files. You can have no more than 4 digits, each corresponding to a
column.
You can set both read and write permissions to a column by adding the
numbers together. Write(2) + Read(4) = Both Read and Write(6). Therefore
to set read and write permissions to ``Owner'', ``Group'', and ``Others''; you
would use chmod 666 file
The file ``bsd.gif'' has been set to mode 664 using the command chmod 664 bsd.gif
An ls -l will display the permissions of all the files in the current directory.
>ls -l total 21 drwxr-xr-x 2 chrisc bsdbook 512 Mar 21 00:50 articles -rw-rw-r-- 1 chrisc bsdbook 926 Mar 21 16:01 blueball.gif -rw-r--r-- 1 chrisc bsdbook 1901 Mar 21 23:50 book.html -rw-rw-r-- 1 chrisc bsdbook 7100 Mar 21 15:57 botbar_raw.gif -rw-rw-r-- 1 chrisc bsdbook 2088 Mar 21 15:57 bsd.gif drwxrwxr-x 3 chrisc bsdbook 512 Mar 22 00:02 cgi-bin drwxrwxr-x 2 chrisc bsdbook 2560 Mar 21 15:50 docs -rw-rw-r-- 1 chrisc bsdbook 0 Mar 22 00:15 file -rw-rw-r-- 1 chrisc bsdbook 2865 Mar 21 15:47 umask.sgml ^^^^^^^^^^ These are the permissions.
The 'd' tells us which files are directories. Directories must have execute permissions enabled in order for a user to change directories to it. A '-' in the first field signifies an ordinary file, in the other fields it signifies a lack of permissions, or a permissions of '0'.
When a file is created the default permissions are set at 666. Umask does just the opposite job of chmod. It removes permissions from the default values at creation time based on the number and column scheme. Therefore to have your files set to read and write by ``Owner'', but read only by ``Group'' and ``Others'', you would use a umask of 22. The Line
umask 22
can be put in your .login file and automatically set every time you login.
The Default Permissions: 666 Your Umask Values: 22 ____ Your New Default Permissions: 644
Now every time you create a file it will have the New Default permissions. A umask of 66 would give you Default permissions of 600, giving only the owner read and write access to the file. chmod can also modify permissions to files using a ``first letter'' short notation. This style of using chmod works exactly the same as the Column and Number Scheme. However, it is easier for new users to remember and use. Consequently it gets a lot of use in everyday tasks, while the Column and Number Scheme gets a lot of use by programs and scripting languages.
Permissions Types: r Read w Write x Execute Affected Area: (column) u User g Group o Others a All Method Affected: + Add to - Remove from = Set equal to
To add execute permissions to all areas of the file ``bsd.gif'', you would use chmod a+x bsd.gif
To remove read permissions from ``Others'' use chmod o-r bsd.gif.
Now you can control the initial permissions of files and modify those permissions later to suit your needs.
In FreeBSD there are several text editors available; one of the most powerful and common is vi. However, to people who haven't grown up using it, learning vi may appear to be the hardest part of System Administration. Used properly, vi is one of the best tools for System Administration. When learning vi the first thing that must be understood is that vi is not a word processor. It is a text editor designed to edit the configuration files that are used by the system; at this job it excels and is (in my opinion) easy to use.
The biggest problem is that vi doesn't even pretend to be user-friendly. If you don't know how to use it, there is absolutely nothing to tell you which key to press to accomplish the required task. Generally, all it does is beep at you. To the advanced user, the lack of help menus is a "feature" not a draw-back, because the screen is not cluttered with unused help features.
Because of the lack of help features in vi, and the difficulties most people have learning it, other text editors have been created. One of those is vim, or ``vi improved''. vim has taken all of the functionality of vi and made it easier to use.
A program named pico from the University of Washington, is a very easy to use text editor. It provides a lot of help features and opens automatically into ``text entry mode''. FreeBSD now ships with the ee editor as the Default. It is very easy to use, but cluttered with help screens.
Unlike most other editors, vi has three modes: Command Mode, Text Entry Mode and EX mode. In Command Mode, each key you press is part of a command. Cursor movement, deleting lines, searching, and such are done in command mode. When you start vi, it starts you out in Command Mode and you have to Enter the correct keys to get to Data Entry Mode.
From Data Entry Mode you can enter text just as you would in a normal text editor. There are several commands to start Text Entry Mode, revolving around what you want to happen prior to entering the text.
You will remain in Text Entry Mode until you press the ESC key, which will return you to Command Mode.
To start a new document, type vi from the command line followed by the name of the file you wish to create.
This will put you into a clean document, provided you have not named an already existing file, and start you off in Command Mode. Now any key entered will be interpreted as a command and vi will promptly beep at you if you press the wrong one. At this point press a to begin entering text. Now enter in several lines of text; if you don't press return, vi will treat this all as one line, with no word wrapping. This is an important feature of vi in System Administration. Some configuration files require that certain lines all be entered on one line.
vi will also show you all the escape characters contained in the text. This is a very helpful feature if you are working with special file formats. Editors like pico will often not display special characters. They will sometimes even remove them.
Press the ESC key to return to Command mode. Use the h and l keys to move the cursor left and right. In vi all the commands are case sensitive. The following commands will move the cursor in various ways.
h One Space Left.
l One Space right.
j One Line Down.
k One Line UP.
w Forward/Right One Word.
b Back/Left One Word.
G Goto Bottom of Document.
Many of the other commands can be combined with these motion commands.
Now position your Cursor at the end of a word and press a. The cursor moved one space forward and is allowing you to append text after that word. Press the ESC key and return to Command Mode. Position your cursor at the end of the same word. This time press i and type some characters. The letters were inserted in front of the cursor position. The following command will allow you to enter Text Entry Mode in various ways.
a Append after the cursor position.
A Append at the End of the Line.
i Insert before the cursor position.
I Append at the Beginning of the Line.
o Open a new line Below the cursor.
O Open a new line Above the cursor.
y Copies or "Yanks" a line *
d Deletes *
c Changes *
p Pastes Below Cursor
P Pastes Above Cursor
* Note: These need to be mixed with motion commands to make these effective. Pressing the same key affects the current line. (ie. |
Position your cursor on a line with text on it and press dd This will delete the current line and save it to a temporary buffer. you can restore it by pressing p.
/ Search
n Find the next match.
. Repeat last Command
u Undo/Redo
The search, repeat, and ``find next'' commands allow you to do a search-and-replace in a very controlled manner. For example, you have a document that has the word me in it several times. You want to change it to the word ``you'' To do this you would search for ``me'' by typing /me. Your cursor will jump to the nearest word containing me.
Then type cwyou and press the ESC key. This will change the word to you.
Now you are set up to search for and replace all the other ``me'' words in the document. To find the next occurance of ``me'' press n to change it to ``you'' press .; if you don't want to change it, press n to go on to the next.
/me will match any word containing me, words like: reames,mess, and mean. Use /\ me\ to find only the word me.
w < filename > Write the file
q Quit vi
w! < filename > Over Write an existing file/force write
q! Quit without saving/force quit
r < filename > read in a file
!ls execute the shell command ls
123 goto line 123
When you press the : key, a : will appear in the lower left hand corner of the screen. This is a prompt that will accept a limited number of commands. It will let you execute shell commands, read in files, or quit vi. When you are done with vi, press :wq and you will save your file and quit out of vi.
To read in or write to a file, you need to supply a file name. If you supplied a file name when starting vi, it will use it by default. Giving a filename at this point would override the one you supplied at startup.
In command mode, if you enter a number, it will repeat the next command that many times. For example, if you are deleting lines, typing 10dd will delete 10 lines. Motion commands may be used also; 10dk will delete 11 lines above and including the one the cursor is on.
In-depth help for learning the vi editor.
vi has basically three modes, 1) command or normal mode, 2) insert or input or append mode 3) ex or command-line mode. vi starts in command mode where you can issue commands, move and/or cut/copy text. The : (colon) puts you in ex mode where you can use powerful ex commands like g, s, v, ! etc. certain commands will put you in insert mode, eg. i, I, a, A, o, O. When in insert mode you can type in text. R will put you in a special case of insert mode, that is overwrite mode ^[ (escape key) will put you in command mode.
SYNTAX OF VI COMMANDS
most commands in vi have the following form ( exceptions are : m, M, n, N, p, P, u, U ) [n] operator eg. 5x delete 5 characters 3ANO! will insert NO! three times at the end of the line if you hitESCafter typing the ! 2fa forward to second a on line 3Tc backward to after the third c 20j move cursor down 20 lines 5H move cursor 5 lines from top of screen 5rx replace next 5 characters with x 5$ move to end of 5th line down (line 1 is current) 3_ move to first non-blank character 3 lines up (line 1 is current) editing commands(c,d,y,>,<,!) have the following general form [n] operator [m] object an object in vi would be a character or a word or a line or a sentence etc. basic operators for editing are c begin a change d begin a delete y begin a yank if the current line is the object of the operator then the object is the same as the operator: cc, dd, yy. if both n an m are specified the effect is n * m, that is n times m. eg. cw change word c2W change next two WORDS (includes punctuation) (could also be 2cW) 2cc change two lines c$ change to end of line c) change to end of sentence 5dd delete next five lines d5G delete to line 5 dG delete to end of file de delete to end of word d^ delete to beginning of line d30| delete to column 30 on line yy yank current line y'z yank to line marked with z y]] yank up to next section yL yank to bottom of screen y2fq yank to second q on line forward commands in vi are not echoed to the screen except the following: / search forward for pattern ? search backward for pattern : invoke an ex command ! invoke a unix (OS) command that takes as its input an object in the buffer and replaces it with output from the command eg. 1G!Gsort -fd the above commands are completed by pressing the return key a buffer in vi would refer to the file as it's kept in memory <, >, and ! can also be combined with a movement command like c, d, and y >> indent current line one shiftwidth 5<< outdent 5 lines starting with current >>% indent to matching parentheses !Gsort -fd sort from current line to end of file
QUICK HELP ( commands and command combinations to get you statrted in vi )
MOVEMENT Character: h cursor left j cursor down k cursor up l cursor right space cursor right Line: [n]G to line n 0, $ first, last position on line +, - first character on next, previous line ^, _ first character on current line (other than tab or space) Screen: ^F, ^B scroll forward, backward one screen ^D, ^U scroll forward, backward half screen ^E, ^Y show one more line at bottom, top of screen Marking position: mx mark current position with x `x move cursor to mark x 'x move cursor to first non-whitespace character containing mark x Miscellaneous movement: fa forward to character a Fd backward to character d tg forward to character before g Tw backward to character after w w beginning of next word W beginning of next WORD (punctuation is part of word) b back one word B back one WORD (punctuation is part of word) e end of word E end of WORD (punctuation is part of word) zENTERposition line with cursor at top of screen z. position line with cursor at middle of screen z- position line with cursor at bottom of screen ), ( next, previous sentence ]], [[ next, previous section }, { next, previous paragraph ( the above will depend on your settings for what constitutes a "section" and "paragraph" ) EDITING TEXT: Inserting text: a append after cursor A append at end of line (same as $a) i insert before cursor I insert at beginning of line (same as _i) o open line below cursor O open line above cursor ^[ terminate insert mode (escape) Deleting and changing text: cw change word C change line starting with current character (same as c$) cc change current line 2cc change 2 lines dH delete to top of screen dd delete current line dj delete current line and one below it d/pat delete to pat(tern) d`z delete to character marked with z d'z delete to line marked with z de delete to end of word d% delete to matching bracket P put text previously yanked or deleted before/above cursor p put text previously yanked or deleted after/below cursor 4s substitute 4 characters S substitute entire line u undo last change . repeat last change ~ reverse case 11~ reverse case of next 11 characters (on current line) Copying and moving: yy copy current line to (unnamed) buffer "xyy copy current line to buffer x "xp put contents of buffer x below cursor ye yank to end of word "xyG yank from current line to end of file into buffer x y2k yank current line and two above it y'x yank to line with mark x y`x yank to character with mark x 3>> indent next three lines one shiftwidth >>34G indent current line to line 34 one shiftwidth Save and exit: :x quit vi, write file if changes were made :w file save copy to file :w >> file append copy to file :q! quit vi, do not save changes :e! return to file before changes were made, after last write :e# edit alternate file Interacting with OS: :!command run command :r file read in contents of file after cursor :r !command read in output of command after cursor :n,m! command run command on lines in file from n to m and replace with output !object command send buffer object to command, replace with output (eg. !G sort -fd will sort from the current line to end of file) ncommand send n lines to command, replace with output
VI COMMAND KEYS IN ALPHABETICAL ORDER
( ^A stands for control-a ^i stands for
-i )
a append after cursor (can be preceded by a number)
A append at end of line (same as $a) (can be preceded by a number)
^A unused
b back up one word (can be preceded by a number)
B back up one WORD (including punctuation) (can be preceded by a number)
^B scroll up one screen (can be preceded by a number)
c begin a change (combine with movement command)
(can be preceded by a number)
C change line from cursor to end of line (same as c$)
(can be preceded by a number)
^C unused; in insert > end insert mode
d begin a delete (combine with movement command)
(can be preceded by a number)
D delete from cursor to end of line (same as d$)
(can be preceded by a number)
^D scroll down a half screen (can be preceded by a number);
in insert > back up on shiftwidth
e goto end of word (can be preceded by a number)
E goto end of WORD (including punctuation) (can be preceded by a number)
^E show one more line at bottom of screen (can be preceded by a number)
f forward to next typed character (can be preceded by a number)
F backward to next typed character (can be preceded by a number)
^F scroll down one screen (can be preceded by a number)
g unused
G goto specified line or end of file (can be preceded by a number)
^G print file info on status line (bottom of screen)
h cursor left (can be preceded by a number)
H goto top of screen (can be preceded by a number)
^H left cursor; in insert > backspace
i insert before cursor (can be preceded by a number)
I insert at beginning of line (same as _i)
(can be preceded by a number)
^I unused; in insert > tab key
j cursor down (can be preceded by a number)
J join two lines (can be preceded by a number)
^J down arrow cursor key; in insert > same as enter
k cursor up (can be preceded by a number)
K unused
^K unused
l cursor right (can be preceded by a number)
L goto bottom of screen (can be preceded by a number)
^L redraw screen usually
m mark current cursor position with character typed (a-z)
M goto middle of screen
^M to beginning of next line (can be preceded by a number);
in insert > same as enter key
n repeat last search command
N repeat last search command in reverse direction
^N down arrow cursor key
o open a new line below the cursor (can be preceded by a number)
O open a new line above the cursor (can be preceded by a number)
^O unused
p put yanked or deleted text after or below cursor
P put yanked or deleted text before or above cursor
^P up arrow cursor key
q unused
Q quit vi, invoke ex (not very useful) return with vi
^Q unused (some terminals, stop data flow)
r change character under cursor to character typed
(can be preceded by a number)
R replace characters (overwrite mode) (can be preceded by a number)
^R redraw screen usually
s substitute characters under cursor to ones typed
(can be preceded by a number)
S change entire line (can be preceded by a number)
^S unused (on some terminals, resume data flow)
t forward to before next character typed (can be preceded by a number)
T backward to after next character typed (can be preceded by a number)
^T goto previous tag; in insert > move right one shiftwidth
u undo last change
U restore current line
^U scroll screen up a half screen (can be preceded by a number);
in insert > delete back to start of insert (depends on terminal
settings)
v unused
V unused
^V unused; in insert > quote next character
w forward one word (can be preceded by a number)
W forward one WORD (including punctuation) (can be preceded by a number)
^W unused; in insert > back up to beginning of word (depends on
terminal settings)
x delete character under cursor (can be preceded by a number)
X delete back one character (can be preceded by a number)
^X unused
y begin a yank (combine with movement command)
(can be preceded by a number)
Y yank current line (same as yy) (map it to y$ to be consistent with
D and C) (can be preceded by a number)
^Y show one more line at top of screen (can be preceded by a number)
zENTER reposition line with cursor to top of screen
z. reposition line with cursor to middle of screen
z- reposition line with cursor to bottom of screen
(the three z commands above can be preceded by a number)
ZZ quit vi, write file if changes were made
^Z suspend vi on systems that have job control (depends on terminal
settings)
^@ unused
^[ terminate insert mode (escape key)
^\ I don't think it does anything
^] goto tag under cursor
^^ edit alternate file
^_ unused?
^? unused? delete key?
space cursor right (can be preceded by a number)
! filter program through external filter, requires an object to work on
(combine with movement command) (press enter to execute)
" use register for next delete, yank or put, registers are (a-zA-Z0-9)
# unused?
$ goto end of line (can be preceded by a number)
% goto matching bracket () [] {}
& repeat last substitute
' to first non-blank character on line with mark (a-z)
( beginning of next sentence
) beginning of current sentence (sentence is delimited by ., !, ? and
followed by at least one space)
* unused
+ down one line to first non-blank character (j_)
(can be preceded by a number)
, repeat f, F, t, T commands in reverse direction
- up one line to first non-blank character (k_)
(can be preceded by a number)
. repeat previous command
/ search forward for text entered (repeat previous search if no
argument supplied) (press enter to execute)
0 goto beginning of line
: enter an ex command (press enter to execute)
; repeat f, F, t, T commands
< begin a shift left (combine with movement command)
(can be preceded by a number)
= unused (unless in lisp mode in which case it formats to standard
lisp formatting)
> begin a shift right (combine with movement command)
(can be preceded by a number)
? search backward for text entered (repeat previous search backward
if no argument supplied) (press enter to execute)
@ execute a register (0-9a-z".)
[[ beginning of current section (as delimited by the sect= option)
\ unused
]] beginning of next section (as delimited by the sect= option)
^ goto first non-blank character on line
_ goto first non-blank character on line (can be preceded by a number)
` goto mark (a-z)
{ beginning of current paragraph (as delimited by a blank line or the
p= option)
n| column n of current line
} beginning of next paragraph (as delimited by a blank line or the
p= option)
~ change case of character under cursor (can be preceded by a number)
EX EDITOR address symbols 0 beginning of file $ end of file 1,$ all lines in file % stands for filename, hence it means the entire file (1,$) # stands for alternate file x,y lines x through y x;y lines x through y, current line reset to x . current line n absolute line number n x-n n lines before x x+n n lines after x +[n] n lines ahead (default is one) -[n] n lines back (default is one) 'x line marked with x '' previous mark /pattern/ forward to line matching pattern ?pattern? backward to line matching pattern
QUICK LIST OF EX COMMANDS AND THEIR ABBREVIATIONS | abbreviate ab | move m | tag ta | | append a | next n | unabbreviate una | | args ar | number # nu | undo u | | change c | open o | unmap unm | | chdir cd chd | preserve pre | version ve | | copy t co | print p | visual vi | | delete d | put pu | write w | | edit e | quit q | xit x | | file f | read r | yank ya | | global g v | recover rec | (window) z | | insert i | rewind rew | (escape to OS) ! | | join j | set se | (lshift) < | | list l | shell sh | (rshift) > | | map | source so | (line number) = | | mark k ma | substitute s & ~ | (execute buffer) * @ |
EX COMMANDS
( anything in [] is optional , anything in {} is the rest of the name of
the command which is also optional )
the default for address is the current line, except for g, g!, v, w; for
these the default is the entire file
ab{breviate} [string text]
define string when typed to be translated into text.
if string and text not specified - list all abbreviations
abbreviations are usually put into your .exrc file
eg. :ab Xvi vi is the best editor
:ab character character
:ab the the
:ab have have
[address]a{ppend}[!]
text
.
append text at address. ! switches autoindent.
ar{gs}
print filename arguments.
cd [path]
without path change to home directory, with path change to path
[address]c{hange}[!]
text
.
replace lines with text. ! switches autoindent.
[address]co{py}destination
copy lines from address to destination.
eg. :1,10co50 copy lines 1 to to 10 to below line 50
[address]d{elete} [buffer][count]
delete lines in address if buffer is specified save lines to buffer.
count specifies the number of lines to delete starting with address.
eg. :/include/,/main/d delete lines between include and main
including include and main
:/include/+,/main/-d as above but not including include nor main
:3d delete line 3
:d3 delete 3 lines starting with current
:.,$d a delete to end of file from current line into buffer a
:d a3 delete next three lines starting with current into buffer a
e{dit}[!] [+n] [file]
begin editing file. ! will discard changes to current file.
n specifies line to begin editing file (default 1).
eg. :e file edit file
:e # edit previous file
f{ile} [filename]
change name of file to filename. without argument print current
filename.
eg. :f %.new appends .new to current filename, renaming file to
file.new
[address]g{lobal}[!]/pattern/[commands]
execute commands on lines that contain pattern, if address is specified
within the address range. ! execute on lines not containing pattern.
without commands print matching lines.
eg. :g/\#include/d delete all lines that have include directives
:g!/\#define/p print lines that are not define statements
:g/^\/\*/p print lines that start with /*
:g/^[ ^I]*$/d delete empty lines as well as lines with only tabs
or spaces
:g/strcmp/d5 delete lines that have strcmp in them as well
as the following 4 lines
[address]i{nsert}[!]
text
.
insert text at line before address. ! switches autoindent.
[address]j{oin}[!][count]
join lines in specified range. ! preserves white space.
eg. :1,5j! join lines 1 to 5, preserve white space
[address]k char
synonymn for mark, character can follow k with no intervening space.
[address]l{ist}[count]
print lines so that tabs show as ^I and end of lines are marked with $.
map[!] [char commands]
define a keyboard macro for named char that is a synonymn for commands.
! will create a mapping for input mode. with no arguments print mapped
keys.
eg. :map ^N :n^M (to get a control character type ^V followed by the
control character)
[address]ma{rk} char
mark lines with char. char is a single lowercase letter.
eg. :ma z mark line with z
:'z return to line with mark z
[address]m{ove} destination
move lines specified by address to destination.
eg. :.,/include/m /string/ move lines from current to include line below
line with string
n{ext}[!] [[+commands] filelist]
edit next file from argument list. if filelist is provided, replace
argument list with filelist. ! will discard changes to current file.
[address]nu{umber}[count]
print lines specified by address, precede each line by its line number.
count specifies the number of lines to print.
[address]o{pen}[/pattern/]
enter vi's open mode with lines specified by address, or at lines
containing pattern.
pre{serve}
save current buffer as if the system had crashed
[address]p{rint}[count]
print lines specified by address. count is the number of lines to print.
eg. :304;+5p print five lines starting with 100, reset current line to
100
[address]pu{t} [buffer]
restore lines that were previously deleted or yanked and put them after
current line. put line from buffer if specified.
q{uit}[!]
quit. ! discard changes to file.
[address]r{ead} file
copy text from file on line below specified by address.
eg. :0r data read in file "data" at top of file
[address]r{ead} !command
read the output of command into file after line specified by address.
eg. :$r !ls -aFC run "ls" and read in its output at end of file
rec{over} [file]
recover file from system save area.
rew{ind}[!]
rewind argument list to first argument. ! discards changes to current
file.
se{t} parm1 parm2
set value to an option with each parm. if no parm is supplied print all
changed options. for boolean options parm is phrased as option or
nooption, other options are option=value. all will print all available
options.
set commands are usually put into your .exrc
eg. :se autowrite tabstop=4 autoindent shiftwidth=4 wrapmargin=5
:se all print all available options
sh{ell}
create a new shell. resume editing when shell exits.
so{urce} file
read and execute ex commands from file
eg. so ~/old.exrc
[address]s{ubstitute}[/pattern/replacement/][options][count]
replace pattern with replacement. if pattern and replacement are omitted
repeat last substitution. count specifies the number of lines on which to
substitute starting with address.
options
c prompt for confirmation
g substitute all instances on line
p print last line on which substitution was made
eg. :%s/[hH]ello/Hi/g replace hello or Hello with hi, all ocurrences
:.,$s/[uU][nN][iI][xX]/\U&/10 upcase unix on next 10 lines
:%s/\<./\u&/g turn the first letter of all words to uppercase
:1,/main/s/int/long/g substitute occurances of int before main with
long
[address]t destination
synonymn for copy
ta{g} tag
switch to file containing tag.
eg. :!ctags *.c run ctags on all .c files in directory
:ta func switch to file containing func, put cursor on it
una{bbreviate} word
remove word from list of abbreviations.
u{ndo}
undo changes made by last editing command.
unm{ap}[!] char
remove char from keyboard macros. ! remove macros for input mode.
[address]v/pattern/[commands]
synonymn for global!
eg. :v/./,/./-j join empty lines to have only single empty line
between lines of text
ve{rsion}
version of editor.
[address]vi [type][count]
enter visual mode at line specified by address. exit with Q. count
specifies initial window size.
- place line at bottom of window
. place line in center of window
^ print previous window
vi [+n] file
begin editing file in visual mode at line n.
[address]w{rite}[!] [[>>] file]
write lines specified by address to file. >> is used to append to file.
with no address write all of the file. ! forces the write.
eg. :1,25w new_file write lines 1 to 25 to "new_file"
:50,$w >> new_file append line 50 to end of file to new_file
[address]w !command
write lines specified by address to command.
wq[!]
write lines specified by address to file and quit. ! forces the write
x{it}
exit file. save changes.
[address]ya{nk} [buffer][count]
place lines specified by address in buffer char. count is the number of
lines to yank starting with address.
eg. :20,100ya z yank lines 20 to 100 into buffer z
[address]z[type][count]
print a window of text. count is the number of lines to display starting
with address.
type
+ place line at top of window(default)
- place line at bottom of window
. place line in center of window
^ print previous window
= place line in center of window. leave line as current line.
[address]![command]
execute command. if address is specified apply lines from address as input
to command, and replace lines with output.
eg. :!ls -aFC run ls, will not read output into file
:0!ls -aFC run ls, read in its output to beginning of file
:11,35!sort -fd sort lines from 11 to 35
:%!spell -b run the spellchecker on entire file
[address]=
print line number of matching address. with no address print line number
of last line.
[address]<[count]
[address]>[count]
shitft lines left(<) or right(>). count specifies the number of lines to
shift starting with address.
eg. :1,9> indent lines 1 through 9 one shiftwidth
address
print line specified by address.
return
print next line.
[address]&[options][count]
repeat previous substitution. count specifies the number of lines to
substitute on starting with address.
eg. :s/msdos/UNIX substitute msdos with UNIX
:g/OS/& redo substitutions on all lines with "OS"
[address]~[count]
replace previous regular expression with the previous replacement from
substitute.
*[buffer]
@[buffer]
execute named buffer
[address]#[count]
synonym for number
The following can be used as addresses in ex, as well as in commands :g,
:s, :v, /, ?
REGULAR EXPRESSION SEARCH AND SUBSTITUTE
. match any single character except newline
* match any number (including none) of the single character immediately
preceding
^ match beginning of line if at start of expression
$ match end of line if at end of expression
[ ] match anything enclosed in []
[^ ] match anything not enclosed in []
\( \) store pattern for later replay
\< match following characters at beginning of word
\> match preceding characters at end of word
\ escape character following (needed for eg. \. to match a .)
\n reuse previous pattern, n is a number between 1 and 9 eg. \1
& reuse previous search pattern
~ reuse previous replacement pattern
\u change character to upper case
\U change characters to upper case
\l change character to lower case
\L change characters to lower case
\e turn off previous \u or \l
\E turn off previous \U or \L
Examples
dig matches dig
^dig matches dig at beginning of line
dig$ matches dig at end of line
^dig$ matches dig as the only word on line
^$ matches the empty line
^..*$ matches a line with at least one character
.* matches any string of characters including none
^[ ^I]*$ as above but line can also contain spaces and/or tabs(^I)
[dD]ig matches dig or Dig
[aA][nN] matches an, aN, An, AN
d[aeiou]g second letter is a vowel
d[^aeiou]g second letter is not a vowel
d.g second letter is anything
^....$ matches a line with exactly four characters
^\. matches any line beginning with a dot
^\.[a-z] same with a lowercase letter following
^[^\.] matches any line that does not begin with a dot
;$ matches a line ending with a semicolon
digs* matches dig, digs, digss, digsss etc.
[a-z][a-z]* matches one or more lowercase letters
[a-zA-Z] matches any character
[^0-9a-zA-Z] matches any symbol (not a letter or number)
\<the matches the, theater, then
the\> matches the, breathe
\< the\> matches the
:%s/\<./\u&/g turn the first letter of all words to uppercase
:%s/\<[a-z][!-~]*\>/\u&/g as above
:%s/\<[a-z]/\u&/g as above
:%s/.*/\L&/ turn entire file to lowercase
:%s/<[^>]*>//g remove strings from file that start with a less than sign
and end with a greater than sign (html tags)
., ^, &, $ must be preceded by a \ to make literal when magic is on
* must be preceded by a \ under certain circumstances although it never
hurts to precede it with a backslash whenever you mean a * not its regexp
meaning % and # should also be escaped as they mean current and alternate
file to ex
Whenever you execute a command at the UNIX prompt, FreeBSD assigns it a Process IDentification number or PID. This is a number between 1 and 65535. This number will be used to identify and control the process the entire time that it is running. When the program terminates, the Number will be recycled into the stack and reassigned when the kernel has cycled completely though all 65535 numbers.
If the same program is run again, it will be given a different PID and FreeBSD will track it through this new number. If two copies of the same program are started at the same time, two individual PID numbers will be assigned; one to each.
ps will give you a listing of all the process you have running.
>ps PID TT STAT TIME COMMAND 12346 p0 Is+ 0:00.57 -tcsh (tcsh) 9871 p0 R+ 0:00.57 ps
ps -a will give you a listing of all of the process running on the system.
To stop a process, you can use the kill command. Select the PID of the process you want to stop. Then type
kill -KILL PID.
For Example to kill process #12346 you would type:
>kill -KILL 12346
Process # -1 refers to every running process that you own. If you have several process running, you can kill all of them, (except your current shell) by typing:
kill -HUP -1
As root, you can use this command to kill, or restart, every process in the system.
If you don't want to use the PID, you can use the program killall. killall requires that you specify the name of the program running as a command line argument. Then it kills every instance of that program that it finds, providing you have permission to kill that program.
For instance:
killall make
would kill all make processes that you have running.
In addition to a priority, each process is assigned a ``Niceness'' value. The default value is 0. The users can give a nice value of up to +20, making processes run slower; freeing up processor time for more important processes. The ``nicer'' a program is, the more it allows other programs to run. Root can give values down to -20, making important processes run faster. All child processes, processes started by a program, inherit the nice value from the parent program. If you give a nice value of -20 to a make world all processes spawned by that program will have the nice value of -20. ie. all compiler processes generated will run at a nice of -20.
To give a program a nice value, precede your command with nice. If you wanted to start ppp with a nice value of +10 you would use:
> nice +10 ppp
If you want to change the nice value of a process that is already running, you would use renice followed by the process ID. To change the niceness value of process #1234 from the default of Zero to -10, you would use:
> renice -10 1234
'Nicing' or 'renicing' to +10 is the most common use of the (re)nice command. It's really just a way of telling FreeBSD that you aren't too concerned about the process finishing immediately, and it's okay to let other processes use more CPU time than your process. Usually, if you are going to run a program that you know will take a long time to run (say 5 minutes or greater) it is considered 'polite' to nice +10 the program. You're program will still finish very quickly. In fact, if the system isn't very busy there will be almost no difference, since no other processes are battling for the CPU!. Unless the system is very overloaded, a nice value of +10 will NOT result in a slow down. When you raise the niceness value of a running program, your program isn't put on hold until others are done, it just receives a smaller percentage of the CPU time. A nice value of +20 indicates that the application in question should ONLY run when the CPU is completely free. Generally, the only time a process is run with a nice value of +20 is when you don't want it to slow down any other aspect of the system.
top is pa process monitoring tool that helps you keep track of system resources. It displays the Active Processes, CPU Utilization, Swap Space Usage, and Memory Available. It also allows you to renice and kill processes. If you watch the CPU utilization, you will notice that it sits at about 90% IDLE most of the time. (Active Servers won't have so many Free Cycles.) Many system administrators find ways to utilize these idle CPU Cycles with creative applications of nice, renice, and top. Processor intensive, time consuming processes can be set to run in the background and only use up IDLE CPU Cycles, allowing your program to run and not infringe on regular operations.

FreeBSD looks at file systems in ``slices'' or partitions. Each file system is assigned to a device. The Floppy Drive is assigned the device ``fd''. The devices are also numbered, starting with 0. The first floppy disk is fd0. Filesystem devices are further divided up into slices. Each slice is assigned a letter. The letter is appended to the device name and number. Traditionally, slices a through h are available. Some slices have special meanings:
a ) This refers to the boot sector of the disk only. b ) This refers to the swap space on the disk if there is any. c ) This references the Whole Disk. Usually what you want. e-h ) This and other such letters would refer to non-bootsector slices if the disk happened to be divided up further.
Access to devices is granted through file representations of those devices contained in the /dev/ directory. Each slice has its own file representation. Therefore to access the whole floppy disk you would reference the file: /dev/fd0c. To actually get at the contents of the disk, you have to mount it into the file system under a directory. If you have a FreeBSD formatted Floppy disk, you can just use the mount command without any special parameters. You will have to specify a directory that exists on the system that you have write permission to, and have permission to access the device file you are trying to mount. By default, general users don't have access to the floppy disk device files.
To mount the floppy to a directory called /mnt you would type:
mount /dev/fd0c /mnt
Then cd to /mnt and work with the files that are there.
If you have a MSDOS disk that you want access to, you will use the command:
mount_msdos /dev/fd0c /mnt
To check and see if it mounted you can cd to the directory
and do an ls, or type df and df will give you a list of all the file systems that are currently mounted
and where they are mounted to. It will also show all the amount of space
used.
Mounting a MSDOS floppy would give you access to the floppy just like a regular directory on the system, however it will automatically truncate the file names of any files copied to that directory to the dos 8.3 notation. (ie. FreeBSD.File.long.name will become FreeBSD.Fil)
When you are done with the floppy disk, you will have to unmount it from the file system. To do this type:
umount /dev/fd0c
OR
umount /mnt
Either one will successfully unmount the file system. You cannot unmount a file system while you are in the directory that the filesystem is mounted to. This will give you a device busy error. You will have to cd out of that directory before unmounting it.
Now when you cd to /mnt it should be empty.