Exercises: Unix Bootcamp: AfNOG 2009 Workshop, Cairo

May 10, 2009

 
Exercises

  1. Use pw to create a new userids that you will use instead of root
  2. Practice with basic filesystem and user commands

  3. Getting help using manpages, docs, and the FreeBSD Handbook
  4. General job control (ctrl-c)
  5. Processes and stopping them
  6. Viewing file contents
  7. File and directory permissions

  8. Disk partitions: how to view your disk
  9. Create a file and use vi to edit the file

  10. Process startup and the RC system
  11. Shutdown and reboot your system

  12. Basic network configuration

  13. Initial login, virtual terminals, command line manipulation
Note: The "#" and "$" characters before commands represents your system prompt and is not part of the command itself. "#" indicates a command issued as root while "$" indicates a command issued as a normal user.

Note 2: If you install software, update your environment as root and the change is not immediately available try typing rehash at the root shell prompt. This is only necessary when running a C shell (e.g., like /bin/csh).

2.) Use pw to create a new userid that you will use instead of root [Top]

First login to your computer if you have not already done so. Login as userid "root" using the password given in class.

Now that you are root you can create a new user account on your machine. If more than one person is using your machine, then be sure that you create an account for each and every person.

To create or remove a user account you should use the "pw" command. To get a feel for the power and complexity of this command take a look at its man pages (we'll practice man some more later):

# man pw
So, first pick a username that you want to use. For example, use your first name, last name, a combination of both, or whatever you prefer. For purposes of this example we use username. So, to add a new user to your system type:
# pw useradd username -m -s /usr/local/bin/bash
This creates the user username. The "-m" says add a new home directory of /usr/home/username and copies files from /usr/share/skel to the new user's home directory. The default shell "bash" will be used. Note that at this point the bash shell has not been installed on your machine. We'll be doing this in the upcoming exercises.

Next you need to set the password for the new userid, otherwise you won't be able to use the account properly. To do this one uses the passwd command. Change your new user's password using the command:

# passwd username

You will be prompted to enter in a new password, then to enter it again to verify. You need to pick a secure password. Note, as root you are allowed to pick any password you want, but you need to pick a secure password for your new account. Here are some quick guidelines to picking a secure password:

For example, something like "1ps4vIce5!" is OK ("IP Services!") isn't bad. Please don't pick this for this exercise.

To use your new account you can logout from your current session and log in with the new username and password.

 

2.) Practice with basic filesystem commands [Top]

Be careful in this exercise. Running as root (the Unix administrative account) means that you can easily damage your system, so we ask that you log out of your root account and log in as your own user account instead.

To logout, type

# exit

If you are unsure of how to proceed ask your instructor or assistants for help before continuing.

The first command that we are going to use is man, this is short for "man"ual. Read about each command to see the range of options that exist.

Many of the basic commands we'll be practicing are built in as part of your shell environment (that is you won't find a binary/program file for cd). To read about commands like cp, cd, ls, mv, rm in more detail you can just type:

$ man builtin
And, for a command like ls you can type:
$ man ls
And, even for a built-in command you can just type "man commandName", or something like:
$ man cd
and this will open the "builtin" man page for you.

If you have problems exiting from "man" press the "q" key. Also, you can use the keyboard arrows to move around in the descriptions.

As we move around directories an extremely useful command is pwd, which return the working directory name you are in. So, if you get lost just type:

$ pwd

We'll do this from time to time as we use directory commands.

Simplified Map of Unix Directory Tree


