Search This Blog

Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Tuesday, 26 July 2016

Linux - Identify the disk type and disk partitions where mount points allocated

We can use the command LSSCSI. This will show disk device type and model.

In order to find which physical device its assigned to , we can use command line 
LS -L /sys/block/sda

This will give long string of "../../devices/pci0000:00/0000:00:0d.0/host..." where "0000:00:0d.0" is a PCI device ID. Use "lspci" to identify it. LSPCI is in sbin therefore require superuser access

"parted -l" also gives information about disks.

We can check the file "/proc/partitions" for all list of disk partitions to which mount points are linked.

all the mount points can be seen in the file "/proc/mounts"
"fdisk" could be used to analyse the disks but it requires super user access.

If you are looking for IBM SAN disks , as an option we can check file system as GPFS
Note from Wiki: IBM discontinued selling the SAN File System in April 2007. It has been replaced by IBM General Parallel File System(GPFS).

Monday, 4 July 2016

Working with Memory in Unix

In Unix, files are arranged in file hierarchy, this hierarchy called file system. Files in different devices or on same devices can be mounted to one hierarchy so that all these files can be accessed as single file system. Each mount point can be assigned a space limit. The mount points, space usage and available space on each mount point can be listed using following command.
DF -H (all small letters)

When mount point reached max space limited then processes writing to that mount point will start failing as space is not available for that mount point even though lots of space available in disk.

In this scenario we have two options,
         1)  Either increase space limit for mount point (If more space available in disk)
         2) Purge some files to release space
 
The command that can be used to increase the logical extents for the mount point is lvextend .
The following article explains how to increase space limit on mount point
In terms of releasing the space.. it will be as easy as using “rm -rf”

But if do not want to delete existing folder structure and delete only the old or big files, we need to do following
          1) Identify the folder which occupied more space
          2)   Delete older and larger files

To identify the folders that occupied more space in a mount point, we can use following command
DU -H –MAX-DEPTH=1 (all small letters)

To delete files bases on size and time, use find as below
FIND /TMP/VAR -TYPE F -MTIME +15 -SIZE +2M -EXEC RM -RF {} \; (all small letters)


If we want to check the RAM size and memory usage in linux system, we could use either of following commands

view /proc/meminfo file

or use "free" command

or use vmstat for memory statistics. 



Monday, 11 April 2016

Create virtual machine in Windows 8 and windows 10 then install Linux Mint OS


Hyper-V (Hypervisor), virtual machine management services, is part of Window 8 and Windows 10 operating system. But this service is not enabled/installed by default. In order to install or enable virtual machine management services in windows 8/10, we need follow below steps

1.     First we need to enable virtual technology hardware at processor level by accessing BIOS steps
    • BIOS is basic input output system. BIOS configuration defines operating system behavior.
    • In order to access BIOS, we need to restart the computer from  “settings à change pc settings à update and recovery à recovery  à Advanced Startup (and restart now) à troubleshoot à Advanced options à startup settings….. then when it’s being restarted, keep enter pressed
    • Once BIOS is open, you can enable virtual technology from configuration tab
2.     Once VT is enabled from BIOS, go to add/remove programs in the control panel, and click “turn windows features on or off” and select all Hyper-V options and click on. It will install Hyper-V
3.     From Hyper-V manager , you can create new virtual machine and select install OS later while creating virtual machine
4.     Download linux mint iso file from https://www.linuxmint.com/download.php
5.     Then start the virtual machine from Hyper-V manager and choose the ISO file for OS installation and follow the installation steps.
6.     OpenSuse can be installed after downloading ISO file. But we need a network connection from virtual machine for this installation. In order to establish the network connection from virtual machine, create virtual external switch using virtual switch manger and share the external virtual switch while creating the virtual machine

Now connecting this virtual machine unix box from host putty session
1.     Check virtual machine  unix box ip address
Ip add show
à eth0: this is the network name in which inet ip address is the ip address we are looking for
2.     Using the above ip address we can connect via putty using SSH connection type. But before this we need to install SSH in the guest linux OS (in virtual machine). We can do this using “sudo apt-get install openssh-server” command
3.     Once SSH is installed, we need to open the port 22. (this can be done by editing file “sudo vi /etc/ssh/sshd_config” and un comment 22 port).. then stop and start the ssh service as “sudo /sbin/service  sshd stop” and “sudo /sbin/service  sshd start”
4.     Check if SSH is enabled and running using the command “netstat –lnpt | grep 22”
5.     You can test if SSH is working fine by running “ssh <vm ip>” in virtual machine itself
6.     If any issues in order to establish SSH connection from host computer to hyper-v virtual machine, we need to configure virtual machine with static ip. To do this, open YaST control center and go to SYSTEMà NETWORK SETTINGS, then in overview tab edit the ip address as static and give some ip address. Then give this same ip address as default IP4v gateway in routing tab. This will cause internet connection not working in virtual machine, but enable SSH connection from host to virtual machine.

7.     Once successfully connected using ip via SSH connection, revert the static ip to dynamic using the YaST control center. This time both internet and putty SSH connection will work. 

Monday, 4 April 2016

Unix Interview questions

