You are on page 1of 9

LAB 2: BASIC LINUX COMMANDS AND SERVER ADMINISTRATION

THEORY:
LINUX USES COMMANDS SIMILAR TO THOSE USED IN UNIX.
LINUX DIRECTORY STRUCTURE




USER ADMINISTRATION
Command Example Description
cat

Sends file contents to standard output. This is a way to list the contents of short files to the
screen. It works well with piping.

cat .bashrc Sends the contents of the ".bashrc" file to the screen.
cd

Change directory

cd /home Change the current working directory to /home. The '/' indicates relative to root, and no matter
what directory you are in when you execute this command, the directory will be changed to
"/home".

cd httpd Change the current working directory to httpd, relative to the current location which is
"/home". The full path of the new working directory is "/home/httpd".

cd .. Move to the parent directory of the current directory. This command will make the current
working directory "/home.

cd ~ Move to the user's home directory which is "/home/username". The '~' indicates the users
home directory.
cp

Copy files

cp myfile yourfile Copy the files "myfile" to the file "yourfile" in the current working directory. This command
will create the file "yourfile" if it doesn't exist. It will normally overwrite it without warning if
it exists.

cp -i myfile yourfile With the "-i" option, if the file "yourfile" exists, you will be prompted before it is overwritten.

cp -i /data/myfile . Copy the file "/data/myfile" to the current working directory and name it "myfile". Prompt
before overwriting the file.

cp -dpr srcdir destdir Copy all files from the directory "srcdir" to the directory "destdir" preserving links (-p option),
file attributes (-p option), and copy recursively (-r option). With these options, a directory and
all it contents can be copied to another directory.
df

Show the amount of disk space used on each mounted filesystem.
less
less textfile
Similar to the more command, but the user can page up and down through the file. The
example displays the contents of textfile.
ln

Creates a symbolic link to a file.

ln -s test symlink Creates a symbolic link named symlink that points to the file test Typing "ls -i test symlink"
will show the two files are different with different inodes. Typing "ls -l test symlink" will
show that symlink points to the file test.
locate

A fast database driven file locator.

locate whereis Lists all files whose names contain the string "whereis".
logout

Logs the current user off the system.
ls

List files

ls List files in the current working directory except those starting with . and only show the file
name.

ls -al List all files in the current working directory in long listing format showing permissions,
ownership, size, and time and date stamp
more

Allows file contents or piped output to be sent to the screen one page at a time.

more /etc/profile Lists the contents of the "/etc/profile" file to the screen one page at a time.

ls -al |more Performs a directory listing of all files and pipes the output of the listing through more. If the
directory listing is longer than a page, it will be listed one page at a time.
mv

Move or rename files

mv -i myfile yourfile Move the file from "myfile" to "yourfile". This effectively changes the name of "myfile" to
"yourfile".

mv -i /data/myfile . Move the file from "myfile" from the directory "/data" to the current working directory.
pwd

Show the name of the current working directory

more /etc/profile Lists the contents of the "/etc/profile" file to the screen one page at a time.
shutdown

Shuts the system down.

shutdown -h now Shuts the system down to halt immediately.

shutdown -r now Shuts the system down immediately and the system reboots.
whereis

Show where the binary, source and manual page files are for a command

whereis ls Locates binaries and manual pages for the ls command.


uname uname a Displays System Information
who who q Display currently logged in users





LINUX USER ADMINISTRATION
chage (1) - Used to change the time the user's password will expire.
chown (1) - Change the owner of file(s ) to another user.
groupadd (8) - Create a new group.
groupdel (8) - Delete a group
groupmod (8) - Modify a group
groups (1) - print the groups a user is in
passwd (1) - Used to update a user's password. The command "passwd username" will set the password for
the given user.
su (1) - run a shell with substitute user and group IDs
useradd (8) - Create a new user or update default new user information
userdel (8) - Delete a user account and their files from the system. The command "userdel -r newuser" will
remove the user and deletes their home directory.
usermod (8) - Modify a user account

LINUX FILE PERMISSIONS
Linux files are setup so access to them is controlled. There are three types of access:
1. read
2. write
3. execute
Each file belongs to a specific user and group. Access to the files is controlled by user, group, and what is called other. The term,
other, is used to refer to someone who is not the user (owner) of the file, nor is the person a member of the group the file belongs
to. When talking about setting permissions for "other" users to use, it is commonly referred to as setting the world execute, read,
or write bit since anyone in the world will be able to perform the operation if the permission is set in the other category.