/                  ("root directory")
/etc               (contains configuration files)
/etc/rc.d          (contains system startup scripts)
/root              (user root's home directory)
/tmp               (place to store TeMPorary files)
/usr               (contains the majority of user utilities, applications, home directories)
/usr/home          (home directories for users on the system*)
/usr/local/etc     (contains third-party configuration files and startup scripts*)
/var               (multi-purpose log, temporary, transient, and spool files location)

*Different from Linux 

For details on the (almost) complete directory tree under Unix/Lunx type "man hier"

Command Glossary


cd                 Change Directory
ls                 LiSt files
mkdir              MaKe DIRectory
mv                 MoVe files
pwd                Print Working Directory
rm                 ReMove files
rmdir              ReMove DIRectory
touch              Update date on file/Create new empty file if none exists

Note: There are some special files on UNIX, which are '.' and '..':

Now we are ready to practice a bit with the commands:

$ cd /
$ pwd
$ ls
$ ls -la
$ cd /tmp
$ cd ..
$ pwd
$ cd tmp
What's going on here? If you don't understand, ask.

$ cd       (take you back to your home directory)
$ pwd
$ touch text.txt
$ cp text.txt new.txt
$ mv text.txt new.txt
What's happening now? If prompted to overwrite, respond "y". Note that "username" is the name of the user account you created in the first exercise.

Now watch what happens if you try to copy a file on itself.

$ cp new.txt /home/username/.
$ cd ../../home/username
$ cd       (to return to our home directory)
$ cp new.txt new.txt.bak
The tab key makes life much easier. Now type:
$ cd
$ mkdir tmp
$ mv new.* tmp/.
$ ls
Finally, we are going to remove the directory that contains the two archives.
$ cd tmp
$ rm *
$ cd ..
$ rmdir tmp
You can force this using a command like this:
$ rm -rf tmp
The use of "rm -rf" is very dangerous!, and, naturally, very useful. For example, if you are "root" and you type "rm -rf /*" this would be the end of your server. This commands says "remove, forcibly and recursively, everything" - Or, if you start in the root directory (/), remove all files and directories without asking on the entire server. If you want to use "rm -rf *" always take a deep breath and check where you are first (really, do this!):
$ pwd
First this says in what directory you are. If you are mistaken, then you have the opportunity to not remove files that you might really need.

 

3.) Getting help using manpages, docs, and the FreeBSD Handbook [Top]

Now that you have FreeBSD up and running you probably want to have a way to figure out how to use it when there are no instructors around, or other FreeBSD-knowledgeable people. First and foremost, make it a habit to read the man pages (MANual pages) for the commands that you use. You might be surprised at some of the things these commands can do! In any case, as you have seen, when in doubt about what a command does, or how it works simply type:

$ man command
Optionally, you might find some additional information for some commands typing:
$ info command
And, to get information about both these commands type:
$ man man

$ info info

After this there is a large amount of documenation available to you in serveral ways. For instance, if you look in:

/usr/share/doc

you will find multiple FreeBSD articles in various languages available to you. In addition, the FreeBSD Handbook is available here under /usr/share/doc/handblook. If you wanted to start reading the FreeBSD Handbook from your local hard drive you could use a web browser on the file /usr/share/doc/handbook/index.html. In URL format in your web browser this looks like:
Note: we are likely not using a graphical interface at this time.

file:///usr/share/doc/handbook/index.html
There is, also, a FreeBSD FAQ file you can look at here:
file:///usr/share/doc/faq/index.html
After this, have a look at some of the available articles by doing the following in a terminal window.
$ cd /usr/share/doc/en/articles

$ ls

Finally, there are several papers available as well. Try:
$ ls /usr/share/doc/papers
If you have a network connection, then you can go to http://www.freebsd.org/docs.html for even more information. Become accustomed to the idea of using man to get specific information about commands, and then using these additional resources to get an overview of entire sub-systems of the FreeBSD operating system. If you want to read the FreeBSD Handbook online it is available here http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/index.html. If you want to understand FreeBSD concepts in more depth, then the FreeBSD Handbook is really where you should begin.


4.) General job control (ctrl-c) [Top]

For this exercise you need to be logged in as root.

When you wish to stop an active process in your shell you can use the keyboard combination "ctrl-c". If this does not work, then you may want to try "ctrl-z". The ctrl-z sequence will suspend the process allowing you to place it in the background using the bg (BackGround) command. This is a bit more complex, but let's practice using ctrl-c right now:

# cd /
# ls -R (starts to recursively show contents of all directories on your system)
press CTRL-C (aborts the output)
In addition, when you type "man command" you can press the "q" key to exit from viewing the man pages. The same goes for "less" as well.

Now let's start a process in your shell to requires you to use ctrl-c to end the process:

