Flipkart

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

Saturday, 18 March 2017

Everything about SymLink in Linux/Unix

This article provides detailed description of Symlink and Hardlink concepts in Linux(RedHat&Centos)/Unix with meaningful examples and useful images.

What is a Symlink?

A Symlink OR a soft link simply means to make a new name which is called as a symbolic link for an existing file or a directory keeping its original name and path untouched.

**We can create more than one symbolic link for a given file/directory to use the same file/directory in different applications or different programs at a time.

**A symbolic link can have any kind of permissions as it is just a reference to the original file/dir. So, only the permissions on original file/dir matter but not of the symbolic link.

**The ownership of the softlink/symbolic link does not matter too except for the case where the link needs to be removed.

To remove a soft link, user must have valid permissions for that link.

Uses of a symbolic or Soft Link:

=>Symbolic links are accessible across the file systems, which means a symbolic link can be created in one file system to a file which is in another partition or file system.

=>This is really useful to save the some amount of disk space as we can have multiple references at different locations to one file which can sit on a single location.

**Command to create a symlink is #ln -s  


Example:

Symlink to a Directory:

[root@server2 dir]# pwd
/server/dir
[root@server2 dir]# ls -lrth
total 4.0K
drwxr-xr-x 2 root root 4.0K Mar 1 11:40 test
[root@server2 dir]# cd test
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
[root@server2 test]# ln -s test test_symlink1
[root@server2 test]# ln -s test test_symlink2
[root@server2 test]# ln -s test test_symlink3
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink1 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink2 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink3 -> test
If  you do a cd to any of the symlinks shown above, you will find the same data which is there in test directory. That's the magic of symlink. Same is true in the below example of a file as well.

 

Symlink to a file:

[root@server2 test]# ln -s f1 f1_symnlink1
[root@server2 test]# ln -s f1 f1_symnlink2
[root@server2 test]# ls -lrth
total 0
-rw-r--r-- 1 root root 0 Mar 1 11:40 f1
-rw-r--r-- 1 root root 0 Mar 1 11:40 f2
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink1 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink2 -> test
lrwxrwxrwx 1 root root 4 Mar 1 11:57 test_symlink3 -> test
lrwxrwxrwx 1 root root 2 Mar 1 11:58 f1_symnlink1 -> f1
lrwxrwxrwx 1 root root 2 Mar 1 11:58 f1_symnlink2 -> f1
[root@server2 test]#


What will happen if a symlink is removed?

As we already discussed, symlink or softlink or symbolic link is just a reference to the original file, absolutely there is no impact to the original file/directory with respect to their data if a symlink is removed.

How to remove a symlink?




A symlink can be removed just like a normal Linux/unix file with the below command.
#rm symlink_name

Worth Noting:

**Symlinks can be created to every file/dir to which you can locally access using the regular Linux file system commands  like #cd, # cat or #ls -lrth.

**That clarifies, a symlink cannot be created to a network location unless the network file/dir is mounted on your local system.

** A symlink can also be created to a file which is non-existent. That symlink is called "Dangling Link"


We will discuss about Hard Links in my next article and then see the differences between the two.

HAPPY LINUX LEARNING :)

Feel free to add any points that can be useful to make this article complete in the comments section.

Other Related Articles:
1.File System State is clean with errors in Linux
2.SCRIPT Monitor File System Usage/size in Linux

Easy search through Google:
Use the text in quotes shown below to get into my blog quickly from Google:

"linuxunixdatabase blogger"

Wednesday, 15 March 2017

TAR Command in LINUX/UNIX

In this Mid-Week article, I am going to share/remind few useful things that can be done using TAR command in Linux/UNIX.

What is TAR stands for?
Tape ARchive==> A Utility which helps to create single file called an ""archive" or a TAR file from number of individual files in Linux/UNIX.

->The contents of this archive can be only the names of the individual files used to create it but not the actual data those file have.

Creating an archive or a TAR file:

Example:

In the below example, I have created f1.out, f2.out and f3.out files with some text.

[root@server2]# ls -lrth
total 12K
-rw-r--r-- 1 root root 317 Mar 15 11:10 f1.out
-rw-r--r-- 1 root root 579 Mar 15 11:11 f2.out
-rw-r--r-- 1 root root 508 Mar 15 11:11 f3.out


