**Unix Commands Exercises**
Goals
====
* Introduction to your Virtual Machine (VM)
* Linux System basic commands
## Notes:
* Commands preceded with _$_ imply that you should execute the command as a general user - not as root.
* Commands preceded with _#_ imply that you should be working as root.
* Commands with more specific command lines (e.g. "rtrX>" or "mysql>") imply that you are executing commands on remote equipment, or within another program.
* If a command line ends with "\" this indicates that the command continues on the next line and you should treat this as a single line.
Exercises
=====
Log in as the ubuntu user using ssh to your machine.
~~~
username: ubuntu
password: {given in class}
~~~
~~~
ssh ubuntu@100.64.0.N
~~~
## Become the root user
At the command prompt type the following command:
~~~
$ sudo -s
~~~
Enter your password when prompted
Now that you are root the command prompt will change. We indicate this using the “#”
symbol.
You are now the super user - be careful!
Ok, exit the root account:
~~~
# exit
$
~~~
## Look at the network configuration of your host
~~~
$ cat /etc/network/interfaces
~~~
Notice that configuration of your host is done using DHCP.
"cat" is for "concatenate" and is one way to view what is in a file.
## List files:
Use ls to list files:
~~~
$ cd [go to your home directory]
$ ls
~~~
Do you see anything? Try this instead:
~~~
$ ls -lah
~~~
What's inside one of these files?
~~~
$ cat .profile
$ less .profile
$ clear
~~~
Press “q” to get out of the less display.
If you don't understand what cat, clear or less do, then type:
~~~
$ man cat
$ man clear
$ man less
~~~
## Working with the command prompt:
You can recall previous commands by using the up-arrow and down-arrow keys. Give this a try now.
Alternately, try typing this command:
~~~
$ history
~~~
If you wish to execute one of the commands in the list you saw type:
~~~
$ !nn
~~~
Where “nn” is the number of the command in the history list. This is useful if you want to run a past command that was long and/or complicated.
Command completion:
With the bash shell you can auto-complete commands using the tab key. This means, if you type part of a command, once you have a unique string if you press the TAB key the command will complete. If you press the TAB key twice you'll see all your available options. Your instructor will demonstrate this, but give it a try by doing:
~~~
$ hist
$ del
$ rm [Include the space after the “rm”]
~~~
# Working with pipes:
## Examples
We saw an example of using pipes when we sorted the contents of our /sbin directory during the presentation. What if you wanted to have this information available in a file and sorted?
~~~
$ cd
$ ls /sbin | sort > sbin.txt
~~~
Now view the contents of what is in sbin.txt to verify that this worked.
~~~
$ less sbin.txt
~~~
Press the "q" key to quit viewing the contents.
## Finding text strings:
Use the command grep to print lines matching a pattern in a data stream (such as a file). For example, view the entry for the ubuntu account in the system passwd file:
~~~
$ sudo grep ubuntu /etc/passwd
~~~
You should see something like:
ubuntu:x:1000:1000:System Administrator,,,:/home/ubuntu:/bin/bash
The previous items above are:
userid:passwd:uid:gid:Name,extrastuff,,:HomeDir:LoginShell
grep is often used with a pipe to FILTER the output of commands. For instance:
~~~
$ history | grep ls
~~~
Will display your previous use of the ls command from exercise 2.
# Update and installing services ##
## Update your software package repository
~~~
$ sudo apt-get update
~~~
This might take a few moments if everyone in class is doing this at the same moment.
## Install the “joe” editor package
~~~
$ sudo apt-get install joe
~~~
The joe editor package just like vi. Try using the editor to create a new file in your ubuntu home directory:
~~~
$ cd
$ joe newfile.txt
~~~
## Install the postfix mailerver software and some mail utilities
At the command line type:
~~~
$ sudo apt-get install postfix mutt mailutils
~~~
You will several prompts. If you are using putty ssh sometimes the screens can be harder to read.
When you are prompted (a fair number of packages will be installed):
* At the initial prompt press {Enter} for {Ok}
* When available, select "Internet Site" (use tab key to move to {Ok} and press {Enter} to continue)
* Accept the hostname presented (tab to {Ok} then press {Enter}
Several tools will use the postfix mailserver during the week. In addition, we will use a number
of the mail utilities (such as _mail_) and you will use the _mutt_ email reader later in the week.
## Stopping and starting a service
Restart the mail service, postfix
~~~
$ sudo service postfix restart
~~~
To do this you can do:
~~~
$ sudo service postfix stop
~~~
You can see if a service is running by typing:
~~~
$ sudo service postfix status
~~~
Now you can start the service again and check the status using the command above.
~~~
$ sudo service postfix start
~~~
You can see if a service is running by typing:
~~~
$ sudo service postfix status
~~~
If a process ID is displayed, then the service is running, but our next exercise will show you another way to verify this.
## Finding and stopping processes
If you wish to find something that is running and then stop it you can use the "ps" (process) command with "grep" and "kill". Let's do this by opening two connections to your virtual machine.
### Using SSH open two terminal connections to your Linux server (user: ubuntu machine: 100.64.0.N)
### One you have opened two terminals go in to one terminal and type:
~~~
$ tail -f /var/log/syslog
~~~
This will let you look at the end of the syslog log file in real time. If events take place that are logged you will see them as they happen. Now, in your other terminal let's look for this process:
~~~
$ ps auxwww | grep tail
~~~
The "auxwww" are options to the ps (process) command. The options mean display all process running that belong to you and to other users and provide information about who owns what process. The three "www"'s mean display the entire command line regardless of how long it is and wrap it in your window.
You will likely see something like this:
root 6903 0.0 0.0 7200 612 pts/0 S+ 03:28 0:00 tail -f /var/log/syslog
root 6986 0.0 0.0 9388 924 pts/1 S+ 03:28 0:00 grep --color=auto tail
You could press "CTRL-C" in the terminal window where the tail command is running, or to stop the process right now you can use the kill command. You need to replace the Process ID (PID) with the process ID number of the tail command running on your machine. In this example the number is "6903". At the command prompt type:
~~~
$ kill NNNN
~~~
Where NNNN is the PID of your tail process. Once you do this return to the other terminal screen. The "tail -f" process should now have exited and you should see something like:
Terminated
ubuntu@pcN:~$