Wednesday, August 29, 2007

Basic Linux Commands - 3

Filesystem

If you are comfortable at the DOS prompt, you should see a lot of similarities in Linux; if most of your experience has been in Windows, the command line may seem a little awkward at first.

Remember that Linux has an hierarchical, unified filesystem (directories within directories; and files, directories, and device drivers are treated as files), supports 256-character filenames (avoid symbols and punctuation except for the dot (.) and note that you can have more than one nonadjacent dots in the filename - e.g. this.is.okay). All command line entries are case sensitive. Also note that Linux uses the slash (/) rather than the backslash (\) you've been using in DOS.

There is extensive online help available on any command on your system called the man pages (or manual pages); type in man CommandInQuestion to get a summary of what the command does and a brief summary of the options. Unfortunately, using the man pages is not an efficient or effective way to learn Unix commands, but is often helpful with syntax.

Linux, like all Unix, assumes you know what you're doing and that you do not make typographical errors. Linux, like all Unix, will execute whatever command you give it - all that matters is that it's a valid command. If it's not what you intended, that's your mistake, so don't blame it on the operating system! This operating system allows you to do your work without constantly nagging you.

There are four types of file:

ordinary files - text files (plain vanilla ASCII), data files (contain special characters not contained in the ASCII set you are familiar with), command text files (shell scripts), and executable files (binaries); directories; links (we'll get into this below); special device files (physical hardware)

The main subdirectories (and contents) are:

/bin - binary files; /boot - information need to boot the system; /cdrom - your CD-ROM drive; (/dos) - your DOS partition; /dev - device drivers; /etc - miscellaneous files (mostly system administration); /home - home directories for users; /lib - programming libraries; /tmp - temporary files; /usr - commands; /var - files that define the system.

(There may be some slight differences between distributions.)

Prompt

If you login as root, the prompt will be machinename:~# and if you login as user, the prompt will be machinename:~$.

The tilde character (~) represents the home directory; appended to the end of a filename, it means a backup of a file that has been edited (the file as it existed before it was last edited, and if your configuration is set up to make backups).

Shell Account




How to Explore Your Shell Account


So you’re in your shell account. You’ve tried the “ls -alF” command and
are pretty sure this really, truly is a shell account. What do you do
next?

A good place to start is to find out what kind of shell you have. There
are many shells, each of which has slightly different ways of working. To
do this, at your prompt give the command “echo $SHELL.” Be sure to type in
the same lower case and upper case letters. If you were to give the
command ECHO $shell, for example, this command won’t work.

If you get the response:

/bin/sh

That means you have the Bourne shell.

If you get:

/bin/bash

Then you are in the Bourne Again (bash) shell.

If you get:

/bin/ksh

You have the Korn shell.

If the “echo $SHELL” command doesn’t work, try the command “echo $shell,”
remembering to use lower case for “shell.” This will likely get you the
answer:

/bin/csh

This means you have the C shell.
Why is it important to know which shell you have? For right now, you’ll
want a shell that is easy to use. For example, when you make a mistake in
typing, it’s nice to hit the backspace key and not see ^H^H^H on your
screen. Later, though, for running those super hacker exploits, the C
shell may be better for you.

Fortunately, you may not be stuck with whatever shell you have when you
log in. If your shell account is any good, you will have a choice of
shells.

Trust me, if you are a beginner, you will find bash to be the easiest
shell to use. You may be able to get the bash shell by simply typing the
word “bash” at the prompt. If this doesn’t work, ask tech support at your
ISP for a shell account set up to use bash.

If you want to find out what other shells you have the right to use, try
“csh” to get the C shell; “ksh” to get the Korn shell, “sh” for Bourne
shell, “tcsh” for the Tcsh shell, and “zsh” for the Zsh shell. If you
don’t have one of them, when you give the command to get into that shell
you will get back the answer “command not found.”

Now that you have chosen your shell, the next thing is to explore. See
what riches your ISP has allowed you to use. For that you will want to
learn, and I mean *really learn* your most important Unix commands and
auxiliary programs. Because I am supreme arbiter of what goes into these
Guides, I get to decide what the most important commands are. Hmm, “ten”
sounds like a famous number. So you’re going to get the:



Ten Meinel Hall of Fame Shell Account Exploration Tools

1) man
This magic command brings up the online Unix manual. Use it on each of
the commands below, today! Wonder what all the man command options are?
Try the"man -k" option.

