Flipkart

Showing posts with label Red Hat. Show all posts
Showing posts with label Red Hat. 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"

Friday, 10 March 2017

Network Routing in Linux (Centos&Red Hat) - It’s all Practical

Agenda:
To know what is Network Routing and to know HOW and WHERE routing in Linux/UNIX can be used.
What is a Network Routing:
Network Routing is a process on Linux and UNIX servers in which a system administrator configures or modifies a kernel structure called a routing table to define the network accessibility among the computers in two or more different networks.
In simple words,  suppose if I want to connect to a system which is in 172.168.1.1 network from 192.168.1.1 network. It is directly not possible to talk to a computer which is in a different network. Here comes the gateway concept. To connect to 172.168.1.1 network, we need to go and connect to its default gateway first which will route us further to the computers which are in its own network.
So, routing helps us to go and connect to the computers which are not in same network through gateways.
A routing table contains the information on how packets are to be forwarded among the computers in different networks.
How to see the present routing settings OR Routing Table on your Linux Server?
#route -n is the command which provides present routing details
Note: You must be ROOT user to be able to configure routs on your Server
#netstat -r was used earlier but now it’s completely obsolete program and the alternate for the same is # ip route
Example output:
[root@LinuxServer ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric      Ref     Use Iface
192.168.1.3     0.0.0.0         255.255.255.255 UH   0      0        0         usb0
x.x.x.x              0.0.0.0         255.255.255.0     U      0      0        0         eth2
172.1.1.1         0.0.0.0         255.255.252.0     U      0      0        0         eth10
172.0.0.0         0.0.0.0         255.255.230.0     U      0      0        0         eth3
0.0.0.0             x.x.x.x          0.0.0.0                 UG    0      0        0         eth2
[root@LinuxServer ~]#
It is necessary to know what each column is meant for in the above output to be able to configure further changes to your routing table as needed for you.
1.Destination⇒ This column represents the route from your Linux server to the target server. In this case, it is 192.168.1.13 in the first row.
2.Gateway⇒ This column has the information whether a gateway is used for the route specified. In this example, only the last row has used default gateway and rest of the routes do not a default gateway.
3.Genmask⇒ This column represents netmask of the interface for which route has been configured
Flags⇒ This column shows whether the route is up and whether Gateway was used for the connection. U->Up and G->Gateway
4.Iface⇒ This column has the interface name of our own Linux server through which a route has been established and supposed to work while connecting to other system in a different network.
Types of Network Routing:
There are two types of network routing. They are:
  1. Dynamic Routing
  2. Static Routing
Dynamic Routing:
A dynamic routing is generally used by an ISP who usually manages large number of network connections which may be difficult to discuss here.
Static Routing:
Static routing  is the one which we use in our enterprise networks.
 We have the commands #route and #ip route to add or delete the routs on your Linux server.
SCENARIO:
Let's take a scenario in which we need a routing change to be able to connect to a system from your Linux server.
Suppose the IP address of your server (server1) is 172.168.1.1 with netmask 255.255.255.0 on interface eth1.
And the server(server2) your aiming to connect has IP:192.168.1.1 with netmask 255.255.255.0 on interface eth1
Where can you find the above details? :)
1. #ifconfig -a is the command to see all the existing network interfaces on your Linux Server.
2. #ifconfig eth1 ==> is used to fetch the details only about eth1 interface.
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   
 So, I now have a goal to establish a route in such a way that I should be able to connect to my server server1 to server2.
==>Command is the below to add a static route for the same on server1:
#route add -net 192.168.1.0 netmask 255.255.255.0 dev eth1
In the above line why did I use 192.168.1.0 instead of 192.168.1.1? Because I would like to be able to connect not only the system with th IP:192.168.1.1 but all the systems in this network.
So, If you have only one system in the target network, you can specify single IP address as well.
==>If the gateway for the network 192.168.1.0 is not added already, we use the below command to add the same:
#route add default gw 192.168.1.20  
If I want to route the connection from server1 to server2, we use the below command:
#route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.20
==>We can also set the flags to allow or reject the connections using #route command as shown below:  
#route add -net 10.1.1.0 netmask 255.0.0.0 reject
With the above command, I restricted access to 10.1.* network from my server.
IMPORTANT POINTS:
1).So far I have added all the routes from server1 to server2 which means, we get access from server1 to server2 but vice versa does not work until you add appropriate routes in server2.  So, game should be both the ways always :)
2).The routs which we usually add using the route commands like above will be vanished after the host reboot. How to make them persistent?
To add a persistent static route in Red Hat and Centos Linux, create a file called route- in the /etc/sysconfig/network-scripts/ directory like below:
 The file /etc/sysconfig/network-scripts/route-eth0 looks like below  
# cat /etc/sysconfig/network-scripts/route-eth0
ADDRESS0=192.168.1.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.1.20
ADDRESS1=172.16.1.1
NETMASK1=255.255.255.0
GATEWAY1=172.16.1.10  
There are different formats to put text in this file, the one which I chose seems to be a meaningful one.
->Once the above file is created, a network restart is required using the below command to make the changes working.
#service network restart
I hope you take system downtime from business while running the above command on your production server :)
How to Delete the existing route entries ?
You can use the same route command with del argument.
Example:
#route del -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.20
**Do not forget to comment out the lines in the route-ethx file for permanent changes
 
HAPPY LINUX LEARNING :)
 
Related Posts from Blog:
Easy search from Google:  
Use the text in quotes shown below to get into my blog quickly:
"linuxunixdatabases 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 :)