# tail -f /var/log/messages
In this example you are viewing the end of the main log message for your system with data appended to the output as the file grows. That is, if a message is generated and placed at the end of the log file you will see this message interactively appear on the screen. This is a very useful tool when you are trying to debug problems on your system. You can open one terminal window (as root, or with an account that can run privileged commands), type "tail -f /var/log/messages", start a process in another terminal window and then view the end results in the window where the tail command is running.

Now to end the "tail -f" process press ctrl-c to abort the output. In this case "q" or "ESC" will not work. You need to know about ctrl-c to do this.

 

 

6.) Viewing file contents [Top]

If you want to see the contents of a file there are three typical ways to do this:

$ cat
$ less
$ more
Each of these commands has it's own features: the typical saying is to remember that "less is more" when it comes to Unix.

Test this using the three commands using them with an informational file like:

$ cd /etc
$ cat motd
$ more services (you can exit with "q")
$ less services (you can exit with "q")
Try looking at some more files, for instance, fstab, rc.conf, termcap, etc. If you don't understand what you are looking at, then use the "man" command. For example, type:
$ man fstab
$ man rc.conf
$ man termcap
Finally, there is one other useful command for looking inside files. This command is tail.

We will need to run this this as the root user. So, do this:

$ logout

login: root
password: <given in class>

Now type:
# tail /var/log/messages
This will show you the last few lines of your main logfile on your system. This can be really useful if you just want to see what the last thing written to a file was. Your file /var/log/messages will get very large over time, so if you used cat, less, or more to view this file for new messages this could become very time-consuming. Now, even more fun is to do the following:
# tail -f /var/log/messages
Now press CTRL-ALT-F3 to go to one of your virtual terminals. Login as root on this terminal. Now go back to your original terminal where you typed the command tail -f /var/log/messages by pressing ALT-F1. You should see a message on the screen saying that root just logged in. The "-f" option means, "output appended data as the file grows" - or, you can watch each new item as it's written to the end of a file. This is incredibly useful when you are trying to debug problems and you need to see what happens in your logfiles in real time.

Don't forget to log out of your other terminal window.

If you have any questions ask the instructor or one of the class helpers.

 

7.) File and directory permissions* [Top]

*Reference: Shah, Steve, "Linux Administration: A Beginner's Guide", 2nd. ed., Osborne press, New York, NY.

If you look at files in a directory using "ls -al" you will see the permissions for each file and directories. Here is an example:

drwxrwxr-x    3 hervey   hervey       4096 Feb 25 09:49 directory
-rwxr--r--   12 hervey   hervey       4096 Feb 16 05:02 file

The left column is important. You can view it like this:

Type User    Group World Links  owner  group  size   date   hour  name
d    rwx     rwx   r-x   3      hervey hervey 4096   Feb 25 09:49 directory
-    rwx     r     r     12     hervey hervey 4096   Feb 16 05:02 file

So, the directory has r (read), w (write), x (execute) access for the user and group. For world it has r (read) and x (execute) access. The file has read/write/execute access for the world and read only access for everyone else (group and world).

To change permissions you use the "chmod" command. chmod uses a base eight (octal) system to configure permsissions. Or, you can use an alternate form to specify permissions by column (user/group/world) at a time.

Permissions have values like this:

Letter  Permission   Value

R       read         4
W       write        2
X       execute      1

Thus you can give permissions to a file using the sum of the values for each permssion you wish to give for each column. Here is an example:

Letter  Permission                   Value

---     none                         0
r--     read only                    4
rw-     read and write               6
rwx     read, write, and execute     7
r-x     read and execute             5
--x     Execute                      1

This is just one column. Thus, to give all the combinations you have a table like this:

Permissions  Numeric      Description
             equivalent  

-rw-------   600          Owner has read & execute permission.
-rw-r--r--   644          Owner has read & execute.
                          Group and world has read permission.
-rw-rw-rw-   666          Everyone (owner, group, world) has read & write
                          permission (dangerous?)
-rwx------   700          Onwer has read, write, & execute permission.
-rwxr-xr-x   755          Owner has read, write, & execute permission.
                          Rest of the world has read & execute permission
                          (typical for web pages or 644).
