Basic Unix i

Embed Size (px)

Citation preview

Basic UNIXWelcome to Basic Unix (Part 1) brought to you by MYEC & PDC DA

SyllabusPart 1 What is UNIX? Your first UNIX Session The UNIX File System Data Streams More Commands The Shells UNIX Security Policy vi Editor Part 2 Multi-Tasking System Management Devices E-mail Networking X-Windows NetBatch & CSD Revision Control

What is UNIX?

UNIX is an operating system that is:Interactive (not batch) Multiprocess Threaded (multi-processor) Multi-user Multi-purpose Open source Secure Networked Portable (Linux Laptop)

Today UNIX interfaces with Windows and shares files through software like Samba, VNC, & Exceed.

The KernelKernel Drivers

Hardware

The heart of any operating system is the hardware.

The Shell, File Systems, UtilitiesThe Shell -command interpreter

File System

Kernel

Network

Utilities

Major UNIX concepts

Hierarchical file system Data Streams: STDIN, STDOUT, STDERR, pipes, redirection, filter Shell sh, csh, Security login, passwd, permissions, groups Built-in mail system Network File System (NFS) Network Information System (NIS/YP) Domain Name System (DNS) X-windows

Quiz

What is UNIX ? What does UNIX use to control the hardware ?

Your first UNIX Session

Logging In

Login into a workstation, computer server, via: telnet, rlogin, rsh, ssh Enter username & password at login prompt Username will be visible Password will NOT be visible

Login Examples% % % % % telnet pgli0003 rlogin pglw8019 rsh pghc0002 rsh insite.fm.intel.com -l ewee ssh insite.fm.intel.com

Remote Access

rlogin l kyeoh3 pglc8010

Establishes a remote connection from your terminal to a remote machine

rsh pglc8010 touch file1

Runs a shell-level non-interactive command on another computer

ssh pglc8010

A substitute for rsh is called ssh; which encrypts all information between the SSH client and server

xhost +

Allows GUI display from remote UNIX systems

setenv DISPLAY pglw8010.png:2

Bring up X Windows

Most UNIX workstations are not with GUI Use runx command to bring up the X windows Windows managers: fvwm2, fvwm95, twm, Default windows manager MYEC provides is fvwm2 fvwm2 environment startup file is $HOME/.fvwm2rc

Change password

Change your password at your first login after you received your account though mail or voicemail. If you havent, do it NOW. Make your UNIX password strong with 6 to 8 characters which combine with alpha-numeric + special characters ($ % ^ @ # ; : > who

| wc -l

who

STDIN STDOUT STDERR

STDIN STDOUT STDERR

wc -l

Redirection with the concept of pipe, you can redirect your data output to a file or devicewho who > file1 ls l file1 cat file1 wc l < file1 date >> file1

% % % % % %

You can redirect error to a file

% wc file1 >& err.file % cat err.file

Quiz

What are those 3 data streams in standard shell ? How do you get the data into a file ? How do you link a commands output to another commands input ?

FiltersBeside pipes, redirections, you can modify your data output with filters

grep sed awk sort

grep

% ps ef | grep sh % ps ef | grep

grep flags

dont want this (filter out) -i neglect lower/upper case -n give line numbers -c count lines-v

sed

sed is Single-line EDitor, it processes data line-by-

line

% cat /etc/passwd | sed s/bin/BIN/ % cat /etc/passwd | sed n 1,2s/bash/BASH/ % cat /etc/passwd | sed n 1,2s/bash/BASH/p

sed flags-n

silent mode

awk

awk is a pattern matching for filtering data

% cat /etc/passwd | grep root % cat /etc/passwd | grep root | awk F: {print $6} % cat /etc/passwd | awk F: {print $1}

awk flags -F field delimiter

sort

Sometimes you will need to sort your data outputsort flags

% cat /etc/passwd | awk F: {print $1} | sort

-d dictionary order-n numeric -r reverse

-u unique (no repeat occurance)

Quiz

What do these commands do: grep, awk, sort ?

Lab

Run the command ypcat hosts | grep pglw and put the sorted second column of the output into a file: linux.ws List all the users logged in and sort them into a file, and display number of users logged in at the last line of the file

More Commands

Some Productive Hints

man, date, hostname, w, whoami, history, !

Wild cards

* ? []

any character(s) one, and only one character a range of characters [0-9] [A-z]

Comparing Files

cmp diff sdiff tkdiff

Hard & Soft Links

ln Hard link Creates a new item, newlink which changes with oldfile rm resistant, non-nfs friendly, non-directory friendly ln s Soft (symbolic) link Creates a new pointer (ala Windows shortcut)

Compressing Files

.gz

gzip / gunzip

Smallest size!

.Z

compress / uncompress

.zip

zip / unzip

tar

tar Tape Archive Restore

Example: Copying file% tar cf

Extract files% tar xvf

View contents of .tar file% tar tvf

Process Table

/usr/bin/ps -wef /usr/ucb/ps auwx kill -9

ps flags

ps ef

ps ax

System V, AIX, HPUX, Linux BSD, SunOS

Lab 4

cd to your home directory

Redirect man page of true to a file true.f Redirect man page of test to test.f Make a directory called lab cd to lab touch 3 files: a, b, c. List them all with l Remove a. List them again with l Remove lab

The Shells

The Shell

UNIX shells are command interpreter and a programming language, usually used by admin for automated operations There are more than one shell in UNIX world sh borne shell, most machines roots shell csh C shell, a favorite for most users, ECs default shell tcsh an enhanced C shell .cshrc your C shell environment file .login your C shell login setting file .aliases this is where you customize your shortcut keys

Shell Environment

env

Common Environment variables:

TERM terminal types HOME home directory PATH - your search path for commands PROMPT your system prompt TZ time zone EDITOR default editor DISPLAY the display location of X UID your user ID GID your group ID

Set Values to Variables

In csh / tcsh:% % % % % % % set hi = Hello set there = world echo $hi $there set unset hi there echo $hi $there setenv DISPLAY `hostname`:0.0

Quiz

How to see your environmental variable? Where is $HOME first set? What kind of variables are these:

HOME, PATH, TERM

Lab

Set a, b, c & d to 1-4 Display their values Unset them

Shell Quotes

used to preserve white space characters string ` ` runs command within the shell

Useful Shell Prompt

Put the following alias into your ~/.aliases, change the prompt if necessary:

alias cd 'cd \!*; set prompt = "%m:`pwd` %# "'

To use it:

source ~/.aliases

Simple Shell Script example#!/bin/sh a=3 b=4 c=`expr $a + $b` echo $a + $b = $c

Looping

whilewhile 1 $ date $ sleep 1 $ end

foreachforeach x ( a b c) $ echo $x $ end foreach x (`env`) $ echo $x $ sleep 1 $ end

n=1 while {$n -ne 10} do echo $n n=`expr n+1` done

Lab

Create a loop routine to count down 10, 91 Use your knowledge of common Unix commands and utilities (filter, pipe, etc) to create 2 files: linux.ws.alive, linux.ws.deadlinux.ws.alive - all Linux workstation alive using ping command linux.ws.dead all linux workstation not responding using ping command

More on tcsh

tcsh being one of the more popular shells FeaturesSimple command cycling/editing Filename/Command completion

tcsh Shortcut Summary

Command History in tcsh

!!

Repeats previous command. !!:p Prints out the previous command. !-2 Repeats second-to-previous command. !-2:p Prints second-to-previous command. !23 Repeats command 23 as specified in the history list. !cat Repeats the most recent command that started with cat. !?cat Repeats the most recent command that had cat in it. !$ use the last word from the previous command and substitute it for this command !* use all words except the first one from the previous command and substitute them for the command.

history Lists out the previous commands in a format like: 86 9:26 cd admin 87 9:26 ls status/ 88 9:26 cd .. 89 9:26 ls tmp/

Quiz

1) 2)

