Day No. 1: What Is An OS
Day No. 1: What Is An OS
1
What is an OS
It is a program which controls hardware
(cpu, ram, hard disk, nic, keyboard, mouse, terminal)
What are the common OS in the market
UNIX (
WIndows (XP, 7, 8, )
2000, 2003, 2008, 2012
MacOS - Mac (Apple)
OS/400 (IBM) - AS/400
MainFrame (IBM)
Command : echo
echo "Hello world"
echo 'Hello world'
echo Hello world
echo Hello world
echo 'Hello world'
echo -e '\n\nHello\nWorld\n'
echo -e '\n\nHello\tWorld\n'
Command : clear
Command : alias
alias cls='clear'
alias
unalias cls
Note: alias will last only for a session
Shortcuts : ctrl + l
Command : whoami
Command : id
Display UID, GID of primary group and GIDs of all secondary groups
Command : date
date -s to set the time of the server - can be run as root
What is a UNIX date : 01-01-1970
date +%D
date +%Y
date +%m
date +%d
date +%d.%m.%Y
date +%d-%m-%Y
Command : cal
cal
cal 1960
cal 5 2014
Command : uname
uname
uname -r
uname -a
Note: architecture,64bit/32it,machinename,
Command : man
man date
man crontab
man 5 crontab
man -k keyword
Command : ifconfig
ifconfig
Note: ip addr, mac addr, netmask, name of ethernet card given by SA
Command : uptime
Command : who
Command : w
Note: JCPU, PCPU, IP Address, What
Command : users
Command : hostname
hostname –s
Command : history
history -c
export HISTSIZE=5000
Note: These commands are stored in a file named .bash_history
!20 <== execute 20th command from your history
ctrl + r <== search at the command prompt
Command : exit
Shortcuts : ctrl + d
Command : ping
ping yahho.com
ping -c2 192.168.0.254
Command : ssh
ssh [email protected]
ssh -l natasha 172.24.0.240
ssh -X [email protected] <== For opening graphical commands
(firefox)
Command : last
Command : write
Command : wall
Shortcuts : ctrl + shift + n
Shortcuts : ctrl + shift + t
Shortcuts : ctrl + shift + +
Enlarge font
Shortcuts : ctrl + -
Reduce font
Shortcuts : up arrow
previous command
Shortcuts : down arrow
next command
Test : No 1
Command : pwd
Command : cd
cd <== goes to your home dir
cd .. <== goes to your parent dir
cd . <== . mean current directory
cd ~ <= goes to your home dir
cd ~natasha <= goes you natasha's home directory
cd ~natasha/bin <= goes to bin directory which is under natasha's
home dir
cd / <== go to the top most dir
cd - <== go to previous directory
Theory : Filesystem (Directory structure)
/
/bin
/etc
/home
/sbin
/root
/tmp
/usr/bin
Command : ls
ls -l <= long listing sorted from a-z
name
time on which the file/dir was last modified
size of a file/dir
group owner
owner/user
no of links
permissions ( owner, members of group owner, others)
file type (- file, d dir, l link, s socket, c char device, b
block device)
devcices (mouse, keyboard, disks, disk partition,
terminal/window, usb)
ls -a <== all - mean show hidden files (hidden file starts with a
.)
ls -t <== list of files sorted by time
ls -r <== list of files sorted by i reverse order
ls -i <== Display inode number of each file
ls -l .
ls -l $HOME
ls -l ~
ls -l ..
ls -l ../..
ls -l ~oracle
ls -ld /home/natasha <== display the info of the direcrory and not
what is inside that directory
Symbol : *
Command : cat
cat /home/arununix/dir1/abhi.txt
cat abhi.txt
cat ./abhi.txt
cat -n abhi.txt
cat -n /etc/passwd
Command : su
su - arununix <== will ask you arununix's password
su - root <== will ask you root's password
su - <== will ask you root's password
Command : more
more /etc/passwd
space bar <== display next page
enter key <== display next line
Command : less
less /etc/passwd
space bar <== display next page
enter key <== display next line
/natasha <== search the word 'natasha' in the file
q <== exit from the file
Command : head
head /etc/passwd <== display top 10 lines of /etc/passwd file
head -6 /etc/passwd <== display top 6 lines of /etc/passwd file
Command : tail
tail /etc/passwd <== display last 10 lines of /etc/passwd file
tail -6 /etc/passwd <== display last 6 lines of /etc/passwd file
tail -f /etc/passwd <== display last few lines contineously
tail -n +2 /etc/passwd <== display all lines from bottom except the
top two
Command : touch
Command : cp
cp /etc/passwd /home/natasha/mypasswd
cp /etc/passwd ./mypasswd
cp /etc/passwd . <= . means pwd
cp /etc/passwd ./bin
cp /etc/passwd bin <== same as above because i im in my home dir
cp /etc/passwd /tmp
cp ../../etc/passwd .
cp /etc/passwd $HOME
cp /etc/*.conf $HOME
cp -i /etc/passwd /tmp
cp -r bin binbac
cp -r ./bin ~/binback
cp -r ./bin ~arununix
cp ~oracle/*.sh ~/bin
Command : set <== display values of user & system defined variables
Command : stat
Command : wc
wc -l <== display total number of lines in a file
wc -w <== display total number of words in a file
wc -c <== display total number of characters in a file
Command : rm
rm a.txt
rm -i a.txt
rm -f a.txt
rm *.sh
rm $HOME/*.txt
rm ~oracle/*.sh
rm /tmp/*.tmp
rm -r ./bin <== remove bin directory
rm -r $HOME/bin <==remove bin directory
rm -r /home/natasha/bin <== remove bin directory
rm -f /tmp/a.txt
rm -i /tmp/a.txt
Command : mkdir
mkdir /home/natasha/newbin
mkdir ~/newbin
mkdir newbin
mkdir ./newbin
mkdir ~oracle/newbin
mkdir $HOME/newbin
mkdir /tmp/natasha
Command : rmdir
rmdir /home/natasha/bin <== remove dir if it is empty
Symbol : ~
Symbol : .
Symbol : ..
Theory : Client Server Concepts
Servers: sshd(22), vsftpd (21), httpd(80) dns(53)
Daemon is a constantly running program in the background
Note: File related commands (ls, cat, more, less, wc , cp, mv, touch,
Note: Dir related commands (ls,
Test : No 2
Day No. 3
Command : man
man -k keyword
SEE ALSO
Command : whatis
whatis ls
Command : whereis
whereis cp
Command : which
which cp
Command : tar
tar -c -f /tmp/natasha.tar /home/natasha
tar -x -f /tmp/natasha.tar -C /tmp/new
tar -t -f /tmp/natasha.tar
tar -c -z -f /tmp/natasha.tar.gz /home/natasha
tar -x -f /tmp/natasha.tar.gz -C /tmp/natasha
tar -c -f /tmp/natasha_26_06_2014.tar $HOME
tar -c -f /tmp/natasha_26_06_2014.tar ~
Command : gzip
gzip abcd.txt
gzip /tmp/abcd.tar
Command : gunzip
gunzip abcd.txt.gz
gunzip abcd.tar.gz
gunzip /tmp/abcd.tar.gz
Command : zip
zip abcd.txt.zip abcd.txt
Command : unzip
unzip abcd.txt.zip
Command : vi
3 modes - command (Esc) , typing mode (insert) , Ex Mode
Type - insert (below current line - o , above current line)
i - insert before current position
a - insert after the current position
o - below the current line
O - above the current line
delete (word, line, multiple lines, char)
dw - delete a word
dd - delete the current line
9dd - delete 9 lines below the current, including the current
line
D - delete from the current position till the end of line
x - delete a current character
copy (word, line, ...)
yw - copy current word
yy - copy current line
9yy - copy 9 lines including the current line
paste (above the current line, below current line, at the end of the
line)
p - paste clipboard below current line
P - paste clipboard above current line
undo
u - undo previous changes
search (above the current cursor location, below the current cursor
location)
/natasha <== search the word natasha below current position
?natasha <== search the word natasha above current position
n - to goto next occurence of the searched word
replace
:1,20s/natasha/peter/ <== replace natasha by peter from line
1 till 20
replace only the first occurence of word
natasha
:1,40s/natasha/peter/g <== replace natasha by peter from line
1 till 40
replace all occurences of word natasha in
these lines
:1,$s/natasha/peter/g <== replace natasha by peter in the
entire file
replace all occurences of word natasha in
entire file
:%s/natasha/peter/g <== replace natasha by peter in the
entire file
replace all occurences of word natasha in
entire file
Command : sort
cat data.txt
Natasha Pune 99
Sachin Parbhani 9000
Sanyam Satara 888
sort data.txt
sort /home/natasha/UNIX/data.txt
sort -r /home/natasha/UNIX/data.txt
sort -k2 /home/student/Desktop/data.txt
sort -r -k2 /home/student/Desktop/data.txt
sort -k2 -r /home/student/Desktop/data.txt
sort -rk2 /home/student/Desktop/data.txt
sort -k3 /home/student/Desktop/data.txt
sort -k3 -n /home/student/Desktop/data.txt
sort -r -n -k3 /home/student/Desktop/data.txt
sort -rnk3 /home/student/Desktop/data.txt
Natasha:Pune:99
Sanyam:Satara:888
Sachin:Parbhani:9000
sort -n -t: -k3 data.txt
sort -n -t':' -k3 data.txt
Command : uniq
uniq data.txt
Command : cut
cut -d":" -f1 data.txt
cut -d":" -f2 data.txt
cut -d":" -f1,3 data.txt
cut -d":" -f1-3 data.txt
cut -d'a' -f2 data.txt
cut -d' ' -f3 data.txt
cut -d' ' -f3 /home/student/data.txt
File : /etc/passwd
stores user info on your system
1. username
2. password (always x)
3. UID
4. GID
5. Name or info of user
6. home directory of the user
7. login shell <= shell program automatically starts when user logs
in
cut -d':' -f1 /etc/passwd
cut -d':' -f1,6 /etc/passwd
Command : fgrep , grep, egrep
grep natasha data.txt
grep -i natasha data.txt
grep -v natasha data.txt
Command : diff
Command : tr
echo 'natasha Natasha | tr -s ' '
Command : cmp
io redirection: cmd 1> filename
io redirection: cmd 1>> filename
1> filename <== created a blank file
2>
What is a /dev/null file
There is a file named null in /dev/ directory. Anyone has the write
permission on this file. Any data written or redirected to this file is
immediately erased. This file, therefore, can be used for collecting
garbage.
io redirection: cmd | cmd
cut -d':' -f1 /etc/passwd | sort
ls -l | sort -n -k5 | tail -1
ls -l /etc | sort -n -k5 | tail -1
who | cut -d' ' -f1 | sort | uniq
io redirection: &>
io redirection: 2>>
io redirection: 2>
tar -cvf /tmp/a.tar /etc 1> a.txt
tar -cvf /tmp/a.tar /etc 2> b.txt
tar -cvf /tmp/a.tar /etc &> c.txt
Command that need input
cat, wc, sort, more, less, grep, fgrep, egrep, head, tail, sed
io redirection: <
Note: stdout is a virtual file sttting in RAM. You cannot see this file
using
ls command. The file descriptor for this file is 1.
Test : No 4
Day No. 5
Ad Practicals : No 1
Ad Practicals : No 2
Ad Practicals : No 3
Day No. 6
Command : id
id <= displays uid and gid of currently logged in user
id oracle <==displays uid and gid of user oracle
Command : groups
groups <== displays all groups that you are a member o (first group
is the primary group)
groups oracle <== displays all groups that the user oracle is a
member of
FIle : ~/.bash_logout
FIle : ~/.bash_profile
FIle : ~/.bash_history
FIle : ~/.bashrc
FIle : ~/.vimrc
File : /etc/group <== list of all groups
Command : chgrp
chgrp dba a.txt <== changes the group owner of a.txt file to dba
Command : chmod
Only owner/root can run the chmod command on a file/directory
u=owner/user g=group o=other a=all
- <== remove the permission
+ <== give the permission
chmod g+x $HOME
chmod go-rwx $HOME
chmod a+rwx $HOME <= be careful when you run this command
chmod a-rwx a.txt
chmod u+rwx,g+rx a.txt
r - 4
w - 2
x - 1
Shortcuts : ctrl + d
Test : No 6
Q: What is a group
Its a collections of users
Q: If a user named natasha creates a file who would be the owner of that
file and who would be the group owner of that file
natasha, natasha's primary group
Q: How to find out natasha's primary group
groups natasha , id natasha <== first group is the primary group
Q: How to find out gid of natasha's primary group
id natasha
Q: how to find out oracle's uid
id oracle
Q: How to find out arun's secondary groups
id arun
groups arun
Q: How to find out who is the owner of a file (e.g /etc/passwd)
ls -l /etc/passwd
stat /etc/passwd
Q: How to find out what permissions the owner has on a file
ls -l /etc/passwd
stat /etc/passwd
Q: How to find out the ower of a directory (e.g. /tmp)
ls -ld /tmp
Q: Can you copy the /etc/passwd to your home directory. Why
cp /etc/passwd $HOME
Q: Can you run the following command succesfully. Why
rm /etc/shadow
You cannot because you do not have write permission of the parent
dir i.e /etc
Q: Can you run the followig command. Why
mv /etc/passwd /tmp
No. Because you don't have write permission on the /etc directory
Q: Can you(oracle) execute the following command. Why
cp /etc/passwd ~natasha
No. Because oracle does not have write permission to ~natasha
directory
Q: How can run the chmod command
owner of the file , root
Q: How can run the chgrp command
owner of the file , root
Q: How can run the chown command
root only
Q: What permissions are required if user oracle wants to execute the
following command
cp ~natasha/.bash_profile ~
Read permission on ~natasha/.bash_profile file
Write permission on ~ (i.e. write permission on oracle's home
directory)
Q: What permissions are required if user natasha wants to execute the
following command
mv /etc/passwd /tmp
Write permission on /etc directory. Any permission on passwd file
will do
Write permission on /tmp
Q: What is the difference between the following two commands
chmod o+rx /tmp/a.txt <== relative permission. means you cannot
tell what will be the final permission of /tmp/a.txt
chmod 755 /tmp/a.txt <== absolute permission
Q: ls -l /tmp/natasha.txt gives the followig output
-rw-r--r-- 1 oracle oinstall 11 Jul 6 23:11
natasha.txt
Command : ps
ps
ps -e <== shows everything i.e. all processes on your system
ps -f <== full listing i.e. give more info of process
ps -ef
ps aux <== give cpu and ram info of each process
Command : top
Command : free
Swap mean virtual ram. It is not real ram. It is some space on
hard disk
Swap = page file in windows
Swap can be a file on a hard disk or it can be a partition on the
harddisk
File : /proc/meminfo
Command : mpstat
File : /proc/cpuinfo
Command : iostat
Command : bg
Command : jobs <== display list of jobs started from the termonal
Command : sleep
Command : kill
kill 8188 <== kill i.e. terminate the process with 8188 PID
kill -9 8188 <== forceful/sure kill if the process cannot be killed
by the above command
Symbol : &
Shortcuts : ctrl + c
Shortcuts : ctrl + z
Test : No 7
Command : nslookup
Command : nmap
Command : netstat
Command : bc
Command : dnsdomainname
Command : scp
Command : ftp
Command : lftp
Command : sftp
Command : wget
Command : rsync
Test : No 8
What is DNS
Any machine in your organizarion can query this DNS to get answers.
1. Give name of the machine and Get IP from DNS.
2. Give IP of the machine nad Get name from DNS
DNS has a database of IPs and hostnames
Which commands are used to query DNS
nslookup
How to know which is your DNS Server. I.e. which DNS server your machine
is querying
cat /etc/resolv.conf
WHich ports are open on 192.168.0.3
nmap 192.168.0.3
Which file documents the list of all possible ports used by various
daemon processes(services)
/etc/services
WHich ports are open on your own machine
nmap localhost
nmap your_own_ip_address
How to find out which clients are connected to your machine (not just ssh
clients)
netstat -tn (t=tcp connections, n=show ip addressess instead of
hostnames
How to find out the domainname of your DNS Server
dnsdomainname
Give at least 2 commands to copy a file from one server to another
scp
You must know the password
it uses the ssh protocol(22). Therefore you need to know the
password that you use to ssh into the machine
ftp
You may not need to know the password (anonymous)
It uses ftp protocol(21)
wget
YOu don't need to know the password
It uses http protocol(80)
rsync
ssh and telnet are not used to copy/download/upload a file from one
server to another. They are used only to establish the connection to
another server.
Q: Assume that your are a user named arun currently logged on to
172.24.0.240
Day No. 9
Command : crontab
Note: crontab is a command which interacts with a daemon named
crond.
Daemon is a constantly running process in the background. The daemon
process
is not attached to the termminal. i.e. TTY is ?. This crond daemon is
automatically started when your server is booted. You can check if this
process is running or not by typing the following command
ps -ef | grep crond
service crond status
The purpose of this daemon process is to wake up every minute and
check if there are any programs/scripts that need to be executed on
behalf of
some user. A program/script scheduled to run at a specific time is called
as a
job. In other words crond is reponsible for running jobs on your system.
The crontab command is the way to tell crond about which
program/script to run at which time and at which interval.
crontab -l <== see the existing job schedule
crontab -r <== remove the existing job schedule (dengerous command)
crontab -l > crontab.backup <== output of crontab -l is stored
in crontab.backup file
crontab < crontab.newfile <== Schedule eritten in crontab.newfile
is sent as an input to the crontab command
Command : find
find command is used to find a file/s in a specific directory
(Note: grep is used to find a word/string in a file/s)
find - search for files in a directory hierarchy
Syntax :
find which_dir [criteria] action
--------- ----------- -------
A file whose name is a.txt and print it on the terminal
find /home -name a.txt -print
A file whose name is a.txt, A.txt, A.TXT and print it on the terminal
find /home -iname a.txt -print
Find in either /home or /etc a file whose owner is natasha and print it
on the terminal
find /home /etc -user natasha -print
Find in /home a file whose groupowner is oinstall and list the file
details like the ls command
find /home -group oinstall -ls
Find in /home a files whose permission is 777 and list the file details
like the ls command
find /home -perm 777 -ls 2> /dev/null
+10 10 -10
<-----------------------------------------|---------------------|
5 15
(Today)
Find files in /home whose modification time is exactly 7 days ago
find /home -mtime 7 -print
Find all log files in /tmp whose modification time is before 7 days and
delete them
find /tmp -mtime +7 -name '*.log' -delete
# touch file_{1,2,3}
# touch FILE_{1,2,3}
# touch file_{pune,mumbai,nagpur}
8> find /var -user root -group mail -print 2> /dev/null
9> find /var/lib -user rpm -group rpm -ls 2> /dev/null | less
13> find /etc -name *.conf.bak -ok rm {} \; # -ok is like -exec, but
asks user for confirmation
19> find /bin /usr/bin/ /sbin -perm -u+s -ls 2> /dev/null
Command : mail
mail <== check the email inbox (1, d 2, d * )
mail [email protected] (. means end of email)
mail -s 'Hello' [email protected] < /etc/passwd
mail -s 'Hello' [email protected] -a b.txt < /dev/null
Test : No 9
===========
Q: Name 4 scheduler software popular in the industry
Control M (Windows or UNIX)
Autosys (Windows or UNIX)
Windows Scheduler (Only on windows)
cron (Only on All UNIX flavors )
Q: What is a job
It is a schedule for a specific program/script/executable
e.g. Run ifconfig command at 3:00 PM every day
e.g. Run backup.sh script at 4:00 AM every sunday
Q: Which process must be running in order to for a job to be executed
crond
Q: How to check if the crond daemon/process is running or not
ps -ef | grep crond
service crond status
Q: WHo is the owner of the crond process
root
Q: How to check the current schedule of jobs
crontab -l
Q: How to submit an instruction to a crond daemon
crontab -e
Q: How to disable one job from an existing scheedule
crontab -e and then comment the line for the job by putting #
Q: How to take a backup of your current job schdule
crontab -l > mycron.bak
Q: What is the 4th field in crontab file
Month
Q: What permissions are required on the program/script file
Owner mush have execute permission
chmod u+x script_name
Q: How to check if your job has failed or succeed
1. Check the email
2. Capture stdout and stderr to a file and check the file's content
Q: Will your job run if you are not logged on
yes. The machine needs to be up
Q: Will your job run if your server is shut down
No.
Q: If you restart the server will your jobs run that were scheduled
during the downtime
Q: How to run a script every minute
* * * * * /home/natasha/a.sh
Q: How to run a script at 8:10 PM every night except sundays
10 20 * * 1-6 /home/natasha/a.sh
10 20 * * 1,2,3,4,5,6 /home/natasha/a.sh
Q: How to run a script at 2:30 PM on Christmas day only,and 31st of
December only
30 14 25,31 12 * /home/natasha/a.sh
Q: Will you receive an email from cron if there is no stdout and stderr
from scheduled script/program
no
Q: Can you execute a script when another job is successfully completed.
i.e. can you have dependency on jobs
No. You will have to do some shell scripting to perform this task
Q: Can you execute a script when a specific file arrives from another
server to your server in a predefined directory.
Yes. Need shell scripting
Q: How to schedule a job for another use
crontab -u sunil -e <= You must be root to execute this command
Q: How to deny some users from secheduling any jobs on their behalf
Put their username isn /etc/cron.deny file <= You must be root to
execute this command
Q: How to allow only few users to schedule cron jobs
Put their username isn /etc/cron.allow file <= You must be root to
execute this command
Q: What will happen if a username is in both files (cron.allow &
cron.deny)
He will be denied to schedule any jobs
Q: What are the two types of link in UNIX
soft link, symlink, symbolic link
hard link
Q: What is a link
It is another way/name of accessing a file
Windows has a same concept like softlink. Its called a shortcut
Windows does not have a concept link hardlink
Q: What is a hardlink
inum of originial file and hardlink are same
sizes/timestamp/owner/permission of originial file and hardlink are
same
if original file is deleted, hardlink still works.
hardlink cannot be created across partitions
cp consumes additional disk space , hardlink does not consume
additional disk space
How to create a hard link
ln a.txt b.txt
Command : rpm
rpm -qa
rpm -qa | grep nmap
rpm -q
rpm –qi
Command : ssh-copy-id
Command : ssh-key-gen
Command : vmstat
Test : No 10
Ad Practicals : No 1
Ad Practicals : No 2
Ad Practicals : No 3
Day No. 12