File names and permission characters
File names can be up to 256 characters long with "-", "_", and "." characters along with letters and numbers.
When a long file listing is done, there are 10 characters that are shown on the left that indicate type and permissions of the file.
File permissions are shown according to the following syntax example: drwerwerwe
There are a total of 10 characters in this example, as in all Linux files. The first character indicates the type of file, and the next
three indicate read, write, and execute permission for each of the three user types, user, group and other. Since there are three
types of permission for three users, there are a total of nine permission bits. The table below shows the syntax:
1 2 3 4 5 6 7 8 9 10
File User Permissions Group Permissions Other Permissions
Type Read Write Execute Read Write Execute Read Write Execute
d r w e r w e r w e

Character 1 is the type of file: - is ordinary file, d is directory, l is link.
Characters 2-4 show owner permissions. Character 2 indicates read permission, character 3 indicates write
permission, and character 4 indicates execute permission.
Characters 5-7 show group permissions. Character 5=read, 6=write, 7=execute
Characters 8-10 show permissions for all other users. Character 8=read, 9=write, 10=execute

[root@localhost home]# ls -l
total 12
drwx------. 2 admin admin 4096 Jun 25 10:43 admin
-rw-r--r--. 1 root root 33 Jun 25 22:50 hello.txt
drwx------. 2 mrtg mrtg 4096 Jun 24 21:43 mrtg


LINUX PROCESS CONTROL
ps ax
- Lists all running processes

top
- Lists all running processes arranging them in descending order with processes consuming more CPU and Memory at top

kill <process id>
- Kills a running process

killall | grep <process_name>
- Kills all process matching given process name

SCHEDULING TASKS WITH CRON
Cron is a task scheduler which executes specific tasks at specific points in
time. Cron runs by Crond daemon. Cron looks for files under /var/spool/cron
directory. It also looks for scripts under /etc/crontab and /etc/cron.d
directory.


#minute, hour, day of month, month, day of week, command
* * * * * command
Table 5-4: Entries in a crontab Command Line
Field Value
Minute 0-59
Hour Based on a 24-hour clock; for example, 23 =
11 p.m.
Day of month 1-31
Month 1-12, or jan, feb, mar, etc.
Day of week 0-7; where 0 and 7 are both Sunday; or sun,
mon, tue, etc.
Command The command you want to run
# Mail a message at 10:45 AM every Friday
45 10 * * fri mail -s "Project Update

# Run 15 minutes past Midnight every Saturday
15 0 * * sat $HOME/scripts/scary.script

LINUX VI
What is vi?
The default editor that comes with the UNIX operating system is called vi (visual editor). [Alternate
editors for UNIX environments include pico and emacs, a product of GNU.]
The UNIX vi editor is a full screen editor and has two modes of operation:
1. Command mode commands which cause action to be taken on the file, and
2. Insert mode in which entered text is inserted into the file.
In the command mode, every character typed is a command that does something to the text file being
edited; a character typed in the command mode may even cause the vi editor to enter the insert mode.
In the insert mode, every character typed is added to the text in the file; pressing the <Esc> (Escape) key
turns off the Insert mode.
While there are a number of vi commands, just a handful of these is usually sufficient for
beginning vi users. To assist such users, this Web page contains a sampling of basic vi commands. The
most basic and useful commands are marked with an asterisk (* or star) in the tables below. With
practice, these commands should become automatic.
NOTE: Both UNIX and vi are case-sensitive. Be sure not to use a capital letter in place of a lowercase
letter; the results will not be what you expect.

To Get Into and Out Of vi
To Start vi
To use vi on a file, type in vi filename. If the file named filename exists, then the first page (or
screen) of the file will be displayed; if the file does not exist, then an empty file and screen are created
into which you may enter text.

vi filename edit filename starting at line 1
vi -r filename recover filename that was being edited when system crashed
To Exit vi
Usually the new or modified file is saved when you leave vi. However, it is also possible to
quit vi without saving the file.
Note: The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is
completed by hitting the <Return> (or <Enter>) key.

:x<Return> quit vi, writing out modified file to file named in original invocation
:wq<Return> quit vi, writing out modified file to file named in original invocation
:q<Return> quit (or exit) vi

:q!<Return> quit vi even though latest changes have not been saved for this vi call

