Unix - UTRGV Faculty Web

Download Report

Transcript Unix - UTRGV Faculty Web

The UNIX OS
Peter J. Vasquez Sr.
CSCI 6303.01
The UNIX OS

What is UNIX?

OpenBSD Installation

Introduction to VI

OpenBSD Configuration

Introduction to ksh

ksh Scripting
What is UNIX?

Design

Architecture

Filesystems

Brief History
UNIX Design

Modular Programming


software design technique that increases the extent to
which software is composed from separate parts, called
modules. http://en.wikipedia.org/wiki/Modularity_(programming)
Re-Usable Code

the likelihood a segment of source code can be used
again to add new functionalities with slight or no
modification.
http://en.wikipedia.org/wiki/Reusability
UNIX Architecture


Kernel – manages the computer's resources
and controls their use by programs.
Consists of:



CPU – responsible for running or executing
programs. The kernel decides which processes are
allocated to the processor.
Memory – used to store instructions and data. The
kernel decides what memory is available to a
process, and whether nor not enough is available.
I/O – input and output devices (keyboards, mice,
disk drives, etc). The kernel allocates requests to
these devices from applications to perform I/O.
UNIX Architecture (cont.)


Shell - user interface to the kernel, where
commands are input (whether on the command
line interface or in a GUI)
Shell Examples:

Bourne Shell (and variants)

C Shell (and variants)
Blackbox, fluxbox, fvwm, GNOME, KDE, XFCE,
CDE

UNIX Architecture (cont.)

System Processes (Daemons) – programs that
assist the kernel with specific system tasks.



Refers to background processes
Handles tasks in the background that the user
cannot be bothered with
Most daemons are child processes of another
running process (usually the init process, which is
the initialization shell script responsible for starting
up most processes after the kernel has loaded)
UNIX Architecture (cont.)

Parent/Child Processes

Relationship of running process to other running
processes. If one process initiates a new process,
it is called the parent, while the new process is
known as a child

New processes are spawned through a procedure
called 'fork', which creates a copy of the parent
along with the needed memory space and
parameters to run with a task/set of instructions

Common example is the execution of shell
commands, which remain interactive while child
processes can be sent to the background to run
UNIX Filesystems

UFS – UNIX File System (4.2/4.4BSD)



a few block at the beginning reserved for boot
blocks
a superblock containing a magic number identifying
as UFS filesystem
a collection of cylinder groups, each containing the
following:




backup copy of the superblock
cylinder group header with statistics
number of inodes
number of data blocks
UNIX Filesystems (cont)

<swap> 


form of virtual memory used to extend available
main memory
dedicated partition, which should be at least the
size of main memory
not required, but highly recommended
UNIX Brief History
A Brief Overview of OpenBSD



UNICS -> UNIX TSS -> BSD -> NetBSD -> OpenBSD
Designed with Security and Simplicity as primary goals
(only 2 remote holes in the default install in the past 10
years)
Major contributions to other UNIX/Linux Operating
Systems include


OpenSSH : free implementation of SSH (secure shell)
which provides for encrypted remote administration of
UNIX/Linux systems (85% of installed systems worldwide)
pf : or packet filter (firewall) has been used in a wide
range of embedded systems (for example, the syswall
security appliance)
OpenBSD Installation


Obtain CD (can be purchased at openbsd.org,
or ISO download from ftp.openbsd.org)
Boot up using correct version (i386 in this
example)

Set up partition sizes, choose filesystem type

Set up basic system and package configuration
OpenBSD Installation (cont.)

Boot from CD
OpenBSD Installation (cont.)

Press 'i' for install
OpenBSD Installation (cont.)

Press <enter>
OpenBSD Installation (cont.)

Press <enter>
OpenBSD Installation (cont.)

Press <enter>
OpenBSD Installation (cont.)

Type 'yes' to proceed with install
OpenBSD Installation (cont.)

Press <enter>
OpenBSD Installation (cont.)

Press <enter> for default disk, or specify disk
OpenBSD Installation (cont.)

Type 'yes' to use the entire disk for OpenBSD
OpenBSD Installation (cont.)

Press <enter>
OpenBSD Installation (cont.)

Now, type '?' for help, then press <enter>
OpenBSD Installation (cont.)

