Sei sulla pagina 1di 45

Unix : An Introduction

Exclusively for VTU vivek.joshi@sun.com ganesh.hiregoudar@sun.com

Agenda
Unix : A Look & Feel System Information User management Archiving tools Automated tasks File system commands Security Important files & directories Miscellaneous

Welcome to Unix world


Features
> > > > >

Multi-user Multi-tasking Hardware independent OS (written in C language) Rich set of tools and scripts Secure

Flavours
> Solaris > FreeBSD > Linux

Look & Feel

Applications

Some more Applications

Lets play the movie

Got to process Images?

First thing first : Unix Docs


Online reference manual
> Commonly referred as unix manual or man pages

Manual pages are divided into different sections


> Sections for commands, system calls, header files etc

Refer to man pages to get more info about a command or an API or a system call or a header file
> Use man command to view the manual pages, e.g. to learn more about ls command, run

/usr/bin/man ls

> More info : /usr/bin/man man


man [-s section] word man -k keyword

Commands are case sensitive. Ls is different from ls.

System Information
What's my system (machine) name? What's the operating system version installed on it? Who all are logged into my machine and what are they doing? What processes they are running? Let us chat ! What packages are installed on the machine?

uname : prints name and version of the current system


On an x86/AMD64 machine
#uname -a
SunOS vha-v40zb-blr03 5.10 Generic_117172-07 i86pc i386 i86pc

OS Name

Machine Name

Release

Version

Hardware Processor

Platform

On a SPARC machine
# uname -a

More info : man uname

SunOS land 5.10 Generic_118835-02 sun4u sparc SUNW,Sun-Blade-100

who : Who are logged into my machine?


$ who
ganesh jayakara surya vivek prakash
case sensitive, who is different from Who /WHO

pts/2 pts/1 pts/10 pts/3 pts/19

Feb 2 13:10 Jan 31 15:10 Jan 11 14:39 Feb 6 16:31 Jan 26 11:34

(goudar) (kaveri) (yanni) (land) (kailash)

vsakar

pts/15

Jan 13 10:40

(cheeta)

User Name

TTY ID

How long the user Machine from where the user has has logged into this machine. Logged in

whodo : who is doing what ?


$ /usr/sbin/whodo
Thu Feb 9 13:04:32 IST 2006 drosera

User name pts/1 pts/1 pts/1 pts/10 pts/10 pts/10 vijay 15:10 19756 0:00 bash 19990 0:03 ftp ramesh 14:39 4155 0:00 csh 4206 0:00 bash Process ID Process (Command)

ps : What are the processes running on the machine?


$ ps -ef | more
UID PID PPID root root root root 0 1 2 293 0 0 0 1 STIME Jan 09 Jan 09 Jan 09 Feb 08 Jan 09 Feb 01 TTY ? ? ? pts/11 ? pts/4 TIME 0:05 0:07 0:00 11:53 0:01 0:00 CMD sched /etc/init pageout realplay /usr/sbin/syslogd /bin/ksh -p mozilla

surya 12190 11067 vivek 26154 880

ps command has lots of options. Please refer to man pages to learn more about the command (man ps).

Lets chat !
Utilities : write, talk and wall write reads from standard input and sends the message to another user.
> Suppose a user ganesh wants to send a message to another user madhu, User ganesh writes meaage to madhu using write commad, $ write madhu <type you message followed by Enter> Hello <followed by an Enter> Ctrl-D User madhu will see this instant message ... Message from ganesh on land (pts/11) [ Sat Feb 18 12:44:47 ] ... Hello <EOT> > If madhu wants to reply, he can also do like $ write ganesh > One way communication Use talk for two way communication

Lets chat more!


talk
> Two way screen-oriented communication > Both user get on screen window to chat > A user an initiate a talk by running talk command as

> talk address It is the user-name or user-name@host

wall : writes to all users


> Writes to all logged in users. > The message is preceeded by Broadcast Message from ...

More info : Read the man pages


> man write > man talk > man wall

pkginfo : prints info about installed packages.


$ pkginfo
system system SUNWGtkr SUNWGtku GTK - The GIMP Toolkit (Root) GTK - The GIMP Toolkit (Usr) The Python interpreter, libraries and utilities a2ps - GNU Any to PostScript filter (root) a2ps - GNU Any to PostScript filter (user) System Accounting, (Root) System Accounting, (Usr) Apache Web Server V2 Documentation Apache Web Server V2 (root) Apache Web Server V2 (usr) GNOME2 SUNWPython system system system system
.......