-rwxrwxrwx   777          Everyone has full access (read, write, execute).
-rwx--x--x   711          Owner has read, write, execute permission.
                          Group and world have execute permission.
drwx------   700          Owner only has access to this directory.
                          Directories require execute permission to access.
drwxr-xr-x   755          Owner has full access to directory. Everyone else
                          can see the directory.
drwx--x--x   711          Everyone can list files in the directory, but group
                          and world need to know a filename to do this. 

Now lets practice changing permissions to see how this really works. As a normal user (i.e. don't login as root) do the following:

$ cd (what does the "cd" command do when you do this?)
$ echo "test file" > read.txt
$ chmod 444 read.txt
In spite of the fact that the file does not have write permission for the owner, the owner can still change the file's permissions so that they can make it possible to write to it:
$ chmod 744 read.txt
Or, you can do this by using this form of chmod:
$ chmod u+w read.txt
The forms of chmod, to add permissions, if you don't use octal numbers are:

$ chmod u+r, chmod u+w, chmod u+x
$ chmod g+r, chmod g+w, chmod g+x
$ chmod a+r, chmod a+w, chmod a+x

Note that "a+r" is for world access. The "a" is for "all", "u" is for "user", and "g" is for "group".

Now, change the file so that the owner cannot read it, but they can write to the file...

$ chmod u-r read.txt
Or, you can do something like:
$ chmod 344 read.txt
You probably noticed that you can use the "-" (minus) sign to remove permissions from a file.

A UNIX Permissions "Gotcha"

If a directory has the World or Group write flag set, and contains a file that is only writeable by the owner, then a member of either the Group or World (everyone) can still make changes to the file. Here's an example of how (become root to do this):

# mkdir /tmp/test
# echo "example text" > /tmp/test/example.txt
# chmod 644 /tmp/test/example.txt
# chmod a+w /tmp/test
# su - userid
$ cp /tmp/test/example.txt .
$ echo "add more text to file" >> example.txt
$ mv example.txt /tmp/test/example.txt
You will receive the following prompt:
override rw-r--r--  root/wheel for /tmp/test/example.txt? (y/n [n])
If you press "y" and ENTER, then your version of the file will now overwrite the read only version of the file owned by root in the /tmp/test directory. This is because write permission has been enabled for World on the /tmp/test directory. Thus, you have permission to mv (i.e. rename) a file of the same name to this directory. This result may seem surprising. If you do:
$ ls -al /tmp/test
You will see that your userid now owns the file. The root user no longer owns the file. So, using this trick is pretty obvious, unless, of course, you set things back to the way they were using the chown command.

 

8.) Disk partitions: how to view your disk [Top]

First, make sure you are logged in as your user and not as root.

Now in a terminal lets look at the partitions. Type:

$ df
$ df -h
What difference did you see between "df" and "df -h". How can you see what your swap contains (note it was not listed using "df")? Use this:
$ swapinfo

If you want to see more detailed information about your disk slices you can use the "fdisk" command. As a general user you are not allowed to run this program, so you must use sudo. Try this by typing:

$ fdisk
Be careful with fdisk as you can remove slices, partitions, etc.

If you are interested in how much space files are taking up in a directory or a group of directories you can use the "du" command. Try it out by typing:

$ du

$ du -h

As usual you can get more information by typing "man du".

Now that you've seen these methods for viewing disk information, you should be able to understand the output of the disklabel command a little better. The disklabel command is really the proper way to view the status of partitions in your FreeBSD slice at it will show you all your FreeBSD slice information no matter if a partition is mounted or not. In some cases this is critical if you are trying to troubleshoot problems and not all partitions have mounted. In order to use disklabel you need to be root.

So, if FreeBSD resides on the slice /dev/ad0s1 on your disk, then to see disk information you type: # disklabel /dev/ad0s1 (Note, we are assuming we are using IDE drives) If we are This shows you output along these lines (will be different for this workshop):