Now type 'p' to see current partition map
OpenBSD Installation (cont.)

Press <enter>
OpenBSD Installation (cont.)

Create (/) root, <swap>, /tmp and /var partitions
OpenBSD Installation (cont.)

Create /usr and /home partitions
OpenBSD Installation (cont.)

Type 'p' and press <enter> to see partitions
OpenBSD Installation (cont.)

Type 'w', <enter>, 'q', <enter>, to write and exit
OpenBSD Installation (cont.)

Now begin basic system, package configuration
OpenBSD Installation (cont.)

Type hostname, networking info, press <enter>
OpenBSD Installation (cont.)

Type in root password, press <enter> for CD
OpenBSD Installation (cont.)

Type in 'all' to select all packages
OpenBSD Installation (cont.)

Press <enter>, type 'done', and press <enter>
OpenBSD Installation (cont.)

Packages begin unpacking/installing to disk
OpenBSD Installation (cont.)

Type timezone 'US/Central', type 'halt' to reboot
OpenBSD Installation (cont.)

Press <enter>, the system will now reboot
Introduction to VI




Two modes of operation

Insert mode : type in a character to modify the
contents under the cursor directly

Command mode : type in a “:” followed by the
command string for more advanced options
We can create files, modify their contents, and
write the changes back to the disk
We can search within files for common strings
Useful in modification of the OS configuration
files
Introduction to VI (cont.)

Login as new user, list contents of directory
Introduction to VI (cont.)

Type 'vi sample' for a new file named 'sample'
Introduction to VI (cont.)

Type 'I' for insert mode, type, press <esc> ': wq'
Introduction to VI (cont.)

We can open the same file to make changes
Introduction to VI (cont.)

Type 'yy' and then 'p' to copy and paste the line
Introduction to VI (cont.)

You can type 'dd' to delete the current line
Introduction to VI (cont.)

We can search/replace a word, ':%s/new/old/g'
Introduction to VI (cont.)

Now our file reads the following:
Introduction to VI (cont.)

We can undo the change by typing 'u'
Introduction to VI (cont.)


A lot of changes are available, for a complete
reference you can type in 'vi reference' into a
search browser, which will provide many results
The most common uses of vi are for the
modification of system configuration files, and in
shell programming (scripting)
OpenBSD Configuration

Default configuration is listed in /etc/rc.conf
OpenBSD Configuration (cont.)

/etc/rc.local for startup change (not /etc/rc.conf)
OpenBSD Configuration (cont.)

For the GUI, configuration is in /etc/xorg.conf
OpenBSD Configuration (cont.)




