Unix: An: Exclusively For VTU
Unix: An: Exclusively For VTU
Agenda
Unix : A Look & Feel System Information User management Archiving tools Automated tasks File system commands Security Important files & directories Miscellaneous
Multi-user Multi-tasking Hardware independent OS (written in C language) Rich set of tools and scripts Secure
Flavours
> Solaris > FreeBSD > Linux
Applications
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
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?
OS Name
Machine Name
Release
Version
Hardware Processor
Platform
On a SPARC machine
# uname -a
Feb 2 13:10 Jan 31 15:10 Jan 11 14:39 Feb 6 16:31 Jan 26 11:34
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
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 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
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
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
staff
Username
>/etc/shadow
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
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
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
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
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
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
More info :
> See the man pages (man crontab)
Automated tasks
/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.
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
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
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
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
> > > > >
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.
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 ?