3)4)

You have been entering a bunch of commands, and several commands earlier was a very long one that you need to enter again. Name at least four ways to enter this command: Type it in completely

Answers1) 2) history, then ! 3) ! 4) arrow-keys

UNIX Security Policy

File/Directory Access Permission

drwxrwxrwxTypeOwner Group Others

Types: file d directory l link b binary

Access Permission: r readable w writable x executable reverse of rwx

Examples

drwxrwxrwx World read/writeable directory -rwxrwxrwx World read/writeable file -r-xr-xr-x Executable only

-rwxr-xr-x Executable, owner writeable-rwxrwx--- Executable, owner & group

writeable, others unreadable -rwxr-x--- Quiz

Modify File/Directory Permission

chmod u+?-?,g+?-?,o+?-? file1 chmod ### file1 Example:

chmod u+rwx,g+rx-w,o+rx-w file1 chmod 755 file1

File/Directory Ownership

chown chgrp chown ##:## (productivity of csh) groups gid

Creation mask setting

The umask command sets or displays the creation mask setting which defines default attributes for new files Settings are the complement of chmod

umask 022 -rw-r- - r - umask 027 -rw-r- - - - -

Sticky bit

sticky bit only works on directories It is to make sure all files inside the directory having same GID

drwxrwsrwx 5 sys sys 543 May 9 09:41 etc

Lab

touch a new empty file: file2

Check its default permission and ownership Change its permission to rwxr-x--Change its GID to PDC Change its ownership to root. Can you?

General UNIX Security Guideline

Login access

rule-based password protection passwords have 3-month shelf life Protect .rhosts with 0600

Network access

File/Directory protection Command search path - $PATH Keyboard/Screen access permission

xlock

PDC Security Guideline

UNIX security has to comply with Intels information security policies & guidelines. Non-compliance of UNIX security will put Intel in danger of losing Intellectual Property.

PDC UNIX Security Guideline (cont)

File permission guidelines

First level, external users cannot access Intel file systems. Second level, you can protect your files from other Intel users. For example, no one else should be able to read your mail since mail is private. Project databases and some other material are sensitive and are accessible only to some users. Keep your home directory non-group writable. No one should be allowed to write in your area. When working in project areas, make your files group writable if they are shared.

vi Editor

vi

UNIX users favorite editor - a headache to Windows users You can use for a text file:

Viewing Inserting Appending Deleting Moving Copying Going to shell from within vi

Modes within vi

command mode

h left, j down, k up, l right x erase character dd delete line, 10dd yy yank or copy, y1 p - paste u - undo . repeat the last vi command (favorite productivity) 0 move to beginning of line

cw change a word r replace a character / - initiate a search n search next pattern J merge lines G go to bottom of lines :# - go to line # :r read a file from shell :set nu, :set nonu set/unset line # ^F page down (forward) ^B page up (backward) :wq, :wq! - write & quit

input mode, to exit

i insert, I insert at the begin of line a append, A append at the end of line o open a new line, or append at the next line

Lab