Unix Introduction/Refresher
This serves as *minimum* unix knowledge base for this course. Students new to the command line should not be discouraged if they are not already familiar with all of the following, but they should study this section closely, as the tools and concepts here will re-appear regularly when dealing with both networking and security. The network utilities and files sections can be skipped initially, as their usage will be explained in more depth in the system administration
GettingStarted section.
Contents
- Fundamental Concepts
- Why Unix?
- The Shell
- Permissions and Root
- Installing Applications
- Man pages
- Frequently Used Utilities and Files
- Basic Utilities
- Common Files and Directories
- Network Utilities
- Network Files
Fundamental Concepts
Why Unix?
Unix-based operating systems are by and large the most robust and secure systems available. Despite comprising only a small percentage of the personal computer market, they are far more common in business, education, and any networking settings due to their efficient multitasking, network implementation, and largely open-sourced design (not all versions of Unix are open-source, but most rely largely on an open-source software base). Due to their multitasking implementation (and decades of testing), Unix systems also tend to be very hard to crash (though certainly not impossible), which also makes them ideal for servers that need to run unhindered for months on end.
The term "Unix" no longer refers to one specific operating system, as the project has divided over the years, though all share a similar framework. Some of the most popular derivations include Linux, SunOS (Solaris), and Mac OSX. Linux itself is divided into many different distributions, or "distros", which tend to have unique filesystems and slightly different organization. It is usually easy to translate knowledge of one into understanding of another though. From here out, unless otherwise noted, examples will be for a Linux system (with details when distro specific).
The Shell
Whenever you're at the command line, you're using a "shell" to interact with the computer. Shells, as the name suggests, separate users and allow them each their own unique interface. They're also nesting (think Russian dolls, only with lots of space between each doll). I can be logged in as "joedirt, type "su fishpaste" ("su" means "substitute user"), and find myself in a new shell for fishpaste. Any changes I make to fishpaste's shell will not affect joedirt's. Similarly, I can be logged in as "mork" on my machine, then use ssh to log into the cs server as "mindy". Again, this is a separate shell and won't affect mork's carefully edited shell initialization file.
A shell has a collection of "environment variables", variables that can affect the behavior of the shell and applications running within it. Several of these are created and updated by the shell itself, such as ones for user and present working directory, though you can create your own as you see fit.
There are different shells available, including bash, csh, and tcsh, and each has a slightly different syntax. Bash is the default shell on most systems and the most common, so we will use that for our examples.
Permissions and Root
Every file and directory on a unix box has a set of permissions. This set specifies different users' abilities to read, write, and/or execute. The permissions are divided into three categories: owner (each file has a unique user classified as owner), group (which can contain any number of users), and everyone else. This allows the owner to give only the minimum necessary access to other people.
You've probably heard "1337 haxxorz" claim they've "rooted" a box. Each Unix system has "root" account with access to everything. This can be very good or very very bad, depending on the circumstances, and you should be very careful when logged in as root. This is where you can accidentally destroy your filesystem and lose all your data with a single command, or where an attacker can "own the box", changing your passwords, deleting your logs, replacing harmless programs with malicious ones. On the other hand, you'll be able to access all parts of your system and do some pretty cool things, so be careful, not afraid.
Installing Applications
Most Linux systems have some sort of package manager -- a tool that will download, compile, and install applications from a repository for you. Debian/Ubuntu uses "apt-get" and Red Hat/Fedora uses "yum". Mac OSX also has a package manager called "fink". The repositories for these tools contain thousands of applications, including most of the most popular ones.
At times however, it is necessary to compile and install an application "by hand". To do this, move to the directory of the source code, run "./configure", "make", and "make install". In order, these three steps set up parameters for compilation and installation, compile an executable program, and copy the executable along with any libraries, man pages, etc. to their desired locations. Sometimes different systems require that ./configure be run with specific options in order for the program to successfully compile, which are not always obvious. At these times, the best thing to do is either search for distro/hardware specific installation instructions, or a pre-compiled compatible version.
Man pages
Man pages are your friend. All of the standard Unix tools (and most tools you install) will have a man (manual) page. You'll find information on what they do, how (and often advice on when) to use them, what options are available, and occasionally the odd history or math lesson. When all else fails, start here.
Frequently Used Utilities and Files
Basic Utilities
- cd - "Change directory."
- ls - "List contents of directory."
- mv - "Move a file or directory."
- cp - "Copy a file or directory."
- cat - "Concatenate and print files." Dumps it all on you at once.
- more - Like cat, but presents it in a scrollable, more user friendly form.
- less - "Less is more." A newer version of cat.
- pwd - "Present working directory." Displays the full path to the directory you're currently in.
- echo - Prints argument to the screen.
- top - Displays the top processes running based off of CPU usage. Lots of handy info here.
- ps - "Process status." Like top, but only lists processes you are currently running from terminals.
- printenv - "Print environment." Prints all your current environment variables.
- passwd - Set a user's password (yours by default).
- su - "Substitute user." Gives you a new shell for a specified user (or root by default).
- sudo - "Substitute user do." Execute a command as a specified user (or root by default).
- chown - "Change owner." Modify the owner and group of a file.
- chmod - "Change file mode." Change permissions for a file or directory.
- ssh - "Secure shell." Using the miracles of the interweb, we can now talk to our computer without being right in front of it.
- scp - A version of copy that uses ssh to transfer files between computers.
- exit - Close the current shell.
- reboot - Exactly what you think it does.
- man - Use by typing "man " to display the man page for the specified program.
Common Files and Directories
Note that files and directories whose names begin with "." are considered "hidden", and will not be displayed by ls without the "-a" option.
- /home// - The home directory for . Each user has a directory like this, which is often the only area in which the user is free to modify file and directories. In OSX, this directory is located at /Users//.
- /home//.bashrc - The bash initialization file for . This file allows you to customize your shell, including appearance, shortcuts, and environment variables, and will be checked every time you start a new shell. Different shell types have similar files, usually of the form ".rc".
- / - The root filesystem. The root of the file tree hierarchy, within which all files and directories are contained.
- /etc/ - Home to most system initialization files and scripts. Pronounced "etsee".
Network Utilities
- ifconfig: Get information about your various interfaces, as well as configure them.
- iwconfig: Similar to ifconfig, but designed for wireless interfaces. We won't worry much about this one.
- route: Manage your routing table.
- iptables: Manage your firewall.
- dhclient: Script to configure your networking settings from a DHCP server.
- arp: manage your arp cache.
- netstat: List information about currently used sockets, local and net-related.
- tcpdump: The poor man's ethereal (then again, what's cheaper than free?). If you're having network problems and can't even download your preferred packet sniffer, this can be helpful for diagnosing problems.
- etc/init.d/network: Network configuration tool. Runs when you boot up, but can be run again if you play with your configuration files.
Network Files
- /etc/resolv.conf: List of nameservers for DNS lookup.
- /etc/sysconfig/networking/devices/ifcfg-eth* (Red Hat/Fedora specific): Configuration files for your different interfaces. These are what get read when the network system tool is run (modifying them requires a network restart to take effect).
Sources and Further Reading