df -kh — Disk space
free -m — Memory
cat /proc/cpuinfo | grep processor | wc -l — No of CPU
cat /etc/redhat-release — os details
ps -ef|grep mysql — mysql service status
ps -ef|grep smon — oracle service status
———–commands to find logged in users in linux————-
last -a
users
finger
whoami
less /var/log/secure
–To check Hard Disk Size
df -kh
–To Check Memory/Ram Size
free -m
Check number of CPUís
–$cat /proc/cpuinfo
To count the number of processing units use grep with wc
$ cat /proc/cpuinfo | grep processor | wc -l
4
The number of processors shown by /proc/cpuinfo might not be the actual number of cores on the processor. For example a processor with 2 cores and hyperthreading would be reported as a processor with 4 cores.
To get the actual number of cores, check the core id for unique values
$ cat /proc/cpuinfo | grep ‘core id’
core id : 0
core id : 2
core id : 1
core id : 3
So there are 4 different core ids. This indicates that there are 4 actual cores.
Ref: http://www.binarytides.com/linux-cpu-information/
Check memory information
–cat /proc/meminfo
Check swap
–$swapon -s
Kernel parameter
–#/sbin/sysctl -a
cat /etc/sysctl.conf
System error log
–/var/log/messages
Nic configuration
–ifconfig -a
Directory Commands ñ mkdir
To create a new directory below current directory:
–$ mkdir new-dir-name
To create multiple versions of directory:
–$ mkdir -p dir1/dir2/dir3
Creates dir1 first. Creates dir2 under dir1. Creates dir3 under dir2
To create a directory with given permissions:
–$ mkdir -m 754 new-dir
new-dir will be created with 754 permissions
Directory Commands ñ rmdir
To remove a directory:
–$ rmdir dir-name
rmdir can remove only empty directories
To remove directory along parent directory:
–$rmdir -p dir1/dir2/dir3
Deletes all the directories
Directory Commands ñ cd
Stands for change directory
Used to navigate among directories
–$ cd dir1
Takes to dir1 directory
–$ cd
Takes to the home directory
Can give full-path-name or relative-path-name as argument
Linux Macine:
–# uname -m
File Backup using ëtarí command
The ìtarî command stands for tape archive that generally used by system/database administrator to
write archives directly to tape devices or can use it to create archives files on disk.
The ëtarí program is easy to use and transportable having limits on file name size,
wonít backup special files, does not follow symbolic links, and doesnít support multiple volumes.
The main advantage is that ëtarí is supported every where or can be moved easily from one disk to another disk or machine to machine.
It is also useful for copying directories.
The ëtarí program takes one of three function command line arguments:
c ñ to create a tar file
t ñ table of contents (see the name of all files)
x ñ extract (restore) the contents of the tar file.
In addition to above function command line argument these argument are useful:
f ñ Specifies filename
z ñ Use zip/unzip to compress the tar file or to read from a compressed tar file.
v ñ Verbose output, show the file being stored into or restored from tar file.
Example:
To tar all .arc and .ctf files into a tar file named data.tgz use:
–tar cvzf data.tgz *.arc *.ctf
This will creates (c) a compressed tar file name data.tgz (f) and shows the file being stored into the tar file (v). The .tgz suffix is a convention for gzipped tar file.
To extract files from tar file
–tar ñxvf filename.tar
To tar up all files and directories under current directories or under PROD1 directory and writes files to filename.tar.
–tar cvzf data.tgz shahid123
–tar -cvf /tmp/filename.tar .
–tar -cvf /tmp/filename.tar PROD1
It is often more useful to tar a directory (which tar all files and subdirectories recursively unless you specify other option)
To see a tar file table of contents
–tar tzf data.tgz
To display only the content in tar binary file
–tar ñtvf filename.tar
When file size is too large (more than 8GB), use ñE option.
–tar -cvfE /data/oradata/tars/PROD1/large_file_blob.tar
Retrieve Oracle version information
SQL> select * from v$version where banner like ëOracle%’;
Other Commands
As a DBA you need to use frequent OS command or alt least how to query the OS and its hardware.
Usually we do it before fresh install upgrade, migrate of database/operating system. Here is some of the useful frequently
used day to day OS command for DBA.
To find and delete files older than N number of days:
find . -name ë.í -mtime +[N in days] -exec rm {} ;
Example : find . -mtime +5 -exec rm {} ;
The above command is specially useful to delete log, trace, tmp file
To list files modified in last N days:
find . -mtime ñ -exec ls -lt {} ;
Example: find . -mtime +3 -exec ls -lt {} ;1
The above command will find files modified in last 3 days
To sort files based on Size of file:
ls -l | sort -nk 5 | more
useful to find large files in log directory to delete in case disk is full
To find files changed in last N days :
find -mtime -N ñprint
Example: find -mtime -2 -print
To find CPU & Memory detail of linux:
cat /proc/cpuinfo (CPU)
cat /proc/meminfo (Memory)
Linux: cat /proc/cpuinfo|grep processor|wc -l
HP: ioscan -fkn -C processor|tail +3|wc -l
Solaris: psrinfo -v|grep ìStatus of processorî|wc ñl
psrinfo -v|grep ìStatus of processorî|wc ñl
lscfg -vs|grep proc | wc -l
To find if Operating system in 32 bit or 64 bit:
ON Linux: uname -m
On 64-bit platform, you will get: x86_64 and on 32-bit patform , you will get:i686
On HP: getconf KERNEL_BITS
On Solaris: /usr/bin/isainfo ñkv
On 64-bit patform, you will get: 64-bit sparcv9 kernel modules and on 32-bit, you will get: 32-bit sparc kernel modules. For solaris you can use
directly: isainfo -v
If you see out put like: ì32-bit sparc applicationsî that means your O.S. is only 32 bit but if you see output like ì64-bit sparcv9 applicationsî
that means youe OS is 64 bit & can support both 32 & 64 bit applications.
To find if any service is listening on particular port or not:
netstat -an | grep {port no}
Example: netstat -an | grep 1523
To find Process ID (PID) associated with any port:
This command is useful if any service is running on a particular port (389, 1521..) and that is run away process which you wish to terminate using kill
command
lsof | grep {port no.} (lsof should be installed and in path)
How to kill all similar processes with single command:
ps -ef | grep opmn |grep -v grep | awk ë{print $2}í |xargs -i kill -9 {}
Locating Files under a particular directory:
find . -print |grep -i test.sql
To remove a specific column of output from a UNIX command:
For example to determine the UNIX process Ids for all Oracle processes on server (second column)
ps -ef |grep -i oracle |awk ë{ print $2 }í
Changing the standard prompt for Oracle Users:
Edit the .profile for the oracle user
PS1=îhostname
*$ORACLE_SID:$PWD>î
Display top 10 CPU consumers using the ps command:
/usr/ucb/ps auxgw | head -11
Show number of active Oracle dedicated connection users for a particular ORACLE_SID
ps -ef | grep $ORACLE_SID|grep -v grep|grep -v ora_|wc -l
Display the number of CPUís in Solaris:
psrinfo -v | grep ìStatus of processorî|wc -l
Display the number of CPUís in AIX:
lsdev -C | grep Process|wc -l
Display RAM Memory size on Solaris:
prtconf |grep -i mem
Display RAM memory size on AIX:
First determine name of memory device: lsdev -C |grep mem
then assuming the name of the memory device is ëmem0í then the command is: lsattr -El mem0
Swap space allocation and usage:
Solaris : swap -s or swap -l
Aix : lsps -a
Total number of semaphores held by all instances on server:
ipcs -as | awk ë{sum += $9} END {print sum}í
View allocated RAM memory segments:
ipcs -pmb
Manually deallocate shared memeory segments:
ipcrm -m î
Show mount points for a disk in AIX:
lspv -l hdisk13
Display occupied space (in KB) for a file or collection of files in a directory or sub-directory:
du -ks * | sort -n| tail
Display total file space in a directory:
du -ks .
Cleanup any unwanted trace files more than seven days old:
find . *.trc -mtime +7 -exec rm {} ;
Locate Oracle files that contain certain strings:
find . -print | xargs grep rollback
Locate recently created UNIX files:
find . -mtime -1 -print
Finding large files on the server:
find . -size +102400 -print
Crontab Use:
To submit a task every Tuesday (day 2) at 2:45PM
45 14 2 * * /opt/oracle/scripts/tr_listener.sh > /dev/null 2>&1
To submit a task to run every 15 minutes on weekdays (days 1-5)
15,30,45 * 1-5 * * /opt/oracle/scripts/tr_listener.sh > /dev/null 2>&1
To submit a task to run every hour at 15 minutes past the hour on weekends (days 6 and 0)
15 * 0,6 * * opt/oracle/scripts/tr_listener.sh > /dev/null 2>&1
UNIX Useful Commands
1) Find whether OS is 64/32 Bit Kernel in UNIX.
uname -a
2) Find free physical memory in UNIX.
free -m
3) Find CPU details in UNIX.
cat /proc/cpuinfo
4) Find files modified within specific time.
find . -mtime -3 (modified less than 3days ago)
5) command used to alter file permissions.
chmod 777 abc.txt
6) Command used to reset the Ownership.
chown oracle:dba abc.txt
7) command used to set, or reset, the users login password.
Passwd username
8) Kill specific process in UNIX.
Kill -9 processid
9) Command used for display last given lines of a file.
tail -n alert_PROD.log
10) Command used for intall a rpm package.
rpm -ivh packagename.rpm
11) Command used to querry about any rpm package
rpm -q packagename
12) Command to Check the server up time
uptime
13) Command to check the file versions
strings -a |grep ë$Headerí
14) Command will keep ëní number of days files and remove rest of file.
find . -mtime +n -exec rm {} ; &
15) Basic commands for vi editor
i :- insert before cursor.
l : insert begining of the line.
a :- append after the cursor.
A :- Append at the end of the line.
o :- insert a blank line below the cursor.
O :- insert a blank line above the cursor position.
h :- from current position one char towards left .
I :- from current position one char towards right.
j :- from current position one line towards down.
k :- from current position one line towards up.
Shift+g :- go to end of the file.
Shift+:1 :- go to top of the file.
dd ñ> delete the ful line.
:q! ó> closing the file without saving any changes.
:wq! ñ> save the changes and force close.
:w ñ> to save the changes without closing the file.
UNIX Crontab Basics
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
óóóóóóóóóóóóóóóóóóóóóóóóóóóóóó
Minute Hour Month Day Month Weekday Command
0-59 0-23 1-31 1-12 0-6 (0=Sunday)
óóóóóóóóóóóóóóóóóóóóóóóóóóóóóó
- * * * * command to be executed
ñ ñ ñ ñ ñ
| | | | |
| | | | +óñ day of week (1 ñ 7) (monday = 1)
| | | +óó- month (1 ñ 12)
| | +óóó day of month (1 ñ 31)
| +óóóñ hour (0 ñ 23)
+óóóó- min (0 ñ 59)
crontab -e Edit your crontab file, or create one if it doesnít already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file.
Example:
To run the calendar command at 6:30 a.m. every Monday, Wednesday, and Friday, enter:
1
30 6 * * 1,3,5 /usr/bin/calendar
To run the calendar command every day of the year at 6:30, enter the following:
1
30 6 * * * /usr/bin/calendar
To run a script called maintenance every day at midnight in August, enter the following:
1
0 0 * 8 * /u/harry/bin/maintenance
Note : You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use,
crontab if your name does not appear in the file /usr/lib/cron/cron.deny. If only cron.deny exists and is empty
scp stands for secure cp (copy), which means that you can copy files across an ssh connection that will be encrypted, and therefore secured.
You can this way copy files from or to a remote server, you can even copy files from one remote server to another remote server, without passing through your PC.
Syntax:
scp [[user@]from-host:]source-file [[user@]to-host:][destination-file]
Description of options
from-host: Is the name or IP of the host where the source file is, this can be omitted if the from-host is the host where you are actually issuing the command
user: Is the user which have the right to access the file and directory that is supposed to be copied in the cas of the from-host and the user who has the rights to write in the to-host
source-file:Is the file or files that are going to be copied to the destination host, it can be a directory but in that case you need to specify the -r option to copy the contents of the directory
destination-file:Is the name that the copied file is going to take in the to-host, if none is given all copied files are going to maintain its names
Options
-p Preserves the modification and access times,
as well as the permissions of the source-file in the destination-file
-q Do not display the progress bar
-r Recursive, so it copies the contents of the
source-file (directory in this case) recursively
-v Displays debugging messages
Example
[[email protected]]$ scp java.tar.gz [email protected]:/home/tamim/
The authenticity of host ‘172.168.0.222 (172.168.0.222)’ can’t be established.
RSA key fingerprint is 23:b9:a4:b9:93:99:28:1f:4c:08:fa:8a:5f:d7:10:d0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.168.0.222’ (RSA) to the list of known hosts.
[email protected]’s password:
java.tar.gz 100% 35MB 11.7MB/s 00:03
File is successfully transfer to the host 172.168.0.222 in home/tamim directory
To copy a directory user scp ñr
================================================================
Some useful Linux Command for DBA
ls: List files
cp: Copy files
mv: Move and rename files
mkdir: Make a directory
alias: Define command macros
rm: Remove files and directories
more: Page through output
head: Show beginning of file contents
tail: Show end of file contents
df: Display filesystem space usage
du: Display directory disk space usage
cat: Show and concatenate files
grep: Search for patterns in files
chmod: Change permissions of files
chown: Change owner of files
zip: Compress and package files together
gedit: A WYSIWYG text editor
export: Make environment settings global
ps: List running processes
touch: Change file time stamps
id: Show information about the current user
sudo: Execute commands as another user
Standard Measurement Tools
ï Top Resource Consumers: top
ï System Activity Reporter: sar
ï Virtual Memory Statistics: vmstat
ï I/O Statistics: iostat
ï System Log files: /var/log/messages
Linux Tools
ï X-based tools: xosview
ï The /proc virtual file system
ï Free and used memory: free
Tools for monitoring and tuning CPU include:
ï top
ï pstree and free
ï vmstat
ï Syntax: vmstat
ï Example : # vmstat 2 5
ï mpstat ñp All
ï sar ñu
ï Syntax: #sar -B
sar -R
ï Example : #sar -B 2 3
sar -R 2 3
ï xosview
ï xload
ï System Monitor
Measuring Total Memory
ï top
ï free
ï cat /proc/meminfo
Monitoring and Tuning I/O
ï /proc file system
ï sar -d
ï I/O statistics by device [iostat ñd]
Syntax : iostat -d
Eample : #iostat -d 2 2
ï I/O activity by partition
iostat ñd -p
ï vmstat
ï xosview
================================================================
Check number of CPU’s
$cat /proc/cpuinfo
Check memory information
cat /proc/meminfo
Check swap
$swapon -s
Kernel parameter
/sbin/sysctl -a
cat /etc/sysctl.conf
System error log
/var/log/messages
Nic configuration
ifconfig -a