Then I used below command to create a tar file which contains the above three files

[root@server2]# tar -cvzf File.tar.gz f1.out f2.out f3.out
f1.out
f2.out
f3.out

[root@server2]# ll
total 16
-rw-r--r-- 1 root root 317 Mar 15 11:10 f1.out
-rw-r--r-- 1 root root 579 Mar 15 11:11 f2.out
-rw-r--r-- 1 root root 508 Mar 15 11:11 f3.out
-rw-r--r-- 1 root root 857 Mar 15 11:17 File.tar.gz


File.tar.gz is the command file that was created using the above TAR command.

Why should we TAR a file?

In my experience I used TAR utility to move bunch of files in compressed mode up to the 1/3rd of the original size of the data with a single command.
==>So, suppose I have 10 files combined have a data of 30 GB, I can make a single TAR file that has a size of just 10 GB and move to the target

How to UnTAR an archive or a TAR file?

#tar -xvzf File.tar.gz
==>The above command gives you back the original files with their actual sizes in the target directory you wish either on local or on network.

Example:

[root@server2]# tar -xvzf File.tar.gz
f1.out
f2.out
f3.out

What other things TAR can do?



Scenario: Suppose you have received a TAR file from one of your superior or a colleague, but you want to know what that file actually has before opening that file. How?

#tar -tvf
==>The above command actually shows the names of the files that the above TAR file or archive has.

[root@server2]# tar -tvf File.tar.gz
-rw-r--r-- root/root       317 2017-03-15 11:10:27 f1.out
-rw-r--r-- root/root       579 2017-03-15 11:11:07 f2.out
-rw-r--r-- root/root       508 2017-03-15 11:11:29 f3.out


-So, if the files above seen are relevant to you, you can think of un-tarring the actual contents. If not, the file could be a malware or virus or anything that could be marked as danger.

Feel free to comment or add more details in the comment's section.

Little follow up work :
You can also break a big tar file into parts for easy uploads, just think of it or try to get the information :).

As always, refer man page of TAR to get required information.

#man tar

HAPPY LINUX LEARNING :)

Easy search from Google:  
Use the text in quotes shown below to get into my blog quickly:
"linuxunixdatabase blogger"

Tuesday, 7 March 2017

How to Debug a Shell Script-(Linux&UNIX)

In this article, I will explain how to debug a shell script.
I am taking a script which was used in one of my previous articles that generates Alerts whenever /opt File System on your Red Hat/Centos Linux Server exceeds its usage 65%.

Below is the reference:
Shell SCRIPT to Monitor File System Usage/size in Linux

The script used to generate alerts is as shown below:
=====
#!/bin/sh
df -kh | grep "/opt" |  awk '{print $5" "$6}'|cut -d " " -f1 --output-delimiter='  '|tail -n 1 | while read Value;
do
  echo $Value
  fssize=$(echo $Value | awk '{ print $1}' | cut -d'%' -f1)

 if [ $fssize -ge 65 ]; then
mail -s "Alert: /opt is Almost out of disk space on Server `uname -n`, Needs Immediate Attention"
serversupport@domain.com
fi
done
======


 Now, how can a system administrator debug this script to make sure if it works fine before he deploys this script on his production Linux Server?

SCRIPT DEBUGGING:

 1.To start debugging any script, take meaning full lines or parts of a line from the above script which works in a command line and try to get the output for each meaningful Linux command of the script.

Output from my system for the above script is as below:

[root@server2 ~]# df -kh | grep "/opt"
/dev/sda7             7.8G  1.4G  6.1G  68% /opt


 [root@server2 ~]# df -kh | grep "/opt" |  awk '{print $5" "$6}'
68% /opt


[root@server2 ~]# df -kh | grep "/opt" |  awk '{print $5" "$6}'|cut
 -d " " -f1 --output-delimiter='  '
68%


[root@server2 ~]# df -kh | grep "/opt" |  awk '{print $5" "$6}'|cut -d " " -f1 --output-delimiter='  '|tail -n 1
68%

  • Tail command helps us especially if there are more than one lines in the output to select the last line
[root@server2 ~]# echo 18%| awk '{ print $1}' | cut -d'%' -f1
68


Since 68 > 65, script is bound to generate an alert for us.