Moving the Cursor
Unlike many of the PC and MacIntosh editors, the mouse does not move the cursor within the vi editor
screen (or window). You must use the the key commands listed below. On some UNIX platforms, the
arrow keys may be used as well; however, since vi was designed with the Qwerty keyboard (containing
no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.
If you go back and forth between a PC environment and a UNIX environment, you may find that this
dissimilarity in methods for cursor movement is the most frustrating difference between the two.
In the table below, the symbol ^ before a letter means that the <Ctrl> key should be held down while
the letter key is pressed.

j or <Return>
[or down-arrow]
move cursor down one line

k [or up-arrow] move cursor up one line

h or <Backspace>
[or left-arrow]
move cursor left one character

l or <Space>
[or right-arrow]
move cursor right one character

0 (zero) move cursor to start of current line (the one with the cursor)

$ move cursor to end of current line
w move cursor to beginning of next word
b move cursor back to beginning of preceding word
:0<Return> or 1G move cursor to first line in file
:n<Return> or nG move cursor to line n
:$<Return> or G move cursor to last line in file

Screen Manipulation
The following commands allow the vi editor screen (or window) to move up or down several lines and to
be refreshed.
^f move forward one screen
^b move backward one screen
^d move down (forward) one half screen
^u move up (back) one half screen
^l redraws the screen
^r redraws the screen, removing deleted lines

Adding, Changing, and Deleting Text
Unlike PC editors, you cannot replace or delete text by highlighting it with the mouse. Instead use the
commands in the following tables.
Perhaps the most important command is the one that allows you to back up and undo your last action.
Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot
go back more than one step.

u UNDO WHATEVER YOU JUST DID; a simple toggle
The main purpose of an editor is to create, add, or modify text for a file.
Inserting or Adding Text
The following commands allow you to insert and add text. Each of these commands puts the vi editor
into insert mode; thus, the <Esc> key must be pressed to terminate the entry of text and to put
the vi editor back into command mode.

i insert text before cursor, until <Esc> hit
I insert text at beginning of current line, until <Esc> hit

a append text after cursor, until <Esc> hit
A append text to end of current line, until <Esc> hit

o open and put text in a new line below current line, until <Esc> hit

O open and put text in a new line above current line, until <Esc> hit
Changing Text
The following commands allow you to modify text.

r replace single character under cursor (no <Esc> needed)
R replace characters, starting with current cursor position, until <Esc> hit
cw
change the current word with new text,
starting with the character under cursor, until <Esc> hit
cNw
change N words beginning with character under cursor, until <Esc> hit;
e.g., c5w changes 5 words
C change (replace) the characters in the current line, until <Esc> hit
cc change (replace) the entire current line, stopping when <Esc> is hit
Ncc or cNc
change (replace) the next N lines, starting with the current line,
stopping when <Esc> is hit
Deleting Text
The following commands allow you to delete text.

x delete single character under cursor
Nx delete N characters, starting with character under cursor
dw delete the single word beginning with character under cursor
dNw
delete N words beginning with character under cursor;
e.g., d5w deletes 5 words
D delete the remainder of the line, starting with current cursor position

dd delete entire current line
Ndd or dNd
delete N lines, beginning with the current line;
e.g., 5dd deletes 5 lines
Cutting and Pasting Text
The following commands allow you to copy and paste text.
yy copy (yank, cut) the current line into the buffer
Nyy or yNy copy (yank, cut) the next N lines, including the current line, into the buffer
p put (paste) the line(s) in the buffer into the text after the current line

Other Commands
Searching Text
A common occurrence in text editing is to replace one word or phase by another. To locate instances of
particular sets of characters (or strings), use the following commands.
/string search forward for occurrence of string in text
?string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction
Determining Line Numbers
Being able to determine the line number of the current line or the total number of lines in the file being
edited is sometimes useful.
:.= returns line number of current line at bottom of screen
:= returns the total number of lines at bottom of screen
^g
provides the current line number, along with the total number of lines,
in the file at the bottom of the screen

Saving and Reading Files
These commands permit you to input and output files other than the named file with which you are currently
working.
:r filename<Return>
read file named filename and insert after current line
(the line with cursor)
:w<Return> write current contents to file named in original vi call
:w newfile<Return> write current contents to a new file named newfile
:12,35w smallfile<Return>
write the contents of the lines numbered 12 through 35 to a new file
named smallfile
:w! prevfile<Return> write current contents over a pre-existing file named prevfile

You might also like