Login as root user
Type 'man afterboot' on first boot to see list of
configurable options (new admin's guide)
Run 'adduser' script to setup defaults
Create User (best practice not to use root for
everything)
OpenBSD Configuration (cont.)

Login as 'root' and type the password
OpenBSD Configuration (cont.)

Press <enter> for default terminal type
OpenBSD Configuration (cont.)

Type 'man afterboot' for admin guide (overview)
OpenBSD Configuration (cont.)

Type 'adduser' and setup defaults for system
OpenBSD Configuration (cont.)

Create User
OpenBSD Configuration (cont.)



Now that we have a local user, we will exit the
session as 'root' and re-login as the new user to
continue configuration process
Configure networking parameters (done at
installation, but can be changed after boot)
Configure GUI for X Windows
OpenBSD Configuration (cont.)

Login, type 'su', root password, press <enter>
OpenBSD Configuration (cont.)

Type 'ifconfig -a' , and vi '/etc/hostname.vic0'
OpenBSD Configuration (cont.)

The network configuration is set to DHCP
OpenBSD Configuration (cont.)

We can setup the GUI with 'X -configure'
Introduction to ksh (K Shell)




A shell is primarily used to interpret commands
into instructions for the operating system
When we first log in, the command line we are
presented uses the korn shell (ksh)
Behavior of ksh is defined through shell
variables
The following screens will show navigation
through commands is ksh to create a directory,
list it's contents, and either dump the sample file
to the screen, or move it into our new directory
Introduction to ksh (cont.)

Some sample shell variables
Introduction to ksh (cont.)

We can also issue 'cat sample' to show our file
Introduction to ksh (cont.)

We can list the contents of our directory, 'ls -al'
Introduction to ksh (cont.)

And make a new directory, 'mkdir temp'
Introduction to ksh (cont.)

Change directory with 'cd temp', list with 'ls -al'
Introduction to ksh (cont.)

Now we can move our file, 'mv ../sample .'
Introduction to ksh (cont.)

And show the directory contains our file, 'ls -al'
Introduction to ksh (cont.)

We can see the contents again with 'cat sample'
ksh Scripting


Combining the previous slides, we can begin to
see how commands can be chained together
These chains of commands can be used to
form shell scripts for common or routine tasks of
the operating system
ksh Scripting Defined



At the beginning of this file, we need to include
!#/usr/bin/ksh
File starts on first line and ends at last line or
when encountering the word 'exit'
There are four types of lines

the shell defining line

empty lines

commentary (begin with a '#')

commands
ksh Scripting Defined (cont)



Script files should be change to permissions
“chmod 700” (rwx – user, no permissions for
group or world)
Commands start at the first word and end with
the end of the line, or with a ';'
Examples:

print -n “Name: “; read name; print “”

grep filename | sort -u awk '{print $4}' | \
uniq -c >> /longpath/file
ksh Variables

Example ksh variables
ksh Arrays

Example ksh arrays
ksh Branching (Conditional
Statements)

Example Branching
ksh Branching (Conditional
Statements) plus Case Statement

Example Branching/Case Statements
ksh Looping

Example Loop Statements
ksh Looping plus continue,break

Example Loop Statements (cont)
ksh Command Line Arguments
(Positional Parameters)

Example Command Line Arguments
ksh Comparisons

Example Use of Comparisons
ksh Variable Manipulation

Example variable manipulation
ksh Regular Expressions

Use * for matching any string (for example, to
get all files end in .c, use *.c)

?(pattern) matches zero or one times

*(pattern) matches any time the pattern

+(pattern) matches one or more times

@(pattern) matches one time the pattern

!(pattern) matches string without the pattern
ksh Functions


Making a Function

function foo { #commands }

foo() { #commands }
Calling the Function


foo arg1 arg2 ... (arguments $1...$n or $*)
Return

exits the function immediately with value specified
after return as exit status (for example, 'return 0')
ksh Data Redirection

Done with “>” “>>” “<” “<<”

Command Output to File



To write to a new file: 'command > file'

To Append to an existing file: 'command >> file'
Standard Error Redirection

To redirect the error output: 'command 2> file'

Discard the error: 'command 2>/dev/null'
File into Command

replaces standard input with file: 'command < file'
ksh Data Redirection (cont)


Combine Input and Output Redirection

'command < infile > outfile'

'command < infile > outfile 2>/dev/null'
Commands into Program

'command <<EOF
input1
input2
input3
EOF'
ksh Pipes and Co-processes


To send output of one command to another

' command1 | command2 | command3 '

for example “last | awk '{print $1}' | sort -u”
Background Processes

command |&

for example, ksh in background 'ksh |&'
ksh Input from User and Files

Read in a Variable


print -n “Enter your favorite color: “;read var; print””
Read into a File Line for Line

get each line of a file into a variable


{ while read myline; do
# process $myline
done } < filename
catch output

last | sort | {
while read myline;do
#commands
done }
ksh Special Variables

$# - Number of arguments on command line

$? - Exit status of last command

$$ - Process id of current program

$! - Process id of last background job

$0 – Program name including path

$1..$n – Command line arguments, one by one

$* - All command line arguments in one string
ksh Sample Script

Create new file with vi, add shell to top
ksh Sample Script (cont)

Keep looping until our condition is met
ksh Sample Script (cont)

Now we can ask the user for some information
ksh Sample Script (cont)

If we have the right person, verify password
ksh Sample Script (cont)

Add condition to limit number of guesses
ksh Sample Script (cont)

If the user login is successful, run command
ksh Sample Script (cont)

Now we make sure script is executable
ksh Sample Script (cont)

We can run the script now with './sample'
ksh Sample Script (cont)

If the name doesn't match, ask again
ksh Sample Script (cont)

Now the name matches, so ask the password
ksh Sample Script (cont)

Password doesn't match, so ask again
ksh Sample Script (cont)

Password matches, run authorized command
Any Questions?