2) ls
Lists files. I suggest getting people in the habit of using "ls -alF".
This will come into play down the road for security-conscious users.”
You’ll see a huge list of files that you can’t see with the “ls” command
alone, and lots of details. If you see such a long list of files that they
scroll off the terminal screen, one way to solve the problem is to use “ls
-alF|more.”

3) pwd
Shows what directory you are in.

4) cd
Changes directories. Kewl directories to check out include /usr, /bin and
/etc. For laughs, I advise exploring in /tmp. Changes
directories. Kewl directories to check out include /usr, /bin and
/etc. /usr directory belongs to the accounts, /bin belongs to the commands
and executable programs/files, and /tmp of course, the temporary
directory.

5) more
This shows the contents of text files. Also you might be able to find
“less” and “cat” which are similar commands.

6) whereis
Think there might be a nifty program hidden somewhere? Maybe a game you
love? This will find it for you. Similar commands are “find” and “locate.”
Try them all for extra fun.

7) vi
An editing program. You’ll need it to make your own files and when you
start programming while in your shell account. You can use it to write a
lurid file for people to read when they finger you. Or try “emacs.” It’s
another editing program and IMHO more fun than vi. Other editing programs
you may find include “ed” (an ancient editing program which I have used to
write thousands of lines of Fortran 77 code), “ex,” “fmt,” “gmacs,” you
may find include “ed” (an ancient editing program which I have used to
write thousands of lines of Fortran 77 code), “ex,” “fmt,” “gmacs,”
“gnuemacs,” and “pico.”

8) grep
Extracts information from files, especially useful for seeing what’s in
syslog and shell log files. Similar commands are “egrep,” “fgrep,” and
“look.”

9) chmod
Change file permissions.

10) rm
Delete file. If you have this command you should also find “cp” for copy
file, and “mv” for move file.



LAN and Internet exploration tools




1) telnet
Telnet allows you to login remotely from a remote computer to a host
server running any unix or unix clone system. Other variation is called
RLOGIN/rlogin. A newer variation also allows you to login more securely
using the secure shell (SSH).

2) who
Shows you who else is currently logged in on your ISP’s LAN. Other good
commands to explore the other users on your LAN are “w,” “rwho, ” “users.”

3) netstat
All sorts of statistics on your LAN, including all Internet connections.
For real fun, try “netstat -r” to see the kernel routing table. However,
be careful. I was teaching a friend the basics of summing up a Unix
system and I told her to do that and ‘ifconfig’. She was booted off the
system the next day for ‘hacker suspicion’ even though both are legitimate
commands for users.”

4) whois
Get lots of information on Internet hosts outside you LAN.

5) nslookup
Get a whole bunch more information on other Internet hosts.

6) dig
Even more info on other Internet hosts. Nslookup and dig are not
redundant. Try to get a shell account that lets you use both.

7) finger
Not only can you use finger inside your LAN. It will sometimes get you
valuable information about users on other Internet hosts.

8) ping
Find out if a distant computer is alive and run diagnostic tests -- or
just plain be a meanie and clobber people with pings. (I strongly advise
*against* using ping to annoy or harm others.)

9) traceroute
Kind of like ping with attitude. Maps Internet connections, reveals
routers and boxes running firewalls.

10) ftp
Use it to upload and download files to and from other computers.

Once you get your shell account, you will probably want to supplement the
“man” command with a good Unix book .


Listing Directories

If you are using bash - the Bourne Again Shell - you can get a listing with dir, or ls (with any shell). The default is to list files in alphabetical order (capitals and numbers first)

Some of the most useful options with ls are:

-a - lists all files, including hidden files; -A - lists all files, except the current and parent directory; -c - sorts file by time (oldest first); -d - lists only the name of a directory, not its contents; -l - lists in long format (showing permissions and other details); -r - lists in reverse order; -t - sorts files by time (newest first); -x - lists all files across the page instead of in columns.

Reading from the far right you have the links, filename, date and time the file was last modified, the file size (in bytes), the name of the group and owner. The number to the immediate left of the owner is the number of links to the file. (Links will be discussed below, so for the time being think of it as a way for one copy of a file to appear to be in several locations.) The long group of letters and hyphens on the left are the permissions.