IMPORTANT NOTE:
  • It is always a better idea to redirect the output of this script to a log file which can be viewed later if you want to track what the script did when this job was actually run.
So a modified Crontab entry would look like below:

CRON ENTRY:

##This job runs every 5 minutes to check file system usage
*/5 * * * * /home/admin/FSalerts.sh > /home/admin/FSalerts.log

 You can open the log file /home/admin/FSalerts.log anytime you wish and see what was the activity done when the job was actually run.

This file is useful to troubleshoot the issues with the script or Cron when you did not receive any alert even if the FS usage exceeded your set threshold.

HAPPY LINUX LEARNING :)

Friday, 17 February 2017

Removing existing LVM from your Linux System

GOAL:

To remove or clean up the existing LVs on Linux Server: 

I described the step by step process on how to remove the existing LVMs from your Linux Box.
This article best works with RedHat Linux and Centos Distributions.
1. We need to know what LVs are present on our Linux Box. Use the below commands to see LVs on your box. 

#lvs  OR  #lvdisplay 

Output is as below:
# lvs
  LV            VG              Attr      LSize    Pool Origin Data%  Move Log Cpy%Sync Convert
  linxback01.vol datavg01        -wi-ao--- 1000.00g
  linxback00.vol linxdatavg00 -wi-ao---    5.00t

# lvdisplay
  --- Logical volume ---
  LV Path                /dev/datavg01/linxback01.vol
  LV Name                linxback01.vol
  VG Name                datavg01
  LV UUID                2k8o6B-ZJuS-40dX-FZF0-Ym96-bpd7-UQ6fY6
  LV Write Access        read/write
  LV Creation host, time host1, 2015-12-18 09:24:11 -0500
  LV Status              available
  # open                 1
  LV Size                1000.00 GiB
  Current LE             256000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  --- Logical volume ---
  LV Path                /dev/linxdatavg00/linxback00.vol
  LV Name                linxback00.vol
  VG Name                linxdatavg00
  LV UUID                aqZsFS-djb4-v0Ws-Hzbv-1gIk-Vjmb-1hsHU7
  LV Write Access        read/write
  LV Creation host, time host1, 2014-08-26 17:00:28 -0400
  LV Status              available
  # open                 1
  LV Size                5.00 TiB
  Current LE             1310715
  Segments               5
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
==>We also need to know what is the associated PV Name to the LV. Command is below: 

# pvs

  PV             VG              Fmt  Attr PSize    PFree
  /dev/emcpowerf03 datavg01        lvm2 a--  1024.00g 24.00g
2. I can go ahead and start the removal process of both the LVMs shown above.But  you also need to know on which file system these LVa are mounted.
How to check that? Here is the way...
#df -kh
Just check for linxback00.vol and linxback01.vol in the output to see which file systems are using this LVs.
Example:
/dev/mapper/linxdatavg00-linxback00.vol
                      5.0T  3.6T  1.1T  77% /Backup_FileSystem

So, the above entry in the df -kh output shows the LV: linxback00.vol is mounted on the Directory : /Backup_FileSystem
Before we remove the LV, we need to unmounts and delete associated File system.
3. Unmounting associated File system using the Commands below:
fuser -cu /Backup_FileSystem
umount /Backup_FileSystem
4. We are all set to remove associated LV now.

#lvremove

Example :#lvremove  linxback01.vol
 
5. Remove associated VG

#vgremove <VG name>

Example: #vgremove datavg01

6. Remove associated PV
pvremove <device name>
Example:#pvremove   /dev/emcpowerf03


7.Validate using below commands if the LV is cleaned as expected. Ideally there should not be any output for the below commands once LV clean up is done. These are all non-disruptive commands.

#vgscan
#lvs
#vgs
#pvs
Important Note:
Usually LVs are associated with external SAN. The above process cleans up your LVs but SAN will still be there on system. You need to Work with your storage team to get the LUNs unmounted from your Linux server as soon as you are done with the LV removal process.
HAPPY LINUX LEARNING AS ALWAYS :)
Feel free to ask any questions or start a discussion about this topic.
My other Posts are below:
File System State is clean with errors in Linux:
How to use IPERF to test interface/network throughput in Linux:
Linux/Unix Network Troubleshooting:

Saturday, 4 February 2017

Linux/Unix Network Troubleshooting

This document outlines the troubleshooting process of network interface related issues on a Linux/UNIX server

I do not post man page details here because every Linux distribution has man page details for all the commands.


I am taking a scenario here to discuss as to what can be done to fix a network interface issue on a Linux server.


Issue: Network connectivity is lost or throughput is ceased(getting very less speed comparatively) all of a sudden on my Linux server.

SOLUTION:

Things to do in sequential order:

You need to be a root user for all these steps.

Take output of #ifconfig command:

Example:

#ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 01:89:A8:G8:4R:54
         inet addr:192.168.9.5  Bcast:192.168.99.255  Mask:255.255.255.0
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:190458 errors:0 dropped:0 overruns:0 frame:0
         TX packets:86768 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:150
         RX bytes:30701269 (29.3 Mb)  TX bytes:7878926 (1.9 Mb)
         Interrupt:9 Base address:0x5000

Check the details from the above output one by one as shown below:

1.Make sure this ip (192.168.9.5) does not have a duplicate entry in your network which means, any IP address has the capacity to server a single interface at a given time. It cannot be active on two or more devices at a time.

If you find a duplicate entry in network for this IP, make sure to drop the other connection and try the functionality of the interface. You can get in touch with your network team here to identify why this IP has a duplicate entry in the network and then to fix it.

2. Reset the problematic interface using below commands

#ifdown eth0
#ifup eth0

Check the functionality, proceed if the issue persists

3. Open the file /etc/sysconfig/network-scripts/ifcfg-eth0 and verify  the MAC address which is also called as Hardware address of the NIC (Network Interface Card) HWaddr 01:89:A8:G8:4R:54 is same as in the above output.

If you see any discrepancy, adjust the MAC address of the NIC to your interface (eth0 in this case).

There are different ways to find out the MAC address of an NIC in linux. You may need to google it which for the way which suits your case.

For an existing connection, ifconfig should be sufficient.

Check the functionality of the interface once the mac address is set appropriately.

4. If the issue still persists, next thing to check is RX packets and TX packets errors and drops.

RX packets/bytes ⇒ Receiving Data by server
TX packets/bytes ⇒ Sending Data by server

#Ethtool -S eth0

⇒ is the best command to see rx/tx or crc related errors on the interface

If you see any RX packets have errors or packets were dropped, this means data is becoming faulty even before it reaches the target server.

So, you need to consider resetting the switch port at this point and see if the errors or the dropped packets are still increasing.

If the errors continue to increase and drop packets are also increasing, first thing to suspect is NIC on your Linux server.

Check the NIC firmware version using #ethtool -i eth0 and see if it is up to date, if not go ahead and update the NIC firmware according to make of NIC.

Check the functionality, if the issue still persists, go ahead and replace your NIC and check the functionality

Same is true for TX errors and drops as well.

5. If the issue still persists, check the cable functionality with the help of your Data Center people and change the cable from external switch to your Linux server if found faulty.

6.If the issue persists, try host reboot and check the functionality

7. With the above steps, we isolated the issue on various aspects and we are clean from server point of view.

It's time to push  the issue to your Network Team and get it fixed from switch point of view.

=======
HOW TO TEST NETWORK SPEED: Use the link below

http://linuxunixdatabase.blogspot.in/2017/02/how-to-use-iperf-to-test.html#!/2017/02/how-to-use-iperf-to-test.html

OR
click on Using IPERF COMMAND LINUX
========

HAPPY LINUX LEARNING :)

Feel free to ask any questions or start a discussion about this topic.

My other Posts are below:


File System State is clean with errors in Linux:
http://linuxunixdatabase.blogspot.com/2017/02/file-system-state-is-clean-with-errors.html

How to use IPERF to test interface/network throughput in Linux:
http://linuxunixdatabase.blogspot.com/2017/02/how-to-use-iperf-to-test.html

Linux/Unix Network Troubleshooting:
http://linuxunixdatabase.blogspot.com/2017/02/linuxunix-network-troubleshooting.html

Removing existing LVM from your Linux System

http://linuxunixdatabase.blogspot.com/2017/02/removing-existing-lvm-from-your-linux.html

Learning AWK and SED Tools for LINUX/UNIX
http://linuxunixdatabase.blogspot.com/2017/02/learning-awk-and-sed-tools-for-linuxunix.html