# /dev/ad0s1:
8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:   409600        0    4.2BSD     2048 16384 25608
  b:  1433600   409600      swap
  c:  8385930        0    unused        0     0         # "raw" part, don't edit

  d:  1843200  1843200    4.2BSD     2048 16384 28552
  e:  3072000  3686400    4.2BSD     2048 16384 28552
  f:  1627530  6758400    4.2BSD     2048 16384 28552
As you can see it includes the "c" partition and "b" or the "swap" partition as well as filetypes.

 

9.) Create a file and use vi to edit the file [Top]

We are going to open an empty file and write something in it. You can be root or a regular user for this exercise. It's better to not be logged in as root whenever possible (hint!).

The vi editor uses "modes"

This is a critical point. The vi editor has two modes. These are:

To go back and forth between these modes when you are in vi you can press: Remember this as it is confusing. The easiest thing to do when you get confused in vi is to press the ESCape key a couple of times and start over.

Now let's do the following:

$ cd /home/username
$ touch temp.txt
$ vi temp.txt
Now you are in vi. Press the "i" key to switch to input mode.

Type something like, "VI is great! I think I'll be using vi from now on instead of Microsoft Word."

Press ENTER to add lines. Type some more stuff, whatever you like.

Here is a short list of vi commands:

Open: vi fn, vi -r fn, vi + fn, vi +n fn, vi +/pat fn
Close: :w, :w!, :wq, :wq!, :q, :q!
Movement: h,j,k,l, w, W, b, B, :n
Editing: i, o, x, D, dd, yy, p, u
Searching: /pattern, ?pattern, n, N

OK, let's save the file that you are in. To do this do:

Press the ESCape key to get in to command mode

Press ":" to get ready to issue a file command

Type "w" and press ENTER to save your file.

Press ":" to get back to the prompt to issue a file command

Press "q" to quite the file

Instead of the multiple steps you could have type ":wq" to write and quite at the same time. If you need to quit a file without saving it after you've made changes, then you press :q!. For many people this is the most important command to remember in vi :-).

Below is a more complete vi cheat sheet. In addition you will be receive a vi summary book as part of the book package for this workshop.

                        vi Cheat Sheet

Open:  

vi filename             (fn=filename)
vi -r filename          Recover a file from a crashed session
vi + filename           Place the cursor on last line of file.
vi +n filename          Place the cursor on line "n" of file.
vi +/pat filename       Place cursor on first occurrence of "pat"tern

Close:

:w                      Write the file to disk. Don't exit.
:w!                     Write the file to disk even if read/only.
:wq                     Write the file to disk and exit.
:wq!                    Write the file to disk even if read/only and quit.
:q                      Quit the file (only if no changes).
:q!                     Quite the file even if changes.

Movement:

A                       Move to end of line, change to insert mode.
h                       Move 1 space backwards (back/left arrow).
j                       Move down 1 line (down arrow).
k                       Move up 1 line (up arrow).
l                       Move 1 space forwards (forward/right arrow)
w                       Move cursor to start of next word.
W                       Same as "w".
b                       Move cursor to start of previous word.
B                       Same as "b".
:n                      Go to line number "n" in the file.

Editing:

i                       Enter in to input mode.
o                       Add a line below cursor and enter in to input mode.
x                       Delete character (del key in some cases).
D                       Delete line from right of cursor to end of line.
dd                      Delete entire line.
u                       Undo last edit or restore current line.
p                       Put yanked text before the cursor.
yy                      Yank current line.

Searching:

/pattern               Search for "pattern" in the file going forwards.
?pattern               Search for "pattern" in the file going backwards.
n                      Find the next occurrence of pattern found forwards.
N                      Find next occurrence of patter found backwards.

Copy/Cut and Paste
nyyp                   Copy n lines to buffer, paste below cursor
nyyP                   Copy n lines to buffer, paste above cursor
nddp                   Cut n lines and copy to buffer, paste below cursor
nddP                   Cut n lines and copy to buffer, paste above cursor

Now let's copy a large file to your home directory so that you can play around with some more vi commands. We'll copy over your /etc/defaults/rc.conf file for this exercise. To do this we do:
$ cd
$ cp /etc/defaults/rc.conf .
Now let's edit this file. To do this type:
$ vi rc.conf
Play with moving around. Move your cursor to a line with text and see what happens when you go in to command mode (ESCape) and use "w" or "W" or "b" or "B" - remember, to get in to command mode press the ESCape key.