One of the first things you should learn is about navigating through the directories. As in DOS, the current directory is represented by a single dot (.); the parent directory is represented by double dots (..). The command for change directory is cd so let's go to a more interesting directory such as /etc for some examples.

cd /etc ls -l

This directory is mostly system administration files, but run ls -l and one of the first things you'll notice is that the listing is very long. (To page back through the listing use Shift+PageUp; to page down use Shift+PageDown.) Near the top of the listing is X11 with a hyphen and greater than symbol (->; sort of like an arrow) and /var/X11R6/lib/ indicating a link to that location; also notice that the permissions begin with an l, while most begin with a hyphen (-) indicating a file, and some permissions begin with a d indicating a directory.

cd /bin ls -l

All the files here are binaries and end with an asterisk (*).

cd ~

ls -a

There are hidden files beginning with a dot (.); usually your configuration files and files derived from your configuration, e.g. .bash.history

Permissions

For security reasons, all Unix systems including Linux have file permissions which allow you to control access to directories - who can read, write, or execute a file or command in a directory.

In the extreme left is either a d or hyphen (-) indicating whether this is a directory or a file (occasionally you will also see an l indicating a link). Then you see three groups of the same three letters in the same order: r for read, w for write, x for execute, and the hyphen (-) for no permission given in that type. The first group of three letters is for the owner, the second group for the group, and the third the world. Whoever creates the file is the owner, and if more than one person is working on a project or needs access to this file they are given permission as a group, and finally how the file is open to anyone who has access to the system (the world).

chmod, chown, chgrp

The command to change file permissions is chmod (change mode). There are two ways for doing this: the numeric system and the symbolic system.

The numeric system uses numbers to track permissions. Using the table below you add together the numeric equivalent for the permissions you want.

400 - owner has read permission 200 - owner has write permission 100 - owner has execute permission

040 - group has read permission 020 - group has write permission 010 - group has execute permission

004 - world has read permission 002 - world has write permission 001 - world has execute permission

Thus chmod 764 SomeFile gives the owner permission to read, write, and execute SomeFile; the group has permission to read and write; the world permission to read only.

The other method for changing modes is the symbolic method. With this method, you have to know the existing permissions because the commands are added or removed relative to how permissions are currently set. The plus sign (+) adds a permission, the minus sign (-) removes a permission.

u - user (owner) g - group o - other (world) a - everyone - user, group, and other

r - read permission w - write permission x - execute permission

t - sticky bit

Thus chmod g+x SomeFile gives permission to the group to execute SomeFile.

In the past Unix crackers used to get around the permissions by messing around with entire directories. The way to prevent this is to set restrictive permissions for the directory using the sticky bit, which makes the directory accessible only to the owner and root without affecting how the individual file permissions are set.

chmod -t TheDirectory

You can also change the owner with the chown command, and change the group with chgrp.

Linking Files

Rather than having multiple copies of a file, Linux uses linking to one file to save disk space and administrative headaches trying to keep multiple copies up to date and synchronized. Linux supports two types of links, hard links and symbolic links.

Hard links are set with the command

ln FileName /NewDirectoryLocation

The problem with hard links is that Linux treats all hard links equally, and before you can delete the original file, you have to remove all hard links.

On the other hand, symbolic links don't need to be physically removed in order to delete the file. (There are some other differences between hard links and symbolic links, but irrelevant to this discussion; consult the man pages.)

Wildcards

Linux has three types of wildcards - the question mark (?), which is used to match a single character, the same as in DOS; the asterisk (*), which is much more expansive than anything in DOS because it can be used to return any number of letters at the beginning or end of an expression; and the final wildcard used to return specific characters as defined within brackets ([ ]).

Processes

A shell acts as the intermediary between the user and the operating system, interpreting your commands into a form the operating system can understand. The shell has the capacity to run multiple commands at one time, and can run commands in the background using the ampersand (&) after the command.

Multiple requests to the shell are called processes. As these requests are made, beginning with init during boot, the shell numbers them. These numbers are important if you want to stop a process: use the command ps to to see a list of current processes, then the command kill and the number of the process you want to stop.

No comments: