Professional Documents
Culture Documents
Unix Pract
Unix Pract
1---------
What is an OS
UNIX (
WIndows (XP, 7, 8, )
MainFrame (IBM)
AIX (IBM),
Solaris (Sub)
HP-UX (HP)
DEC (Digital)
Open Source
License, Subscription
How to logon to UNIX machine
Command : echo
ld'
echo -e '\n\nHello\nWorld\n'
echo -e '\n\nHello\tWorld\n'
Command : clear
Command : alias
alias cls='clear'
alias
unalias cls
Shortcuts : ctrl + l
Command : whoami
Command : id
Display UID, GID of promary group and GIDs of all seconday groups
Command : date
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
Command : who
Command : w
Command : users
Command : hostname
hostname -s
Command : history
history -c
export HISTSIZE=5000
Command : exit
Shortcuts : ctrl + d
Command : ping
ping yahho.com
Command : ssh
ssh shekhar@172.24.0.240
Command : last
Command : write
Command : wall
Shortcuts : ctrl + shift + n
Enlarge font
Shortcuts : ctrl + -
Reduce font
Shortcuts : up arrow
previous command
next command
Test : No 1
Q: Which command will log you out from your current session
Q: How do you search a command with a specific key word from your history
Q: Display list of users who have logged on to your system since the last 4 days
Q: Name 4 flavors of UNIX that are currently popular in the indian IT industry
Q: Is Linux Free
Day No. 2
---------
Command : pwd
Command : cd
cd <== goes to your home dir
cd ~shekhar/bin <= goes to bin directory which is under shekhar's home dir
/bin
/etc
/home
/sbin
/root
/tmp
/usr/bin
Command : ls
name
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)
ls -a <== all - mean show hidden files (hidden file starts with a .)
ls -l .
ls -l $HOME
ls -l ~
ls -l ..
ls -l ../..
ls -l ~oracle
ls -ld /home/shekhar <== display the info of the direcrory and not what is inside that directory
Theory : What is inode (index Node). It is structure. A table with info of a file
Symbol : ?
Symbol : *
Command : cat
cat /home/arununix/dir1/abhi.txt
cat abhi.txt
cat ./abhi.txt
cat -n abhi.txt
cat -n /etc/passwd
permissios:
r4
w2
x1
Dir ls vi, rm cd
Command : su
passwd shekhar <== change shekhar's password (only root can change someone else's password)
Command : more
more /etc/passwd
Command : less
less /etc/passwd
Command : head
head /etc/passwd <== display top 10 lines of /etc/passwd file
Command : tail
tail -n +2 /etc/passwd <== display all lines from bottom except the top two
Command : touch
Command : cp
cp /etc/passwd /home/shekhar/mypasswd
cp /etc/passwd ./mypasswd
cp /etc/passwd ./bin
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
Theory : What is a shell script & how to execute it
Command : file
Command : set <== display values of user & system defined variables
Command : stat
Command : wc
mv a.txt b.txt
mv /home/shekahr/a.txt /home/shekhar/b.txt
mv $HOME/a.txt b.txt
mv ~/a.txt ~shekhar/b.txt
mv ./a.txt ./b.txt
mv a.txt /tmp
mv a.txt /tmp/b.txt
mv ~arununix/.bash_profile ~
mv ~oracle/*.sh ~/bin
mv /home/shekhar/bin /home/shekahr/binbak
mv /home/shekhar/bin/* /home/shekahr
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 -f /tmp/a.txt
rm -i /tmp/a.txt
Command : mkdir
mkdir /home/shekhar/newbin
mkdir ~/newbin
mkdir newbin
mkdir ./newbin
mkdir ~oracle/newbin
mkdir $HOME/newbin
mkdir /tmp/shekhar
Command : rmdir
du -b /etc/passwd
du -b /tmp
du -b ~
du -b $HOME
du -s -b .
du -s -b /
Symbol : ~
Symbol : .
Symbol : ..
Note: File related commands (ls, cat, more, less, wc , cp, mv, touch, stat,
head, tail
chmod,chown chgrp)
vi)
Note: Dir related commands (ls -ld , mkdir, rmdir , rm -r cd , stat, chmod, chown, chgrp
Test : No 2
ls -l ( - regular file
d directory file
l link file
s socket file)
stat
ls -l , stat
ls -l , stat, du
ls -l /etc
du /etc
Q: *Display list of all files that have extension .config in /etc directory
ls -l /etc/*.config
pwd
home dir
/tmp
cd
cd ~
cd $HOME
cd /tmp
Q: *Write a command to go to /tmp diretory using relative path (assume that your are in /etc directory)
cd ../tmp
cp /etc/passwd $HOME
cp /etc/passwd ~
cp /etc/passwd ~shekhar
cp ../etc/passwd ../home/shekhar
cp ../../etc/passwd .
Q: You are in /tmp directory. you file the following commands. Is there any difference between them
:q!
rm -rf ./bin
rm -rf bin
rm -rf /bin
Q: Copy the password file in /etc directory to /tmp directory. Name it as shekhar_passwd
cp /etc/passwd /tmp/shekhar_passwd
Q: *Who is the owner of /etc/passwd file. Who is the owner of the file you just created
ls -l /etc/passwd
stat /etc/passwd
Q: *What is an inode and how to find out and inode number of a file
ls -li /etc/passwd
stat /etc/passwd
ls -l /etc/passwd
stat /etc/passwd
*Q: Copy all .conf files from /etc to your home directory
cp /etc/*.conf /home/shekhar
cp /etc/*.conf ~/shekhar
cp ./*.conf /home/shekhar
cp *.conf /home/shekhar
cp *.conf ~shekhar
cp /etc/*.conf .
*Q: Create a directory named config_files under your home directory
mkdir /home/shekhar/config_files
mkdir ~shekhar/config_files
*Q: Move all .conf files in your home directory to the config_files directory that you just created.
mv /home/shekhar/*.conf /home/shekhar/config_files
mv /home/shekhar/*.conf ~shekhar/config_files
mv ~shekhar/*.conf ~shekhar/config_files
mv ~shekhar/*.conf ~shekhar/config_files
mv ~/*.conf ~/config_files
*Q: What is the size of /etc/group file. Tell 3 commands to find the size of a file.
ls -l /etc/group
ls -s /etc/group
stat /etc/group
du -b /etc/group
Q: Create a backup of all files in config_files directory under your home directory to
/tmp/shekhar_backup directory
mkdir /tmp/shekahr_backup
cp /home/shekhar/config_files/* /tmp/shekhar_backup
*Q: Which command will display the top 26 lines of /etc/passwd file
*Q: Write a command to delete all shell scripts from the bin directory of shekhar's home directory. Shell
must ask you a y/n question before deleting each file.
rm -i ~shekhar/*.sh
rm -i /home/shekhar/*.sh
Q: Create an alias in such a way that you will be always asked for a y/n confirmation when you delete
any file.
*Q: How do display the bottom 6 lines of the group file in the /etc directory
tail -6 /etc/group
*Q: Copy all .config files /etc directory to a newly created directory named config in your home
directory.
Copy all shell scripts from shekhar's home directory to a newly created directory named bin in your
home directory.
Q: Take a backup of both of the above directories inder /tmp/shekharbackup - replace shekhar by your
name
$HOME
/tmp/shekharbackup
bin
config
mkdir /tmp/shekharbackup
mkdir /tmp/shekhar/backup/bin
cp ~shekhar/bin/*.sh /tmp/shekharbackup/bin
cp -a a,txt b.txt
cp -r dir1 dir2
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 -t -f /tmp/shekhar.tar
tar -c -f /tmp/shekhar_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
Command : unzip
unzip abcd.txt.zip
Q: Take the compressed backup of your friend's home dir and keep the backup file in your home dir.
What is the compression ration
Command : vi
dw - delete a word
9dd - delete 9 lines below the current, including the current line
paste (above the current line, below current line, at the end of the line)
undo
search (above the current cursor location, below the current cursor location)
replace
redo
save
:w <== save
:q <== quit
:set nu
:set nonu
k <== up arrow
page up
page down
:r a.txt <== bring contents of a.txt into current file at the current cursor location
Test : No 3
gzip abcd.tar
*Q: Which compression utility is used to compress a the file when we use -z option tar
gzip
gunzip abcd.tar.gz
unzip abcd.txt.zip
man -k copy
which ifconfig
whereis ifconfig
$HOME
- bin
- docs
- commands.txt <== copy from UNIX dir under shekhar's home dir
- tmp
move all other files and dir under you home dir to tmp
mkdir /home/jason/tmp
mv /home/jason/* /home/jason/tmp
mkdir /home/jason/bin
mkdir /home/jason/docs
cp ~shekhar/UNIX/commands.txt ~/docs
wc -l $HOME/docs/mycommands.txt
*Q: How many lines are there in commands.txt file in shekhar's home dir
wc -l ~shekhar/UNIX/commands.txt
ls -l abcd.tar
stat abcd.tar
*Q: reduce the tar file using the best compression utility
Q: Extract your docs dir in /tmp from the tar file that you have just created
*Q: take a backup of bin and docs directory under your home directory. The backup file should be
named as /tmp/mybak.tar
Day No. 4
---------
Command : sort
cat data.txt
Shekhar Pune 99
Sachin Parbhani 9000
sort data.txt
sort /home/shekhar/UNIX/data.txt
sort -r /home/shekhar/UNIX/data.txt
Shekhar:Pune:99
Sanyam:Satara:888
Sachin:Parbhani:9000
Command : uniq
uniq data.txt
uniq -c data.txt
Command : cut
cut -d":" -f1 data.txt
File : /etc/passwd
1. username
2. password (always x
3. UID
4. GID
7. login shell <= shell program automatically starts when user logs in
Regular expressions
egrep '(shekhar|peter)' data.txt <== show all line those contain either shekhar or peter
grep -E '(shekhar|peter)' data.txt <== show all line those contain either she khar or
peter
Command : diff
Command : tr
Command : cmp
I/O Redirection
> 1>
>> 1>>
2>
2>>
<
<<EOF
&>
&>>
tee
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: &>
io redirection: 2>>
io redirection: 2>
C*Q: take a backup of bin and docs directory under your home directory. The
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
Test : No 4
Q: Find out all IP address from where users are currently logged in
w | grep -v 'load average:'| grep -v TTY | tr -s ' ' | cut -d' ' -f3
alias ip='ifconfig | head -2 | tail -1 | cut -d":" -f2| cut -d" " -f1'
Q: Find out all uniq IP address from where users were logged in (use last command)
Q: List of IPs from where users had logged in which are not internal IPS(172, 192, desktop)
last | fgrep -v 192. | fgrep -v 172. | fgrep -v desktop | tr -s ' ' | cut -d ' ' -f3 | sort | uniq
last | egrep -v (172|192|desktop) tr -s ' ' | cut -d ' ' -f3 | sort | uniq
last | grep -E -v (172|192|desktop) tr -s ' ' | cut -d ' ' -f3 | sort | uniq
Q: Display ip address/es or machine names of the machine from where shekhar has loogged in
who | grep shekhar | cut -d'(' -f2 | cut -d ')' -f1 | sort | uniq
process Management
--------------------
What are the main attributes of a process that are stored in process table
1. owner of a process
4. CPU consumption by
5. RAM consumption
6. Nice value
7. command
ps
ps -f
ps -e
ps -ef
ps aux
kill pid
kill -9 pid
ctrl c
ctrl z
bg
fg
jobs
&
Q: How many processes oracle is running
Q: Display PID and owner of the process which is comsuming highest cpu
ps aux | sort -k3 -n | tail -1| tr -s ' ' | cut -d' ' -f1,2
Q: Display PID and owner of the process which is comsuming highest ram
ps aux | sort -k4 -n | tail -1| tr -s ' ' | cut -d' ' -f1,2
br[nr]k
bank
banrk
bark
bakk
^root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
r..t
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
Day No. 5
---------
Ad Practicals : No 1
Ad Practicals : No 2
Ad Practicals : No 3
Day No. 6
---------
Theory : permissions, groups, users
Command : id
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
Command : chgrp
chgrp dba a.txt <== changes the group owner of a.txt file to dba
Command : chmod
chmod a+rwx $HOME <= be careful when you run this command
w-2
x-1
chown
chown arun.dba a.txt <== changes owner to arun and group owner to dba
chown arun:dba a.txt <== changes owner to arun and group owner to dba
chown -R arun:dba /home/shekhar <== changes owner to arun and group owner to dba of all
file and subdirectories inside /home/shekhar
Shortcuts : ctrl + d
Test : No 6
Q: What is a group
id shekhar
id oracle
id arun
groups arun
ls -l /etc/passwd
stat /etc/passwd
ls -l /etc/passwd
stat /etc/passwd
ls -ld /tmp
cp /etc/passwd $HOME
rm /etc/shadow
You cannot because you do not have write permission of the parent dir i.e /etc
No. Because you don't have write permission on the /etc directory
cp /etc/passwd ~shekhar
No. Because oracle does not have write permission to ~shekhar directory
root only
Q: What permissions are required if user oracle wants to execute the following command
cp ~shekhar/.bash_profile ~
Q: What permissions are required if user shekhar wants to execute the following command
mv /etc/passwd /tmp
chmod o+rx /tmp/a.txt <== relative permission. means you cannot tell what will be the final
permission of /tmp/a.txt
cp /tmp/shekhar.txt /tmp/shekhar.txt.new
cp -p /tmp/shekhar.txt /tmp/shekhar.txt.new
What will be the attributes(permissions, owner, groupowner, timestamp) if user root execites
the following commands
cp /tmp/shekhar.txt /tmp/shekhar.txt.new
cp -p /tmp/shekhar.txt /tmp/shekhar.txt.new
Day No. 7
---------
Command : ps
ps
ps -ef
Command : top
Command : free
Swap mean virtual ram. It is not real ram. It is some space on hard disk
File : /proc/meminfo
Command : mpstat
File : /proc/cpuinfo
Command : iostat
Command : bg
Command : fg <== bring the background job to foreground. The background job
Status :
Running in background
Stopped
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
lsof
lsof -u shekhar <== all files opened by shekhar (can be run by root)
lsof -p 1881 <== shows all files opened by process 1881
Symbol : &
Shortcuts : ctrl + c
Shortcuts : ctrl + z
Test : No 7
======================================================================
Q: What is a daemon
doing its work. The names of daemon processes usually end with a letter 'd' .
Example, crond, vsftpd, httpd, sshd, atd, ntpd, udevd, iscsid, libvirtd. They are not tied to any terminal. It
means under the heading of TTY there would be a '?' for this process
sshd - Secure Shell daemon (if this is down, no one can connect using ssh to the server)
ntpd - time server daemon (other machines in your organization will sync up their system time
with this machine)
udevd - this daemon is used to give consistent/same names to all devices
/dev/sdb
Q: Run sleep command in one window and kill it from another window
Q: There is a shell script named /tmp/my.sh. Copy it to your home dir. Run it in the background.
sh my.sh &
sh ~shekhar/my.sh &
sh /home/shekhar/my.sh &
jobs
kill 8237 <= 8237 is the PID found from the ps command
jobs
ctrl c <== the job should be running in the foreground. i.e. it should be attached to a tty
kill
free
free
cat /proc/meminfo
top
Q: Why should you not used kill -9 as the first option to kill a process
You may create defunt processes. Which creates memory leak. ememory
kill -9 8188 < == Sinnale no. 9 tells the process to surely kill itself
Q: How many CPUS are there on your system. Give their info
cat /proc/cpuinfo
cat /proc/cpuinfo <== Look for Model name. it will show 'virtual' if the machine is virtual
top
---------
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
hat is DNS
Domain name server. it is a daemon process running on one of the servers in our organization.
The name of the daemon process is named.
Any machine in your organizarion can query this DNS to get answers.
nslookup
How to know which is your DNS Server. I.e. which DNS server your machine is querying
cat /etc/resolv.conf
nmap 192.168.0.3
Which file documents the list of all possible ports used by various daemon processes(services)
/etc/services
nmap localhost
nmap your_own_ip_address
How to find out which clients are connected to your machine (not just ssh clients)
dnsdomainname
scp
it uses the ssh protocol(22). Therefore you need to know the password that you use to ssh into
the machine
ftp
wget
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
1. Copy a file named a.txt in /tmp of 192.168 0.3 to your home directory
Note: The above command will ask you shekhar's password on 192.168.0.3
Note: The above command will ask you peters's password on 192.168.0.4
on 192.168.0.4
Note: The above command will ask you priya's password on 192.168.0.3
Note: The above command will ask you abhishek's password on 192.168.0.4
Q: WHy the following command would not work
It will copy entire /etc directory to /tmp directory on 172.24.0.240. It will ask you shekhar's
password on 172.24.0.240
Q: On which port ftp server runs
21
ftp ip_address_of_the_server_where_ftp_daemon_is_running
These days not used in industry. Because it transfers data/file in non-encrypted format
lftp ip_address_of_the_server_where_ftp_daemon_is_running
lftp does data transfer in encrypted and has many advanced commands to transfer the
file
sftp ip_address_of_the_server_where_ftp_daemon_is_running
ftp://192.168.0.250/
Q: Mention at least 5 different ftp commands
help
pwd
ls -l
put b.txt
mget *.sh
mput *.txt
quit
Day No. 9
---------
Command : crontab
automatically started when your server is booted. You can check if this
job. In other words crond is reponsible for running jobs on your system.
----- --------------
minute 0-59
hour 0-23
Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The speci-
fied range is inclusive. For example, 8-11 for an "hours" entry specifies execution at hours
8, 9, 10 and 11.
Lists are allowed. A list is a set of numbers (or ranges) separated by commas. Examples:
"1,2,5,9", "0-4,8-12".
Step values can be used in conjunction with ranges. Following a range with "<number>" speci-
fies skips of the number’s value through the range. For example, "0-23/2" can be used in the
hours field to specify command execution every other hour (the alternative in the V7 standard
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)
Syntax :
A file whose name is a.txt, A.txt, A.TXT and print it on the terminal
Find in /home a file whose groupowner is oinstall and list the file details like the ls command
Find in /home a files whose permission is 777 and list the file details like the ls command
+10 10 -10
<---------------------|---------------------|
5 15
(Today)
Find all log files in /tmp whose modification time is before 7 days and delete them
# 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
Test : No 9
q <== quit
mail abishekk (entire subject and body and at the end type a . )
Scheduler - cron
===========
Q: What is a job
crond
Q: How to check if the crond daemon/process is running or not
root
crontab -e
crontab -e and then comment the line for the job by putting #
Month
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
No.
Q: If you restart the server will your jobs run that were scheduled during the downtime
* * * * * /home/shekhar/a.sh
Q: How to run a script at 8:10 PM every night except sundays
10 20 * * 1-6 /home/shekhar/a.sh
10 20 * * 1,2,3,4,5,6 /home/shekhar/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/shekhar/a.sh
Q: How to run a script at 2:30 AM on Christmas day only,and at 2:30 PM on 31st of December only
30 2 25 12 * /home/shekhar/a.sh
30 14 31 12 * /home/shekhar/a.sh
Q: Write a shell script in your home directory named backup.sh to take the backup of your home dir.
Schedule this script to run every night at 9:00 PM. Check the next day if the script was executed
successfully. redirect the stderr and stdout of your script to a log file named $USER_backup.log in /tmp
directory
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.
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
Put their username isn /etc/cron.allow file <= You must be root to execute this command
hard link
Q: What is a link
ln -s /tmp /home/shekhar/abcd
ln -s orig_file_name link_name
ln -s /home/shekhar/a.txt /tmp/b.txt
ln -s a.txt b.txt
Q: What is a hardlink
cp consumes additional disk space , hardlink does not consume additional disk space
ln a.txt b.txt
find $HOME -inum 558 -print 2> /dev/null (558 is your file's inum)
Day No. 10
---------
Command : rpm
rpm -qa
rpm -q
rpm -qi
Command : ssh-copy-id
Command : ssh-key-gen
Command : vmstat
Test : No 10
rpm -q software_name
rpm -q httpd
ssh-keygen
$HOME/.ssh
rw-------
RSA
ssh-copy-id student@192.168.0.2
The above command will copy your public key in student's account
~student/.ssh/authorized_keys
Day No. 11
---------
Ad Practicals : No 1
Ad Practicals : No 2
Ad Practicals : No 3
Day No. 12
-----------
Networking
--------------