Now press "/" and type a word that is in your document, then press ENTER. What happens?

Do the same, but press the "?" key at first. Use ESCape to start in command again again if necessary.

To save your file press the ":" key and next type "w" and enter . .

To exit and save do:

:wq
To exit and not save anything (lose all changes you have made since the last save) do:
:q!
But, try to save your file for later use. Practice saving, exiting, opening a file in vi again, etc.

Feel free to open this file again and practice using the vi commands listed above.

 

10.) Process startup and the RC system [Top]

This is a rather complex topic. But, in a nutshell, under FreeBSD, when your machine boots processes (that is daemons or services) are configured and/or started like this:

To get a better feel for this you should read:
$ man rc
Then, you should probably read this again...

Now, if you want to start a process each time your machine boot you generally add an item to /etc/rc.conf to indicate this. For instance, if you wanted to enable the ssh daemon (server) each time your machine started then you would add the line:

sshd_enable="YES"
to /etc/rc.conf. If you look at /etc/defaults/rc.conf you'll see that sshd is not enabled in this file by default. By enabling sshd in /etc/rc.conf this overrides the setting in /etc/defaults/rc.conf. In addition, if you look in /etc/rc.d/ you'll find an sshd script file that starts this service.

To see how sshd is enabled initially do this:

$ grep sshd /etc/defaults/rc.conf
You should see something like:
sshd_enable="NO"                # Enable sshd
sshd_program="/usr/sbin/sshd"   # path to sshd, if you want a different one.
sshd_flags=""                   # Additional flags for sshd.
These are the lines in /etc/defaults/rc.conf that deal with the ssh daemon. When you specified to enable the ssh daemon during installation then in the file /etc/rc.conf the lines that read:
sshd_program="/usr/sbin/sshd"   # path to sshd, if you want a different one.
sshd_flags=""                   # Additional flags for sshd.
became active. So, if your ssh program was not "/usr/sbin/sshd" for some reason, then in /etc/rc.conf you could add:
sshd_program="/new/directory/sshd"   # new path to sshd
to override what's in /etc/defaults/rc.conf.

You could just put everything in /etc/defaults/rc.conf, but you don't want to do this. If you upgrade your system it's almost certain that /etc/defaults/rc.conf could be overwritten. In addition, this file is large and it would be hard to see the changes you had made if you were to do them in /etc/defaults/rc.conf.

For the following exercises you must log in to a terminal as root.

To start a service manually you can use it's startup script by hand. For instance, try typing:

# /etc/rc.d/sshd
What is returned on the screen? It should be something like:
Usage: /etc/rc.d/sshd [fast|force|one](start stop restart rcvar keygen reload status poll)
So, you could type
# /etc/rc.d/sshd status
to see if ssh is running. If it is, then try:
# /etc/rc.d/sshd stop
to start the service. Now type:
# /etc/rc.d/sshd start
to restart the service. Note the startup script option "reload" - This would let you make changes to the ssh configuration file(s) and then reload the service without actually stopping it so that it reads the new configuration. Note that already connected clients would not see this new configuration change until the logoff and log back in again.

Finally, and this is not obvious, if a startup script in /etc/rc.d has not been enabled in /etc/defaults/rc.conf or /etc/rc.conf, then even if you manually invoke the script it will not run. You will not get any indication of this other than the service not starting (i.e. use "ps auxw| grep servicename" and you won't see it started).

At this point take a closer look at /etc/rc.d/

# ls /etc/rc.d
and use "man" to read about some of the services. If you want to try and start and stop some of these services feel free to do so now, but remember you'll need to add 'servicename_enable="YES"' in /etc/rc.conf for the service to start.

 

11.) Shutdown and reboot your system [Top]