1.      How do we kill a process group and all children of that process group with single command?
Ans: If we want to kill a process group, you can supply negation of group number to kill as “kill –term -1234”, where 1234 is process group id i.e. PGID. If we just use ps -ef, we will not get PGID, instead we get PPID and PPID can not be used to kill process group. To identify the PGID, use PS with -o option.
2.      Using the regular expression in grep, how do we retrieve all the lines which have A as 6th character?
Ans : grep “^.\{5\}A"
Explanation: .(dot) will tell grep that 1 or more characters and \{5\} tells that total 5 characters. Then A is 6th character.
3.      How do we debug the unix script?
Ans:  use –x while running script using ksh or code “set –x” in the script to print all the lines in script
4.      Set –x prints commands after variable substitution. How do you print or echo commands before variable substitution?
Ans:  set -v
5.      If we want to refer a variable or input parameter of parent process in the child process, how do we call the child process?
Ans:  Child process within unix script could be invoked as internal process or external process. If we want to refer input parameters or variables of parent process in the child process, then we need to invoke the child process as internal process. (this can be done as “. Script_full_path” )
For variable usage in the internal process, we need to export the variable.
Input parameters of parent process could be used in the internal child process same way as they used in parent process (i.e. using $1, $2 etc)
6.      How do we know what all processes currently running in the system?
Ans: using ps command.
7.      How could we print 11th line in a file containing 20 records?
Ans: we could use either HEAD and TAIL combination or SED
Using HEAD and TAIL :  head -11 file_name | tail -1
Using SED :   sed –n ’11 p’ file_name
8.      How do we print first 3 words in a record?
Ans: we could use awk, cut to print words treating each word as a field separated by space.
Using cut:  cut –f1-3 –d’ ‘ file_name
Using awk:  awk –FS=” “ ‘{print $1,$2,$3}’ file_name
9.      How do we erase all files in the current directory, including all its sub-directories, using only one command?
Ans: rm –r *
We have another command called "shred" used to overwrite a file number of times (default 25) to make it unrecoverable, not exactly same as rm. Files removed using rm also not recoverable , but file remains at disk (only inode link removed using rm) so sophisticated recoverable 3rd party tool could read therefore shred must be used to shred sensitive information. 
10.   How do we find out history of all commands executed?
Ans : using history command
11.   Differentiate cmp command from diff command?
Ans: The cmp command is used to find out if both file are same (byte by byte) or not. The diff command is used to indicate the changes that is to be made in order to make the two files identical to each other.
12.   If I am owner of a file and I want to change the file owner, how do I do that?
Ans: Only super user or root user can execute change owner command. The file owners can’t change the owner of a file.
In order to change the file owner or group, we can use CHOWN and CHGRP commands.
13.   If I want to echo the data to standard output as well as write to a file in single command, how do I do that?
Ans: using tee command
14.   How do mark end of the command in unix script?
Ans: Normally we use enter or return key to end the line. This end of line character act as signpost. But we could also use ; (semicolon) as command end mark.
15.   What is inode and how do we list them?
Ans: Unix file is stored into two different parts of the disk.  Data Blocks and INODEs
Data blocks contain the actual data
Directories  are tables that contain the link between the file name and inode
Inode contains the file related information like owner , permissions, size, last accessed, last updated, number of hard links
To list inodes we can use –I option in ls.  For example “ls –i” will list all Inodes
16.   what is difference between whois and finger?
ans: whois gives basic information about user logged in.  finger give more details/personal information about the users who are not even logged in. finger could also be used to get information about the remote host users.
17.   What is PS1?
Ans: command prompt string is stored in PS1. We can reset prompt string by changing the PS1 variable value
18.   What are the different unix standard streams?
Ans: stdin (file descriptor 0), stdout (file descriptor 1) and stderr (file descriptor 2)
Echo “abc “ > /dev/null 2>&1 will print nothing because
standard output with file descriptor 1 is directed to /dev/null/ (null or blackhole in disk) and standard error with file descriptor 2 is redirected to file descriptor 1 that is further redirected to /dev/null/
19.   Difference between command piping and grouping?
Ans: piping is the way of using output from one command as input to other command
Grouping is grouping multiple commands together and printing combined output
Piping Examples:  ls -al | more    -- this print one page of listing and pause and print more on enter
Grouping example: (date; cal; date) > out.txt          -- writes all date, calender  information to out.txt file
20.   What is xargs and how its used?
Ans:
If you combine two commands with | without using xargs, the first command output will be used as input data to second command, i.e. second command operate on first command output
But with xargs, first command output will be used as input parameter to second command i.e. second command will operate using first command output
By default xargs runs the command /bin/echo. We can tell xargs to run the command we want.
For example  
Find . –name *.sh | grep “CREATE TABLE”
                     This command will list all the .sh file and grep if any of the .sh file has name containing “CREATE TABLE”..
Find . –name *.sh | xargs grep “CREATE TABLE”
                     This command will list all the .sh file and grep if each and every .sh file if the content of the file has “CREATE TABLE” string
21.   How do we run the jobs as background task ?
Ans: using &
If we want to capture stdout then we can use nohup
background job can be brought foreground using "fg %jobnum", get jobnum using "jobs" command
cntrl+z can be used to interrupt a job and stop temporary and resume job using fg or bg commands (These commands are part of job monitor functions)
22.   How do we schedule a job in unix?
Ans: using crontab or "at"
Edit the crontab (crontab –e ) then add an entry
23.   Can I specify optional arguments for a script and if yes, how do we process optional arguments?
Ans: yes, we can specify using getopts
The getopts utility can be used to retrieve options and option-arguments from a list of parameters
Optional input parameters should be prefixed with -.
For example when running any unix command we give optional parameters right … for rm command, the positional parameter is file name and optional parameters are –f, -r etc..
We need to use while command to process all optional parameters.. example
While getopts ….alloptions, ex: abcdc …..  name
Do
      Case $name in
                a)
                 b)
      esac
done