SUNWa2psr SUNWa2psu SUNWaccr SUNWaccu SUNWapch2d SUNWapch2r SUNWapch2u

system system system

System Info : Summary


/usr/bin/uname : prints name/version of the system /usr/bin/pkginfo: prints software packages info /usr/bin/who : who is on the system ? /use/bin/whodo: who is doing what ? /usr/bin/ps : reports process status talk, write, wall : to talk or send messages More info : See the manual pages
> man ps (displays manual pages of ps command) > man uname (displays man pages of uname command) > man ls (displays man pages of ls command)

User management
Creating a new user (/usr/sbin/useradd) User deletion (/usr/sbin/userdel) Modifying User properties (/usr/sbin/usermod) > Home directory > Shell > Group User management files Root user

Creating a user
/usr/sbin/useradd -g staff -d /var/tmp/test -s /usr/bin/bash -c "test user" test
Group Home directory Default shell User Description User name

Home directory : A directory where the user can keep his or her files. Files v/s Directories : A file can be considered as a program, text, data,
executable ... whereas a directory is a holder that contains the files and/or directories. It's equivalent to folder in Windows environment.

File/directory permissions : Specifies who can read or write to the file. Shell : It's a command interpreter that reads a user provided command from a
terminal or a file and executes it. Some common shells bash, ksh and sh.

You can see the permission with /usr/bin/ls command. More on the permission is on next slide.

File/Directory permissions
$ ls -l
-rw-r--r--r-xr-xr-x -rw-------rwxrwxrwx drwxr-xr-x 1 test 1 test 1 test 1 test 2 test staff staff staff staff staff 1149 Feb 234269 Feb 230150 Feb 7 13:04 intro.txt 8 12:51 vtu.sdw 9 19:26 vtuba.sxi 2006 world-writable 2006 mydir

0 Feb 10 512 Feb 10

d rwx rwx rwx


d : Directory - : Regular File l : Link r w x : Read : Write : Access

User(u) Group(g) Others(o)


drwxr-xr-x means it's a directory and can be read/written/accessed by the user (owner). It can be read/accessed by group or others but can't be written/modified by them. -rw------- means it's read/modified by the user (owner) but can't be read/modifed by group or others.

Examples
$ id uid=101 (vivek) gid=10(staff) $ ls -l file1 -rw-r--r-- 1 vivek staff 0 Feb 14 2006 file1

Notes :
Commands chmod, chgrp and chown can be used to change permissions, group and ownership. Three kind of permissions on files/dir > read (r), write (w), execute (x) Permissions can be given to > User/owner (u) > Group (g) > Others (o) a+rwx means rwx (all) permissions given to user, group and others. u+rwx,go=x means user can read, write, execute but groups and others can only execute! More info : man chmod Important from test / interview perspective

$ chmod u+rwx, go-rwx file1


All permissions to owner NO permissions to group and others

$ ls -l file1 -rwx------ 1 vivek $ chmod a+x file1


Execute permissions to ALL (user, group, others)

staff

0 Feb 14 2006 file1

$ls -l file1 -rwx--x--x 1 vivek staff 0 Feb 14 2006 file1

Deleting a user/Modifying user's info


Deleting a user
> /usr/sbin/userdel test

Modifying the user's properties


> Changing default shell > /usr/sbin/usermod -s /usr/bin/ksh test

User's information is stored in following files > /etc/passwd

Sample entry in /etc/passwd

test:x:200746:1:Test user:/var/tmp/test:/bin/bash Password UId GroupId Description Homedir shell

Username

>/etc/shadow

Contains encrypted password

Root user
A special user who has full access to all the resources (files,
directories, processes ...)
> Similar to 'Administrator' user in Windows environment.

How to login as root user if logged in as non-root user ? $ /usr/bin/id Current User Name Current User Group uid=100(ramesh) gid=10(staff) $ /usr/bin/su password:

Not visible

# /usr/bin/id uid=0(root) gid=1(other)


Logged in as root user now

User management : Summary


/usr/sbin/useradd : adds a new user /usr/sbin/userdel : deletes the specified user /usr/sbin/usermod : modifies user's properties /use/bin/chmod : changes perms for files/dirs /usr/bin/chown : changes file/dir ownership /usr/bin/chgrp : changes the group More info : Read the man pages
> > > >

/usr/bin/man useradd /usr/bin/man chmod man chown man su

Archiving tools
To create one single file archive that contains user specified files or directory structure To compress/uncompress the files Similar to winzip in Windows environment Available Tools
> tar

The tar command archives and extracts files to and from a single file called a tarfile Default entension of the files .Z (eg, myfile.Z) Adaptive Lempel-Ziv coding for compression zip is a compression and file packaging utility for Unix, MSDOS, Windows NT Default extension .zip Analogous to combination of tar and compress

> compress & uncompress


> zip & unzip


Archiving tools
> gzip & gunzip

Default extenstion .gz Lempel-Ziv coding (LZ77) for compression Default extension bz2 Burrows-Wheeler block sorting text compression algorithm, and Huffman coding Better compression as compared to LZ77/LZ78-based compressors

> bzip2 & bunzip2


Examples :
$ pwd /var/tmp/vivek $ ls -l -rw-r--r-1 vivek drwxr-xr-x 6 vivek -rw-r--r-1 vivek -rw-r--r-1 vivek -rwxr-xr-x 1 vivek Prints current working directory

staff 282135 staff 512 staff 1181779 staff 3984633 staff 164576

Mar Mar Mar Mar Mar

6 11:57 SecureProgramming.pdf 6 11:49 opensolaris-tools/ 6 11:45 vtuba.pdf 6 11:45 vtuba.sxi 6 11:56 wget-i386.bin

$ tar -cvf myfile.tar *

User specified files to be archived Tar ball (a single archive that hold all the file)

a SecureProgramming.pdf 276K a opensolaris-tools/ 0K a opensolaris-tools/onbld/ 0K ... a opensolaris-tools/netbeans/3.5V/netbeans.css 9K a vtuba.pdf 1155K a vtuba.sxi 3892K a wget-i386.bin 161K $ ls -l myfile.tar -rw-r--r-- 1 vivek staff 838136320 Mar 6 12:06 myfile.tar $ bzip2 myfile.tar $ $ ls -l myfile* -rw-r--r-- 1 vivek staff 274741732 Mar 6 12:06 myfile.tar.bz2
Zipped file

$ ls -l myfile* -rw-r--r-- 1 vivek staff 274741732 Mar 6 12:06 myfile.tar.bz2 $ bunzip2 myfile.tar.bz2 $ $ ls -l myfile* Archive file retrieved (uncompressed) -rw-r--r-- 1 root root 838136320 Mar 6 12:06 myfile.tar $ $tar -xvf myfile.tar Extracting files from archive x SecureProgramming.pdf, 282135 bytes, 552 tape blocks x opensolaris-tools, 0 bytes, 0 tape blocks ... x opensolaris-tools/netbeans/3.5V/netbeans.css, 8918 bytes, 18 tape blocks x vtuba.pdf, 1181779 bytes, 2309 tape blocks x vtuba.sxi, 3984633 bytes, 7783 tape blocks x wget-i386.bin, 164576 bytes, 322 tape blocks $

More info : See the man pages for tar and bzip2

Automated tasks
/usr/bin/crontab
> Schedule system tasks to execute repetitively at regular

intervals. > Each user on the system can have a crontab file. > Crontab files are placed in /var/spool/cron/crontabs

To view crontab file for user 'admin'


> $ /usr/bin/crontab -l admin

10 3 * * 0,4 /lib/newsyslog 12*** /usr/bin/ls > /dev/null


Time for the command execution Command

More info :
> See the man pages (man crontab)

Automated tasks

File system commands


/usr/sbin/mount
>
It attaches a file system to the file system hierarchy at the specified mount_point. Also, prints the currently mounted file systems.

/usr/sbin/umount
> It unmounts a currently mounted file system.

/usr/bin/du
> It summarizes disk usage

/usr/bin/df
> It displays number of free disk blocks and free files

/usr/sbin/quota
> It display a user's ufs file system disk quota & usage.

More info : See the man pages (eg, man du)

Examples
du
> Disk Usage of directory SUNWrtvc
$ du -k SUNWrtvc 376 SUNWrtvc/bin 10 SUNWrtvc/examples/rtvc_capture_movie 24 SUNWrtvc/examples/rtvc_display 68 SUNWrtvc/examples/rtvc_video_conference ... 7 SUNWrtvc/man/man1 19 SUNWrtvc/man/man3 27 SUNWrtvc/man 532 SUNWrtvc

> Summarizing usage