For this exercise you need to be root. It is better to close open files and programs (for example vi, etc.), but it is not necessary. Before continuing read the man pages for shutdown, init, halt, and reboot (you'll see they are all connected):

# man shutdown
# man init
# man reboot
# man halt
Now, in a terminal do the following (save data, etc. as this will immediately reboot your machine!):
# shutdown -r now
Now your machine is rebooting. The "-r" stand for "reboot" and the "now" meant to take this action "now". This takes a moment. To stop your machine entirely you can use the command:
# halt
But, if power management is not properly supported you may still need to press the power button to fully shut off your machine.

Or, you can also change your run level to run level 0, which is the same as "halt". So, you would write:

# init 0
And, to reboot this is the same as init 6, or:
# init 6
If you are running something like gdm for a graphical login prompt on your machine you can usually use the provided menu choices to reboot or shutdown. The thinking is that once you have this level of access, then you can simply turn off the machine's power if you wish. At the very least it is much more friendly to use a software interface to shutdown or reboot than pulling the power as processes have a chance to clean up, save data, etc.

Note, sometimes it is useful to bring your machine down to runlevel 1, or "single user mode". For instance, if you are running X Windows and want to shut it down quickly (you'd really only do this on a desktop machine, by the way!), then you can open a terminal window as root (or use "su"), and then type"

# init 1
This will shut down X Windows, networking, and quite a bit more. Now you are in "single user mode". To get back to "multi-user mode" you simply type:
# exit
This exits your single-user mode shell and tells the system to go back to multi-user mode.

 

12.) Basic network configuration [Top]

To view the status of your network interfaces you use the command ifconfig. If you have an ethernet card, then, in general, you will see two network devices when you type the command:

$ ifconfig
Here is a sample output from this command on a laptop that uses a Lucent wireless card:
lo0: flags=8049 mtu 16384
        inet 127.0.0.1 netmask 0xff000000 
        inet6 ::1 prefixlen 128 
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
wi0: flags=8843 mtu 1500
        inet6 fe80::202:2dff:fe5e:86fb%wi0 prefixlen 64 scopeid 0x2 
        inet 196.200.222.247 netmask 0xfffffe00 broadcast 196.200.223.255
        ether 00:02:2d:5e:86:fb
        media: IEEE 802.11 Wireless Ethernet autoselect (DS/2Mbps)
        status: associated
        ssid AfNOG 1:AfNOG
        stationname "FreeBSD WaveLAN/IEEE node"
        channel 1 authmode OPEN powersavemode OFF powersavesleep 100
        rtsthreshold 2312 protmode CTS
        wepmode OFF weptxkey 1
There are two devices listed here, "lo0" (loopback 0), and "wi0" (wireless 0). FreeBSD references network interfaces by the driver that is loaded to communicate with the device. Thus the "wi0" device uses "wi" driver, which in this case supports a Lucent Technologies wireless card. The "lo" or "loopback" interface is a special interface that is always present. It is a virtual network interface that is only on your computer.

You can use the ifconfig command to bring "down" a network device, to make it active, to change the IP address associated with the device, the netmaks, the SSID for wireless cards, and so forth. There are many, many options and capabilities for the ifconfig command. You should read:

$ man ifconfig
to understand what is possible.

Substitute "rl0" with your network card name as appropriate. And, before making changes note down what your current IP address and netmask are so that you can reset your machine's network configuration. Do not worry if you can't do this at the end of the exercises. Simply ask an instructor for help. It is an easy thing to fix and a good way to learn.

If you wanted to assign an IP address to the "rl0" (RealTek driver) network card and give it a netmask of 255.255.255.0 (or a /24 network), then you could do this (you need to be root to change settings on a network interface, so login as root in a terminal now):

# ifconfig rl0 inet 192.168.1.100 netmask 255.255.255.0
Optionally you could do:
# ifconfig rl0 inet 192.168.1.100/24
If you wish to make this permanent each time you boot your machine, then you should edit the file /etc/rc.conf (as root) and add an entry like this:
# vi /etc/rc.conf
# bring up the rl0 network interface
ifconfig_rl0="inet 192.168.1.100 netmask 255.255.255.0"
Once you've done this you can type a command like:
# /etc/rc.d/netif restart
to reinitialize the network with the new parameters.

Finally, if you are on a network that uses DHCP to obtain network addresses you can use the following tool to obtain an address for a network interface quickly:

# dhclient rl0
And, if you want your machine to boot and automatically get a network address via DHCP for an interface, then you edit the file /etc/rc.conf and add the following entry:
ifconfig_rl0="DHCP"
If you wish to completely stop network and/or restart networking you can use the netif script in /etc/rc.d/ (as root). For instance, if you type:
/etc/rc.d/netif stop
your network interfaces ("lo0" and "rl0" for instance) will stop. You can type:
/etc/rc.d/netif start
to restart them. You may have noticed the use of "0" (zero) in the interface names. This is how you can distinguish between two identical network cards if you have a machine with two (maybe your machine is acting as a firewall or gateway). In this case you might have two RealTek-based cards in your machine. You would then have network interfaces "rl0" and "rl1".

 

13.) Initial login, virtual terminals, command line manipulation [Top]

