This page does not represent the most current semester of this course; it is present merely as an archive.
The purpose of this lab is to get you familiar with the most important aspects of the command line environment, sometimes called the shell, sh, bash, or the terminal.
It is our plan to have most labs use NoMachine/NX to have you interact with a Linux environment from your personal computer. However, the systems staff is a bit behind schedule in creating all NX accounts. It is possible this lab will not use NX, even though it will prepare you to do so in the future.
We have not used this particular lab before. Thus, we might have targeted the length incorrectly. Follow the TAs’ suggestions on pacing, if to skip bits, etc.
Download and run the installer for your operating system
Run the resulting exectuable (which may be called “No Machine”, “NX”, or “nxplayer” depending on platform)
You may see a “welcome” screen; if so click past it
There are several variants of the next screen, but all of them have a “New” button; click this
There are five steps to setting up a new connection
Protocol: leave as default “NX”
Host: use nx.cs.virginia.edu
, with default port (4000) and UDP communication
Authentication: use “password”
Proxy: do not use a proxy
Save as: name your connection; you’ll want to re-use this instead of selecting “New” in the future
Connect to your newly-created session
If asked, Accept host authenticity. If you care to check, it’s
nx.cs.virginia.edu
nxfront
re-routes you to the least-used back-end computer), all beginning 128.143.67
SHA 256 2D b9 74 B2 5C 7A 92 6F DD FD 98 2E 0D 35 A1 E5 99 8A A8 5B 81 A7 41 16 8F 46 0F 03 9E 3F 7F B1
.
Note: this might change as certificates are updated; I’d not suggest checking it unless you are particularly paranoid.
Log in with your computing ID and the password you were emailed by the CS systems administrator (not your netbadge password; this one is separate and should have been emailed to you on Friday)
It will probably require you to change your password. Do so. And remember your new one.
Create a new virtual desktop (unless you already have one created from earlier)
Unless you know you want something else, pick the “fit to window” and the “menu panel covering all screens” options
You are now in a Linux (CentOS) remote desktop! For this lab, open a terminal:
There are other applications available, such as the Firefox web browser; if you want to use command line tools beyond what this class will require you might also be interested in the department modules documentation.
Our goal in this lab is for you to understand how to navigate on the command line. In particular, you should be able to
man
, --help
, and -h
Visit http://web.mit.edu/mprat/Public/web/Terminus/Web/main.html, a somewhat cheesy introduction to the basics of the command line. Explore it until you
ls
, pwd
, cd
(including cd ..
), and less
mv
and man
There is a lot more you can do (creating a magic locker, explore a hidden tunnel, learn about grep
and rm
, etc.) but those are the most important basics. If NX
is not working, keep exploring for the full lab time; you’ll learn a lot and hopefully also have fun along the way. Otherwise, continue with Step 2.
The most common “command line environment” in Linux is called bash
, a variant of the more primitive sh
ell. Terminus was somewhat like bash; following is a comparison
Terminux | Bash |
---|---|
Location | Directory |
Item | File |
pwd shows one location |
pwd shows a full “path”: each step needed to get here, separated by / |
ls shows locations and items separately |
ls shows all directories and files in one list |
mv only moves items into locations |
mv can also move directories into one another and rename files (as e.g. mv oldname newname ) |
less and man show text below current text |
less and man temporarily take over the screen, letting you scroll around with arrow keys until you exit the view with the q key |
Try these out in your NX terminal.
The first word you type is called a “command”; after that come a series of “arguments” or “command-line arguments”. Together, the command and its arguments make up a “command” or “command line”.
Many commands accept special arguments beginning with a hyphen called “options”. For example, most include an option named either -h
or --help
that gives a shorter summary of usage than does man
.
Most (though not all) command lines will provide various forms of autocompletion to help streamline interaction. The two most useful are
Pressing the tab key when the cursor is preceded by an incomplete word that can only be completed in one way will fill in the rest of the word.
Pressing tab twice when the cursor is preceded by an incomplete word that can be completed in several ways lists all of the completions the command line knows about.
These even work in Terminux.
ssh
There is a very useful command ssh
that allows you to log into a different computer’s command line remotely. To learn this, and a few other commands, we have another game:
Visit http://overthewire.org/wargames/ and read. Many of the pages list web resources to help you learn more.
If you don’t like reading (☹), try
We suggest getting to level 4 of Bandit, though you might find other levels and games interesting.
chmod
and set up your directory safelyEach directory has three permissions, called “read”, “write”, and “execute”.
ls
won’t work in the directory.
cd
won’t work into the directory and nothing will work with a path that includes the directory in the middle.
You can change permissions using the chmod
command. It has multiple ways to be used, but a few simple examples are
chmod a+r foo # all users can read foo
chmod a-w foo # no user can write foo
chmod u+w foo # the owning user can write foo
Directories can contain other directories, and also can contain files. Files, like directories, have names and permissions but cannot be entered with cd
. The permissions also have different meaning than with directories:
Every user belongs to one ore more groups, and every file or directory has an owning user and an owning group. Permissions are specified as read/write/execute for the user, group, and others.
You can find out your user name with whoami
and your group memberships with groups
.
You should set up your home directory so only you can access it, not other people in your group nor strangers not in your group:
cd # go home
chmod g-rwx . # remove group-access to read, write, and execute this directory
chmod o-rwx . # remove other-access to read, write, and execute this directory
The main interactions you have in a terminal consist of directories, files, and commands.
You type commands. First comes the name of the command, and then optionally an number of arguments to it. Commands are kind of like functions, but the syntax is different: spaces are used instead of parentheses and commas.
One of the commands is called echo
. It is sort of like a print
in Python or a println
in Java. For example, if you type
you should see a line displayed,
This is my first command
It is traditional to display example commands to run with a $
before them, like
You don’t type the $
when you see this.
Some characters have special meaning, and need to be escaped if you want to use them by using a backslash \
or enclosing the command in single quotes.1
Most commands take a few basic arguments and any number of a large set of option specifiers.
Option specifiers are generally provided in one of several ways:
Many options are a single character long, preceded by a hypen. For example, ls
has an option -a
that shows hidden files and -l
that lists more details. Single-letter options can be combined into a single option, so you can use these by running any of the following:
If a single option is more than a single character, preceed it by two hypens. Long options cannot be combined like short options can. For example, ls
’s -a
option can also be written --all
Some options expect a special value to be provided with them. For example, ls
has an option -w
that needs to be followed by an integer specifying how many characters wide the display wants to be.
If a long option has an argument, it is indicated with a =
and no spaces. For example ls
’s -w
option can also be written --width
Command | Example use | Description |
---|---|---|
cd |
cd foo/baz |
change directory |
pwd |
pwd |
print the current working directory |
ls |
ls |
list directory contents |
ls -l |
ls with display in long form |
|
mkdir |
mkdir foo |
make a new directory |
cp |
cp foo baz |
copy a file to a new name or directory |
mv |
mv foo baz |
move or rename a file or directory |
rm |
rm foo |
remove (i.e., delete) a file (warning: there is no trash bin to undelete from) |
rm -r foo |
rm recursively, deleting a directory and all of its contents. Dangerous! Can result in a lot of files vanishing forever if you type the wrong directory name. |
|
rmdir |
rmdir foo |
rm an empty directory. Use rm -r if the directory is not empty. |
cat |
cat foo |
concatenate files and print their contents |
less |
less foo |
show a scrolling view of a file’s contents (type q to exit the view) |
man |
man ls |
show the manual page for a command |
chmod |
chmod a-r foo |
change the file mode bits (permissions) |
ssh |
ssh mst3k@labsrv01 |
secure shell (log into remote computer) |
scp |
scp foo mst3k@labsrv01 |
cp over ssh (copy files to/from remove computer) |
Commands don’t have to print to the screen or read input from the keyboard; both of these can be redirected to files or other commands.
If a command ends with > filename
it will print to a file with that name instead of to the screen. Warning: this will replace any contents a file of that name used to have!
If a command ends with >> filename
it will print to the end of a file with that name.
If a command ends with < filename
it will read input from a file with that name instead of from the keyboard.
If two commands are separated by |
, the first prints to the “keyboard” of the second.
Try these out: run each of the following lines one at a time and make sure you understand why it outputs what it does.
$ mkdir tmp
$ cd tmp
$ ls
$ pwd
/home/mst3k/tmp
$ echo This is going into a file > newfile.txt
$ ls
newfile.txt
$ cat newfile.txt
This is going into a file
$ echo another line >> newfile.txt
$ ls
newfile.txt
$ cat newfile.txt
This is going into a file
another line
$ echo and another > newfile.txt
$ cat newfile.txt
and another
$ cd ..
$ rm tmp
rm: cannot remove ‘tmp’: Is a directory
$ rmdir tmp
rmdir: failed to remove ‘tmp’: Directory not empty
$ rm -r tmp
Most (though not all) command lines will provide various forms of autocompletion to help streamline interaction. Common components include
Pressing the tab key when the cursor is preceded by an incomplete word that can only be completed in one way will fill in the rest of the word.
Pressing tab twice when the cursor is preceded by an incomplete word that can be completed in several ways lists all of the completions the command line knows about.
If a command begins with a !
(pronounced “bang”), what follows helps specify a previously-typed command.
!ech
ech
!-
n, where n is an integer!?ech
ech
somewhere in it
Directories are like named rooms with other rooms inside them.
cd
and ls
You can enter one of a directory’s contained rooms with the cd
command
You can look around the room and see what’s there with the ls
command
$ ls # lists all the things in the current directory
$ ls baz # lists all the things in the "baz" directory
There are four special directory names
.
cd .
does nothing
..
cd ..
enters the directory that contains the current directory
/
cd /
exits all directories and goes to the “root” directory. Does not work in thew middle of a directory path, only the beginning.
~
You can combine directory moves by connecting names with /
; thus cd foo/baz/../xyxxy
does the same thing as
which also does the same thing as
or cd foo/xyxxy
; the ..
undoes the preceding baz
.
Each directory has three permissions, called “read”, “write”, and “execute”.
ls
won’t work in the directory.
cd
won’t work into the directory and nothing will work with a path that includes the directory in the middle.
You can change permissions using the chmod
command. It has multiple ways to be used, but a few simple examples are
$ chmod a+r foo # all users can read foo
$ chmod a-w foo # no user can write foo
$ chmod u+w foo # the owning user can write foo
Directories can contain other directories, and also can contain files. Files, like directories, have names and permissions but cannot be entered with cd
. The permissions also have different meaning than with directories:
There are at least three games that teach more about the command line:
Visit http://overthewire.org/wargames/ and read. Many of the pages list web resources to help you learn more.
If you don’t like reading (☹), try
and consult http://overthewire.org/wargames/bandit/bandit0.html to get started.
Visit http://web.mit.edu/mprat/Public/web/Terminus/Web/main.html, read, and type.
Type the following to get started
Double quotes sometimes work too, but don’t escape $
, \``,
!, or
\`. They also have some nuanced special meanings we won’t encounter in this course.↩