$ du -sk SUNWrtvc 532 SUNWrtvc

Examples
df
> Displays the amount of disk space occupied by file systems.
$ df -lk Filesystem /dev/dsk/c0t0d0s3 /dev/dsk/c0t0d0s7 kbytes 6049806 used 3504244 avail 2485064 capacity 59% 70% Mounted on / /oracle

22293505 15233138 6837432

mount
> Attaches a file system (FS) in FS hierarchy at a specified point. > It also lists mounted file systems. > umount unmounts mounted file systems.
$ ls /oracle (empty directory) $ $ mount -F ufs /dev/dsk/c0t0d0s7 /oracle $ ls /oracle All files or directories in disk c0t0d0s7 can be accessed

Security
Different levels of security
> Kernel level security check (check for uid 0, e.g. root) > File level security (File/directory permissions) > Tight Security policies

Safe and Virus free


> No unauthorized access > Kernel level security checks > Can't be bypassed as compared to Windows registry

Security features in Solaris


> Least privileges > Role Based Access Control (RBAC)

Important files/dirs
Directories
/usr/bin /usr/sbin /etc /var > /tmp > /usr/share/man
> > > >

: : : : : :

user level executables like ls system level commands like format contains configuration files contains run time configuration files contains temporary files which go away with reboot conventional path for manual page directories : contains users info : defaults for each file system : system logs : user access and administration info : info about all installed files/dirs/pkgs

Files
> > > > >

/etc/passwd and /etc/shadow /etc/vfstab /var/adm/messages /var/adm/utmpx /var/sadm/install/contents

More info : Man pages (eg, man passwd or man vfstab)

Miscellany : 1
PATH variable
> To populate shell search path > PATH=/usr/bin:/usr/sbin

Shell will look into /usr/bin/ first then in /usr/sbin when a command is executed. Examples : Suppose firefox browser is in /usr/sfw/bin directory, then shell will throw an error message, firefox : not found Set the PATH variable as : as Separator/Delimiter PATH=$PATH:/usr/sfw/bin

Old PATH Value Newly provided path New search path as /usr/bin:/usr/sbin:/usr/sfw/bin Shell will find the file firefox now and execute it.

Miscellany : 2
MANPATH variable
> Default search path for manual pages is /usr/share/man

Example
> Man pages for mysqladmin are not in default man path
$ man mysqladmin Error message saying no manual pages for mysqladmin No manual entry for mysqladmin.

> We need to populate MANPATH variable same as PATH


$ MANPATH=$MANPATH:/usr/sfw/share/man/ $ man mysqladmin

> See the man pages now

Any other path where mysql man pages installed

Miscellany : 3
Unix is case sensitive operating system. Most of the languages like C, C++ and Java are also case sensitive.
> An integer is different from Integer.

User test and Test are different. Command ls is different from Ls File creation
> Use editor at command line : vi or gedit or xemacs > In GUI: Launch-> Application-> Accessories->Text editor

Miscellany : 4
Solaris and other flavours of unix supports an extensive set of removable media and peripheral devices with no or very minimal configuration. Scanner Flash drive CD / DVD Unix system Jump drives Floppies Zip drives

Summary
Unix : A Look & Feel System and user related commands Case sensitive File and directory permissions Archives tools like tar, gzip File system commands like du, df Important files and directories Security aspects More info : man pages

Resources
The Unix Programming Environment
by Kernighan & Pike

Unix Manual (man pages) http://docs.sun.com http://www.sun.com/bigadmin/ http://www.opensolaris.org/ Unix Faqs : http://www.faqs.org/faqs/unix-faq/faq/ Post your queries at http://forum.sun.com/

Exercises
1) Try out the commands taught in this lecture on Solaris or any other Unix/Linux environment. 2) What's a shell ? 3) What's the significance of PATH variable? 4) A file has a permissions like -r-xr-xr-x owned by user gopal. Can the user gopal write to it, yes or no? & why ? 5) How to find the size of a file or a directory ? 6) How to find the time when a file test.txt was modified ? 7) How to see and change the file permissions ? 8) Try out following commands as well cat, head, tail, mkdir, mv, cp, rm, rmdir, wc, groups, newgrp, date, cal, touch Look at their man pages for details

What's Next ?

Shell Programming by Moinak Ghosh (The father of BeleniX)

Send your queries to vivek.joshi@sun.com ganesh.hiregoudar@sun.com

Potrebbero piacerti anche