The first time you login on your system after installation you will be presented with a prompt that looks something like this:

FreeBSD/i386 (name.domain) (ttyv0)

login:
At this point you can enter in "root" and, when prompted, the password we gave you in class for the root account. Once you are logged in you can work with an additional 7 virtual terminals if you wish. Actually, you can login from any virtual terminal you want at any time. To do this simply press:

CTRL-ALT-FN

With "FN" being anything from the F1 to the F8 key. By default you are in ttyv0 which corresponds to the CTRL-ALT-F1 keyboard sequence. Go ahead and login and then press:

CTRL-ALT-F2

and login again. Feel free to do this on F3, F4, F8, etc. as you wish. You can cycle through each terminal session easily. This is an extremely useful technique when you wish to do more than one thing at the same time, but you are not using a graphical interface. Since we are loading the mouse daemon (mouse support) you can, also, copy and paste text between your virtual terminals. To do this go back to your initial login terminal by pressing:

CTRL-ALT-F1

Try typing in the command:

$ clear
and pressing ENTER. Note that your screen clears and goes to the top. Now, you can easily recover your last command by pressing the UP-ARROW key on your keyboard once. Get used to doing this as it can be very useful if you have entered in a long command and made a mistake. You can press UP-ARROW to get the command back, then you can use the LEFT-ARROW to move your cursor to where the mistake is and correct it, then simply press ENTER to reissue the command. Note, you do not need to go back to the end of the command line before pressing ENTER.

In any case, you've pressed UP-ARROW once and should have the command "clear" visible. Now take your mouse and highlight just the command using the left mouse button. Without doing anything else with your mouse now press:

CTRL-ALT-F2

to get to one of your other terminal sessions. Now just press the middle mouse button once. What happened? The text "clear" should have pasted on to your command line. Now you can press ENTER to execute the command. You can use this same trick to copy and paste in to editor windows, long and complex commands, between applications in a graphical environment, etc.

One final useful tip when working in your terminal sessions. As you type each command it is being saved in to a file called your "history file". This has a very useful purpose. Go back to your original login terminal pressing:

CTRL-ALT-F1

and type the command:

$ history
You should see a list of commands you have entered in earlier. This list is probably short and it will even include incorrect commands you may have typed in. To quickly and immediately recover and execute a prior command make note of the number in the left-hand column next to the command and then just type:
$ !N
Where N is the number. So, if "clear" had been the second command, and you typed in:
$ !2
Then clear would appear on the command line very quickly and immediately execute. During the week you are going to be typing in some long and complex commands. The use of history can save you considerable time. If you press the UP-ARROW key repeatedly you can scroll through the previous commands you have entered beginning with the last command. Give it a try.

Here's one scenario where virtual terminals might be useful...

You've just installed your favorite MTA (email server/Mail Transport Agent) and you typed something like (don't do this now):

pw usermod username -G mail
to place your user in the "mail" group. What just happened if your user was also a member of the "wheel" group and this is how you are using su and sudo? You can no longer become root in your session. To fix this you could go to a virtual console window, log in as root, and reissue the command above, but this time correctly:
pw usermod username -G wheel,mail
To fix this problem.

[Return to Top]

Hervey Allen


Last modified: Wed May 6 06:41:36 CEST 2009