Useful Unix and Vi commands and references for Greenplum DBA


Understanding routing

posted May 5, 2013, 5:27 PM by Sachchida Ojha

On Linux and UNIX systems, information on how packets are to be forwarded is stored in a kernel structure called a routing table. You need to manipulate this table when configuring your computer to talk to other computers across a network. The routing table can be used for both static and dynamic routing. Dynamic routing consists of the kernel making decisions as to which route, out of multiple present routes, a packet should take. Since dedicated routers and ISPs generally deal more with dynamic routing, I won’t go into detail, as it is beyond the scope of this Daily Drill Down. In this Daily Drill Down, however, I will focus on static routing because it is the most common.

You can examine the routing table by using the netstat program like this:
netstat -r

This will print out a routing table that may look something like the one in Table A.

Table A
DestinationGatewayGenmaskFlagsMSSWindowIrttIface
192.168.10.0*255.255.255.0U4000eth0
127.0.0.0*255.0.0.0U4000lo
default192.168.10.10.0.0.0UG4000eth0
Kernel IP routing table

The Destination column identifies the destination network. The Gateway column identifies the defined gateway for the specified network. An asterisk (*) appears in this column if no forwarding gateway is needed for the network. The Genmask column shows the netmask for the network; in this case, it is 255.255.255.0. The Iface column shows the network interface. If you had more than one interface, you would see lo (for loopback), eth0 (first Ethernet device), and eth1 (for the second Ethernet device), and so on for the number of interfaces you have installed. Under the Flags section, the U flag means the route is up, and the G flag means that specified gateway should be used for this route. There are other flags that you may see, which include: D for dynamically installed, M for modified, and R for reinstated. These three flags indicate that the route was created or modified by a routing daemon after encountering an ICMP Redirect message. (Usually, you won’t see these flags unless you use dynamic routing.) Finally, you may see a ! flag, which indicates a rejecting route.

The MSS column indicates the default Maximum Segment Size for TCP connections over this route. The Window column indicates the default window size for TCP connections over this route, and the Irtt column indicates the Initial Round Trip Time for this route. The kernel uses this to select values for certain TCP parameters without having to wait for potentially slow answers from remote hosts. These three columns you will normally not need to worry about unless performance is suffering and you're trying to find a way to tweak it. Under most circumstances, you will not redefine the defaults here.

As you can see, I have defined a route for 127.0.0.0, which is the loopback network. The loopback network is handled locally, so no forwarding gateway is required. I also have a defined route for 192.168.10.0, which is the network that the eth0 interface is attached to. It also does not require a forwarding gateway because this is the local network. The last line contains a Destination of default, sometimes shown as 0.0.0.0, which means everything else not already classified. In this case, everything not destined for the loopback network or the 192.168.10.0 network will be sent to the 192.168.10.1 address—which is the forwarding gateway—and the route to the Internet.

Changing static routes
Now that I’ve shown you how to display currently defined routes, you’ll need to know how to change them. This can be done easiest using the Linuxconf program under Red Hat or Linux-Mandrake and similar administration programs under other Linux variants. Some variants, however, do not have tools like Linuxconf to handle defining routes, so let’s take a look at the "old school" method, instead. To do this configuration by hand, you'll have to learn how to use theroute command.

The first argument passed to route will be either add or del, which will tell route if it is adding or deleting a route from the routing table. The rest of the arguments that route uses are dependant upon which action you have chosen.

Let's take a look at creating the routing table so that we have defined the same routes, as shown in the previous example. The first thing you need to do is add the network to which you belong by using:
route add -net 192.168.10.0 netmask 255.255.255.0 dev eth0

This will add the network 192.168.10.0/255.255.255.0 to the Ethernet device eth0. This corresponds with the first line shown in the netstat output.

To add the loopback network, you would use:
route add -net 127.0.0.0 netmask 255.0.0.0 lo

This adds the network 127.0.0.0/255.0.0.0 to the Ethernet device lo, which represents the loopback (or local) network. It is not necessary to do this if you’re using Linux kernels 2.2 or higher, as those kernels will handle this automatically.

Finally, you need to add the default gateway using:
route add default gw 192.168.10.1

This creates the final entry shown by netstat and tells Linux to route all packets not destined for the 192.168.10.0 network or the loopback network to the defined gateway, in this case 192.168.10.1.

In the same way, you can delete networks using the del command with route like this:
route del -net 192.168.10.0

This will delete the routing table entry for the 192.168.10.0 network.

Multinetwork routing
So what happens if you have a more complicated network? Let's assume for a moment that you have two LANs, the first with the 10.0.0.0 network and a second with the 192.168.10.0 network. There is a firewall between the two networks, with two network cards: eth0 is attached to the 10.0.0.0 network, while eth1 is attached to the 192.168.10.0 network. This firewall needs to route packets from the 10.0.0.0 network through the 192.168.10.0 network, which will in turn forward packets to the Internet.

In this scenario, you’d set up the firewall system with two IP addresses: 10.0.0.1 on eth1 and 192.168.10.25 on eth0. The gateway to the Internet on the 192.168.10.0 network is still 192.168.10.1. On the firewall system, you would run route with the following commands:
route add -net 192.168.10.0 netmask 255.255.255.0 dev eth1
route add default gw 192.168.10.1
route add -net 10.0.0.0 netmask 255.0.0.0 dev eth0

On the router, this defines both networks: 192.168.10.0 on eth1 and 10.0.0.0 on eth0. It also assigns 192.168.10.1 as the default gateway.

On the computers in the 10.0.0.0 network, you would use route like this:
route add -net 10.0.0.0 netmask 255.0.0.0 dev eth0
route add default gw 10.0.0.1

This tells each computer that the default gateway is 10.0.0.1, which is your firewall/router.

With both the firewall and the 10.0.0.0 network set up, you should be able to route all packets from the 10.0.0.0 network to the Internet and to the 192.168.10.0 network. So what happens if you have a system in the 192.168.10.0 network you want to be able to talk to systems in the 10.0.0.0 network?

On each system in the 192.168.10.0 network, you will have to configure your routing table a little differently. Here, you would traditionally use:
route add -net 192.168.10.0 netmask 255.255.255.0 dev eth0
route add default gw 192.168.10.1

This configures the network and the default gateway. However, in this case, 192.168.10.1 knows nothing about the 10.0.0.0 network, so your packets would get lost because 192.168.10.1 has no idea where to send the packets and will attempt to send them through the default gateway. You need to add another routing statement to each system in the 192.168.10.0 network like this:
route add -net 10.0.0.0 netmask 255.0.0.0 gw 192.168.10.25

This command tells the kernel to route all packets destined for the 10.0.0.0 network to 192.168.10.25, which it defines as a gateway. So now, by using the three route commands, your kernel will know where to send packets. In this situation, a few things happen:

Packets to 192.168.10.0 are handled without a gateway.
Packets to 10.0.0.0 are sent to the defined gateway, 192.168.10.25.
Packets traveling anywhere else are sent to the default gateway, 192.168.10.1.

Using Linuxconf
Routing can be configured in a more intuitive way if you have the Linuxconf program, or something similar, available to you.

With Linuxconf, you can easily set up routing. To do so, fire up Linuxconf and select the Networking section. Click on Routing And Gateways. Here, you have a few options from which you can choose.

You’ll first need to set the defaults. Click the Defaults button, and define your default gateway by entering 192.168.10.1. If this were a router you were configuring, you would click Enable Routing to tell the kernel to allow packets from other systems to be routed through your system to the defined gateway.

Here, you can also define routes to other networks, routes to other hosts, and routes to alternate local networks. To define routes to other networks, you would use the route command. Click Route To Other Networks, and you will be given a list of the currently defined routes. Click Add to add a new route.

In the Add New Route window, you define the gateway to the other network, the destination network, and the netmask, which is an optional setting. In this case, you might set 192.168.10.25 as the gateway with 10.0.0.0 as the destination. You could optionally select 255.0.0.0 as thenetmask. Click Accept to save the routing definition.

You can similarly define the route to another host. For instance, if you only wanted to be able to reach the computer at IP address 10.0.0.28, you might select the Route To Other Hosts option and add a new route with the gateway of 192.168.10.25 and the destination of 10.0.0.28. In this way, you don't have to do blanket routing, because you would only be dealing with the one host. If you want to deal with more than one host in the 10.0.0.0 network, you probably want to define a route to the entire network. This is handy if you have multiple gateways routing to different parts of the same network. For instance, if 10.0.0.28 was located behind a gateway at 192.168.10.125, while the rest of the 10.0.0.0 network you need was located behind 192.168.10.25, you might define the gateway here of 192.168.10.125 for the destination 10.0.0.28 and have the blanket route to 10.0.0.0 defined in the Route To Other Networks section. Since the packet will always be routed with the best match (or shortest path), it is safe to define routing in this fashion.

Finally, you can define other local networks in the Route To Alternate Local Networks section. Unlike the previous definitions, this does not work by going through a defined network but by broadcasting through the defined Ethernet interface. For instance, if you were configuring your router, the defined local network may be 192.168.10.0 for eth0 and 10.0.0.0 for eth1. You could define the interface as eth1, the destination as 10.0.0.0, and the netmask as 255.0.0.0. This would tell the kernel to route all traffic to the 10.0.0.0 network directly over the eth1 interface instead of through a gateway.

Another likely interface would be running a multilayered network where your primary address is in the 192.168.10.0 network with a secondary address as an alias in the 10.0.0.0 network. For instance, your primary eth0 address might be 192.168.10.12 and your alias—which would be listed as eth0:0—might be 10.0.0.12. You would define the interface as eth0, the destination as 10.0.0.0, and the netmask as 255.0.0.0. Your system would then handle traffic for both the 192.168.10.0 and 10.0.0.0 networks transparently.

More often than not, however, you won't need to make use of this unless you are configuring a router or you have a rather large and complex LAN.

After you've made your changes in Linuxconf, simply exit and save your changes. Then, restart your network using:
/etc/rc.d/init.d/network restart

what is init.d?

posted May 4, 2013, 9:33 AM by Sachchida Ojha

The init.ddirectory contains a number of start/stop scripts for various services on your system.

when you look at the /etc directory you will find directories that are in the form rc#.d (Where # is a number reflects a specific initialization level - from 0 to 6). Within each of these directories is a number of other scripts that control processes. These scripts will either begin with a "K" or an "S". All "K" scripts are run before "S" scripts. And depending upon where the scripts are located will determine when the scripts initiate. Between the directories the system services work together like a well-oiled machine. But there are times when you need to start or stop a process cleanly and without using the kill or killall commands. That is where the /etc/init.ddirectory comes in handy.

[sachi@sachi etc]$ ls -l *.d
lrwxrwxrwx.  1 root root   11 Apr 21 07:22 init.d -> rc.d/init.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc0.d -> rc.d/rc0.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc1.d -> rc.d/rc1.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc2.d -> rc.d/rc2.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc3.d -> rc.d/rc3.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc4.d -> rc.d/rc4.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc5.d -> rc.d/rc5.d
lrwxrwxrwx.  1 root root   10 Apr 21 07:24 rc6.d -> rc.d/rc6.d

bash_completion.d:
total 120
-rw-r--r--. 1 root root  1158 May 21  2011 abrt-cli.bash
-rw-r--r--. 1 root root 45281 May 25  2011 git
-rw-r--r--. 1 root root  2516 Apr  7  2011 gvfs-bash-completion.sh
-rw-r--r--. 1 root root  2592 Apr  7  2011 pk-completion.bash
-rw-r--r--. 1 root root 39423 May 26  2009 subversion
-rw-r--r--. 1 root root 10082 May 21  2011 yum.bash
-rw-r--r--. 1 root root  8031 Feb 18 14:22 yum-utils.bash

chkconfig.d:
total 0

cron.d:
total 12
-rw-r--r--. 1 root root 113 Mar 29  2011 0hourly
-rw-r--r--. 1 root root 108 May 20  2011 raid-check
-rw-r--r--. 1 root root 251 May 20  2011 sysstat

depmod.d:
total 4
-rw-r--r--. 1 root root 116 Dec  2  2010 dist.conf

dnsmasq.d:
total 0

dracut.conf.d:
total 0

event.d:
total 12
-rw-r--r--. 1 root root 146 Jan 25  2011 ck-log-system-restart
-rw-r--r--. 1 root root 141 Jan 25  2011 ck-log-system-start
-rw-r--r--. 1 root root 137 Jan 25  2011 ck-log-system-stop
ls: cannot open directory ipsec.d: Permission denied

latrace.d:
total 148
-rw-r--r--. 1 root root   392 Jul 24  2010 ctype.conf
-rw-r--r--. 1 root root   825 Jul 24  2010 dirent.conf
-rw-r--r--. 1 root root   417 Jul 24  2010 dlfcn.conf
-rw-r--r--. 1 root root   643 Jul 24  2010 fcntl.conf
-rw-r--r--. 1 root root   273 Jul 24  2010 getopt.conf
-rw-r--r--. 1 root root   655 Jul 24  2010 inet.conf
-rw-r--r--. 1 root root    68 Jul 24  2010 ioctl.conf
-rw-r--r--. 1 root root   651 Jul 24  2010 libintl.conf
-rw-r--r--. 1 root root   646 Jul 24  2010 libio.conf
-rw-r--r--. 1 root root   286 Jul 24  2010 locale.conf
-rw-r--r--. 1 root root   292 Jul 24  2010 misc.conf
-rw-r--r--. 1 root root   976 Jul 24  2010 mman.conf
-rw-r--r--. 1 root root 11029 Jul 24  2010 ncurses.conf
-rw-r--r--. 1 root root  3937 Jul 24  2010 netdb.conf
-rw-r--r--. 1 root root  7686 Jul 24  2010 pthread.conf
-rw-r--r--. 1 root root   869 Jul 24  2010 pwd.conf
-rw-r--r--. 1 root root  2289 Jul 24  2010 resource.conf
-rw-r--r--. 1 root root  2626 Jul 24  2010 signal.conf
-rw-r--r--. 1 root root  3365 Jul 24  2010 socket.conf
-rw-r--r--. 1 root root  1642 Jul 24  2010 stat.conf
-rw-r--r--. 1 root root  3943 Jul 24  2010 stdio.conf
-rw-r--r--. 1 root root  6327 Jul 24  2010 stdlib.conf
-rw-r--r--. 1 root root  2834 Jul 24  2010 string.conf
drwxr-xr-x. 3 root root  4096 Apr 21 07:29 sysdeps
-rw-r--r--. 1 root root   198 Jul 24  2010 syslog.conf
-rw-r--r--. 1 root root   934 Jul 24  2010 term.conf
-rw-r--r--. 1 root root   671 Jul 24  2010 termios.conf
-rw-r--r--. 1 root root  1642 Jul 24  2010 time.conf
-rw-r--r--. 1 root root   472 Jul 24  2010 typedefs.conf
-rw-r--r--. 1 root root  4399 Jul 24  2010 unistd.conf
-rw-r--r--. 1 root root   564 Jul 24  2010 utmp.conf
-rw-r--r--. 1 root root   419 Jul 24  2010 wait.conf

ld.so.conf.d:
total 28
-rw-r--r--. 1 root root  17 Jul 30  2010 atlas-x86_64.conf
-r--r--r--. 1 root root 324 May 25  2011 kernel-2.6.32-100.34.1.el6uek.x86_64.conf
-r--r--r--. 1 root root 324 May 20  2011 kernel-2.6.32-131.0.15.el6.x86_64.conf
-rw-r--r--. 1 root root  17 Jan 27  2011 mysql-x86_64.conf
-rw-r--r--. 1 root root  42 Apr 24 17:02 oracle.conf
-rw-r--r--. 1 root root  22 Jul 25  2010 qt-x86_64.conf
-rw-r--r--. 1 root root  21 Apr  2 19:45 xulrunner-64.conf

logrotate.d:
total 48
-rw-r--r--. 1 root root  71 Mar 29  2011 cups
-rw-r--r--. 1 root root 103 May 20  2011 dracut
-rw-r--r--. 1 root root 185 May 21  2011 httpd
-rw-r--r--. 1 root root 162 May 22  2011 libvirtd.lxc
-rw-r--r--. 1 root root 163 May 22  2011 libvirtd.qemu
-rw-r--r--. 1 root root 136 Jul  7  2010 ppp
-rw-r--r--. 1 root root 329 Jul  7  2010 psacct
-rw-r--r--. 1 root root 219 May 22  2011 sssd
-rw-r--r--. 1 root root 228 Mar 30  2011 syslog
-rw-r--r--. 1 root root  32 Apr  8  2010 up2date
-rw-r--r--. 1 root root 100 Jul  8  2010 wpa_supplicant
-rw-r--r--. 1 root root 100 May 21  2011 yum

lsb-release.d:
total 0
-rw-r--r--. 1 root root 0 Apr  3  2011 core-4.0-amd64
-rw-r--r--. 1 root root 0 Apr  3  2011 core-4.0-noarch
-rw-r--r--. 1 root root 0 Apr  3  2011 graphics-4.0-amd64
-rw-r--r--. 1 root root 0 Apr  3  2011 graphics-4.0-noarch
-rw-r--r--. 1 root root 0 Apr  3  2011 printing-4.0-amd64
-rw-r--r--. 1 root root 0 Apr  3  2011 printing-4.0-noarch

makedev.d:
total 140
-rw-r--r--. 1 root root   359 Jul 24  2010 00macros
-rw-r--r--. 1 root root  6213 Jul 24  2010 01alsa
-rw-r--r--. 1 root root   302 Jul 24  2010 01cdrom
-rw-r--r--. 1 root root    37 Jul 24  2010 01console
-rw-r--r--. 1 root root   242 Jul 24  2010 01ftape
-rw-r--r--. 1 root root   218 Jul 24  2010 01generic
-rw-r--r--. 1 root root   141 Jul 24  2010 01ia64
-rw-r--r--. 1 root root   210 Jul 24  2010 01ibcs
-rw-r--r--. 1 root root   256 Jul 24  2010 01ide
-rw-r--r--. 1 root root    96 Jul 24  2010 01ipfilter
-rw-r--r--. 1 root root    90 Jul 24  2010 01isdn
-rw-r--r--. 1 root root   619 Jul 24  2010 01linux1394
-rw-r--r--. 1 root root 27779 Jul 24  2010 01linux-2.6.x
-rw-r--r--. 1 root root   377 Jul 24  2010 01mouse
-rw-r--r--. 1 root root    42 Jul 24  2010 01qic
-rw-r--r--. 1 root root   113 Jul 24  2010 01raid
-rw-r--r--. 1 root root  2325 Jul 24  2010 01redhat
-rw-r--r--. 1 root root  1915 Jul 24  2010 01s390
-rw-r--r--. 1 root root   271 Jul 24  2010 01sound
-rw-r--r--. 1 root root   476 Jul 24  2010 01std
-rw-r--r--. 1 root root   124 Jul 24  2010 01undocumented
-rw-r--r--. 1 root root   163 Jul 24  2010 01v4l
-rw-r--r--. 1 root root   480 Jul 24  2010 02cciss
-rw-r--r--. 1 root root   902 Jul 24  2010 02dac960
-rw-r--r--. 1 root root   464 Jul 24  2010 02ida
-rw-r--r--. 1 root root 11324 Jul 24  2010 02linux-2.6.x

modprobe.d:
total 36
-rw-r--r--. 1 root root   52 Apr 21 07:20 anaconda.conf
-rw-r--r--. 1 root root  884 May 20  2011 blacklist.conf
-rw-r--r--. 1 root root   25 May 24  2011 blacklist-kvm.conf
-rw-r--r--. 1 root root  382 Dec  2  2010 dist-alsa.conf
-rw-r--r--. 1 root root 5596 Dec  2  2010 dist.conf
-rw-r--r--. 1 root root  473 Dec  2  2010 dist-oss.conf
-rw-r--r--. 1 root root   30 Jul  2  2010 openfwwf.conf
-rw-r--r--. 1 root root   49 Apr 21 17:33 vmware-fuse.conf

oddjobd.conf.d:
total 8
-rw-r--r--. 1 root root 1203 Mar 30  2011 oddjobd-introspection.conf
-rw-r--r--. 1 root root 1315 Jan 27  2010 oddjobd-mkhomedir.conf

pam.d:
total 208
-rw-r--r--. 1 root root 272 Mar 29  2011 atd
-rw-r--r--. 1 root root  97 May 20  2011 authconfig
-rw-r--r--. 1 root root  97 May 20  2011 authconfig-gtk
-rw-r--r--. 1 root root  97 May 20  2011 authconfig-tui
-rw-r--r--. 1 root root 192 May 21  2011 chfn
-rw-r--r--. 1 root root 192 May 21  2011 chsh
-rw-r--r--. 1 root root 232 Mar 30  2011 config-util
-rw-r--r--. 1 root root 293 Mar 29  2011 crond
-r--r--r--. 1 root root 146 Mar 29  2011 cups
-rw-r--r--. 1 root root  71 Jan 27  2011 cvs
-rw-r--r--. 1 root root 115 Jul  4  2010 eject
lrwxrwxrwx. 1 root root  19 Apr 21 07:30 fingerprint-auth -> fingerprint-auth-ac
-rw-r--r--. 1 root root 659 Apr 21 07:30 fingerprint-auth-ac
-rw-r--r--. 1 root root 708 Mar 28  2011 gdm
-rw-r--r--. 1 root root 480 Mar 28  2011 gdm-autologin
-rw-r--r--. 1 root root 489 Mar 28  2011 gdm-fingerprint
-rw-r--r--. 1 root root 701 Mar 28  2011 gdm-password
-rw-r--r--. 1 root root 485 Nov 19  2010 gnome-screensaver
-rw-r--r--. 1 root root 147 Oct  5  2009 halt
-rw-r--r--. 1 root root  70 May 20  2011 ksu
-rw-r--r--. 1 root root 728 May 21  2011 login
-rw-r--r--. 1 root root 172 Apr  4  2011 newrole
-rw-r--r--. 1 root root 154 Mar 30  2011 other
-rw-r--r--. 1 root root 146 Jul  7  2010 passwd
lrwxrwxrwx. 1 root root  16 Apr 21 07:30 password-auth -> password-auth-ac
-rw-r--r--. 1 root root 896 Apr 21 07:30 password-auth-ac
-rw-r--r--. 1 root root 155 Apr 19  2011 polkit-1
-rw-r--r--. 1 root root  71 May 20  2011 postgresql
-rw-r--r--. 1 root root 147 Oct  5  2009 poweroff
-rw-r--r--. 1 root root 144 Jul  7  2010 ppp
-rw-r--r--. 1 root root 147 Oct  5  2009 reboot
-rw-r--r--. 1 root root 613 May 21  2011 remote
-rw-r--r--. 1 root root  97 Apr  8  2010 rhn_register
-rw-r--r--. 1 root root 167 Apr  4  2011 run_init
-rw-r--r--. 1 root root 143 Mar 29  2011 runuser
-rw-r--r--. 1 root root 105 Mar 29  2011 runuser-l
-rw-r--r--. 1 root root 145 Jul  8  2010 setup
lrwxrwxrwx. 1 root root  17 Apr 21 07:30 smartcard-auth -> smartcard-auth-ac
-rw-r--r--. 1 root root 711 Apr 21 07:30 smartcard-auth-ac
lrwxrwxrwx. 1 root root  25 Apr 21 07:24 smtp -> /etc/alternatives/mta-pam
-rw-r--r--. 1 root root  76 Apr  6  2011 smtp.postfix
-rw-r--r--. 1 root root 575 May 20  2011 sshd
-rw-r--r--. 1 root root 341 May 20  2011 ssh-keycat
-rw-r--r--. 1 root root 487 Mar 29  2011 su
-rw-r--r--. 1 root root 202 May 20  2011 sudo
-rw-r--r--. 1 root root 187 May 20  2011 sudo-i
-rw-r--r--. 1 root root 137 Mar 29  2011 su-l
lrwxrwxrwx. 1 root root  14 Apr 21 07:30 system-auth -> system-auth-ac
-rw-r--r--. 1 root root 937 Apr 21 07:30 system-auth-ac
-rw-r--r--. 1 root root  97 May 20  2011 system-config-authentication
-rw-r--r--. 1 root root  97 Dec 20  2010 system-config-date
-rw-r--r--. 1 root root  97 Nov 20  2010 system-config-kdump
-rw-r--r--. 1 root root  97 Nov 20  2010 system-config-keyboard
-rw-r--r--. 1 root root  97 Dec 30  2010 system-config-network
-rw-r--r--. 1 root root  97 Dec 30  2010 system-config-network-cmd
-rw-r--r--. 1 root root 118 May 20  2011 system-config-users
lrwxrwxrwx. 1 root root  12 Apr 21 07:24 uln_register -> rhn_register
-rw-r--r--. 1 root root 163 May 22  2011 xserver

popt.d:
total 0

prelink.conf.d:
total 0

profile.d:
total 76
-rw-r--r--. 1 root root 1133 Mar 29  2011 colorls.csh
-rw-r--r--. 1 root root 1143 Mar 29  2011 colorls.sh
-rw-r--r--. 1 root root   80 Jan 27  2011 cvs.csh
-rw-r--r--. 1 root root   78 Jan 27  2011 cvs.sh
-rw-r--r--. 1 root root  192 Mar 29  2011 glib2.csh
-rw-r--r--. 1 root root  192 Mar 29  2011 glib2.sh
-rw-r--r--. 1 root root   58 May 20  2011 gnome-ssh-askpass.csh
-rw-r--r--. 1 root root   70 May 20  2011 gnome-ssh-askpass.sh
-rw-r--r--. 1 root root 1741 May 21  2011 lang.csh
-rw-r--r--. 1 root root 2706 May 21  2011 lang.sh
-rw-r--r--. 1 root root  122 May  4  2010 less.csh
-rw-r--r--. 1 root root  108 May  4  2010 less.sh
-rw-r--r--. 1 root root  990 Jul 25  2010 qt.csh
-rw-r--r--. 1 root root  933 Jul 25  2010 qt.sh
-rw-r--r--. 1 root root 2142 Dec  3  2010 udisks-bash-completion.sh
-rw-r--r--. 1 root root   97 Mar 30  2011 vim.csh
-rw-r--r--. 1 root root  269 Mar 30  2011 vim.sh
-rw-r--r--. 1 root root  161 Apr 30  2010 which2.csh
-rw-r--r--. 1 root root  169 Apr 30  2010 which2.sh

rc.d:
total 60
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 init.d
-rwxr-xr-x. 1 root root  2617 May 21  2011 rc
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 rc0.d
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 rc1.d
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 rc2.d
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 rc3.d
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 rc4.d
drwxr-xr-x. 2 root root  4096 Apr 26 20:46 rc5.d
drwxr-xr-x. 2 root root  4096 Apr 22 13:43 rc6.d
-rwxr-xr-x. 1 root root   220 May 21  2011 rc.local
-rwxr-xr-x. 1 root root 19546 May 21  2011 rc.sysinit

report.d:
total 16
-rw-r--r--. 1 root root 219 Apr  3  2011 ftp.conf
-rw-r--r--. 1 root root 132 Apr  3  2011 localsave.conf
-rw-r--r--. 1 root root 890 Apr  3  2011 RHEL.conf
-rw-r--r--. 1 root root 281 Apr  3  2011 scp.conf

rwtab.d:
total 4
-rw-r--r--. 1 root root 18 May 22  2011 sssd

sane.d:
total 316
-rw-r--r--. 1 root root   25 Jul 25  2010 abaton.conf
-rw-r--r--. 1 root root   14 Jul 25  2010 agfafocus.conf
-rw-r--r--. 1 root root   24 Jul 25  2010 apple.conf
-rw-r--r--. 1 root root   26 Jul 25  2010 artec.conf
-rw-r--r--. 1 root root 4140 Jul 25  2010 artec_eplus48u.conf
-rw-r--r--. 1 root root  543 Jul 25  2010 avision.conf
-rw-r--r--. 1 root root   29 Jul 25  2010 bh.conf
-rw-r--r--. 1 root root  193 Jul 25  2010 canon630u.conf
-rw-r--r--. 1 root root   35 Jul 25  2010 canon.conf
-rw-r--r--. 1 root root 2834 Jul 25  2010 canon_dr.conf
-rw-r--r--. 1 root root 1160 Jul 25  2010 canon_pp.conf
-rw-r--r--. 1 root root  509 Jul 25  2010 cardscan.conf
-rw-r--r--. 1 root root  754 Jul 25  2010 coolscan2.conf
-rw-r--r--. 1 root root  754 Jul 25  2010 coolscan3.conf
-rw-r--r--. 1 root root   34 Jul 25  2010 coolscan.conf
-rw-r--r--. 1 root root  984 Jul 25  2010 dc210.conf
-rw-r--r--. 1 root root  984 Jul 25  2010 dc240.conf
-rw-r--r--. 1 root root  704 Jul 25  2010 dc25.conf
-rw-r--r--. 1 root root  492 Jul 25  2010 dell1600n_net.conf
-rw-r--r--. 1 root root  682 Jul 25  2010 dll.conf
drwxr-xr-x. 2 root root 4096 Jul 25  2010 dll.d
-rw-r--r--. 1 root root   12 Jul 25  2010 dmc.conf
-rw-r--r--. 1 root root 1378 Jul 25  2010 epjitsu.conf
-rw-r--r--. 1 root root  376 Jul 25  2010 epson2.conf
-rw-r--r--. 1 root root  793 Jul 25  2010 epson.conf
-rw-r--r--. 1 root root 1679 Jul 25  2010 fujitsu.conf
-rw-r--r--. 1 root root 1157 Jul 25  2010 genesys.conf
-rw-r--r--. 1 root root 1149 Jul 25  2010 gphoto2.conf
-rw-r--r--. 1 root root 7792 Jul 25  2010 gt68xx.conf
-rw-r--r--. 1 root root  396 Jul 25  2010 hp3900.conf
-rw-r--r--. 1 root root   76 Jul 25  2010 hp4200.conf
-rw-r--r--. 1 root root  238 Jul 25  2010 hp5400.conf
-rw-r--r--. 1 root root  497 Jul 25  2010 hp.conf
-rw-r--r--. 1 root root   22 Jul 25  2010 hpsj5s.conf
-rw-r--r--. 1 root root   24 Jul 25  2010 hs2p.conf
-rw-r--r--. 1 root root   38 Jul 25  2010 ibm.conf
-rw-r--r--. 1 root root  367 Jul 25  2010 kodak.conf
-rw-r--r--. 1 root root  113 Jul 25  2010 leo.conf
-rw-r--r--. 1 root root   96 Jul 25  2010 lexmark.conf
-rw-r--r--. 1 root root  187 Jul 25  2010 ma1509.conf
-rw-r--r--. 1 root root  666 Jul 25  2010 matsushita.conf
-rw-r--r--. 1 root root  279 Jul 25  2010 microtek2.conf
-rw-r--r--. 1 root root  268 Jul 25  2010 microtek.conf
-rw-r--r--. 1 root root 2125 Jul 25  2010 mustek.conf
-rw-r--r--. 1 root root 3824 Jul 25  2010 mustek_pp.conf
-rw-r--r--. 1 root root  809 Jul 25  2010 mustek_usb.conf
-rw-r--r--. 1 root root   13 Jul 25  2010 nec.conf
-rw-r--r--. 1 root root  573 Jul 25  2010 net.conf
-rw-r--r--. 1 root root  365 Jul 25  2010 p5.conf
-rw-r--r--. 1 root root   75 Jul 25  2010 pie.conf
-rw-r--r--. 1 root root  492 Jul 25  2010 pixma.conf
-rw-r--r--. 1 root root 4142 Jul 25  2010 plustek.conf
-rw-r--r--. 1 root root  943 Jul 25  2010 plustek_pp.conf
-rw-r--r--. 1 root root  391 Jul 25  2010 qcam.conf
-rw-r--r--. 1 root root   29 Jul 25  2010 ricoh.conf
-rw-r--r--. 1 root root  183 Jul 25  2010 rts8891.conf
-rw-r--r--. 1 root root   13 Jul 25  2010 s9036.conf
-rw-r--r--. 1 root root 1052 Jul 25  2010 saned.conf
-rw-r--r--. 1 root root   48 Jul 25  2010 sceptre.conf
-rw-r--r--. 1 root root 1464 Jul 25  2010 sharp.conf
-rw-r--r--. 1 root root  115 Jul 25  2010 sm3840.conf
-rw-r--r--. 1 root root 2239 Jul 25  2010 snapscan.conf
-rw-r--r--. 1 root root   10 Jul 25  2010 sp15c.conf
-rw-r--r--. 1 root root 2224 Jul 25  2010 st400.conf
-rw-r--r--. 1 root root  178 Jul 25  2010 stv680.conf
-rw-r--r--. 1 root root   28 Jul 25  2010 tamarack.conf
-rw-r--r--. 1 root root  355 Jul 25  2010 teco1.conf
-rw-r--r--. 1 root root  636 Jul 25  2010 teco2.conf
-rw-r--r--. 1 root root  217 Jul 25  2010 teco3.conf
-rw-r--r--. 1 root root 1807 Jul 25  2010 test.conf
-rw-r--r--. 1 root root 1495 Jul 25  2010 u12.conf
-rw-r--r--. 1 root root  386 Jul 25  2010 umax1220u.conf
-rw-r--r--. 1 root root 3094 Jul 25  2010 umax.conf
-rw-r--r--. 1 root root 1684 Jul 25  2010 umax_pp.conf
-rw-r--r--. 1 root root  204 Jul 25  2010 v4l.conf
-rw-r--r--. 1 root root  399 Jul 25  2010 xerox_mfp.conf

setuptool.d:
total 56
-rw-r--r--. 1 root root 42 Jul  8  2010 98netconfig
-rw-r--r--. 1 root root 69 Jul  8  2010 98system-config-authentication
-rw-r--r--. 1 root root 47 Jul  8  2010 98system-config-display
-rw-r--r--. 1 root root 62 Jul  8  2010 98system-config-keyboard
-rw-r--r--. 1 root root 50 Jul  8  2010 99authconfig
-rw-r--r--. 1 root root 43 Jul  8  2010 99kbdconfig
-rw-r--r--. 1 root root 42 Jul  8  2010 99mouseconfig
-rw-r--r--. 1 root root 33 Jul  8  2010 99ntsysv
-rw-r--r--. 1 root root 46 Jul  8  2010 99printconf-tui
-rw-r--r--. 1 root root 45 Jul  8  2010 99sndconfig
-rw-r--r--. 1 root root 59 Jul  8  2010 99system-config-firewall-tui
-rw-r--r--. 1 root root 58 Jul  8  2010 99system-config-network-tui
-rw-r--r--. 1 root root 44 Jul  8  2010 99timeconfig
-rw-r--r--. 1 root root 40 Jul  8  2010 99Xconfigurator

statetab.d:
total 0
ls: cannot open directory sudoers.d: Permission denied

xinetd.d:
total 52
-rw-r--r--. 1 root root 1157 Mar 30  2011 chargen-dgram
-rw-r--r--. 1 root root 1159 Mar 30  2011 chargen-stream
-rw-r--r--. 1 root root  523 Jan 27  2011 cvs
-rw-r--r--. 1 root root 1157 Mar 30  2011 daytime-dgram
-rw-r--r--. 1 root root 1159 Mar 30  2011 daytime-stream
-rw-r--r--. 1 root root 1157 Mar 30  2011 discard-dgram
-rw-r--r--. 1 root root 1159 Mar 30  2011 discard-stream
-rw-r--r--. 1 root root 1148 Mar 30  2011 echo-dgram
-rw-r--r--. 1 root root 1150 Mar 30  2011 echo-stream
-rw-r--r--. 1 root root  332 Mar 28  2011 rsync
-rw-r--r--. 1 root root 1212 Mar 30  2011 tcpmux-server
-rw-r--r--. 1 root root 1149 Mar 30  2011 time-dgram
-rw-r--r--. 1 root root 1150 Mar 30  2011 time-stream

yum.repos.d:
total 12
-rw-r--r--. 1 root root  183 Apr  1  2011 adobe-linux-x86_64.repo
-rw-r--r--. 1 root root  116 Apr 21 16:44 google-chrome.repo
-rw-r--r--. 1 root root 2887 Apr 22 10:24 public-yum-ol6.repo


Now if you are using a distribution like Fedora you might find this directory in /etc/rc.d/init.d. Regardless of location, it serves the same purpose.

In order to control any of the scripts in init.d manually you have to have root (or sudo) access. Each script will be run as a command and the structure of the command will look like:

/etc/init.d/command OPTION

Where command is the actual command to run and OPTION can be one of the following:
start
stop
reload
restart
force-reload

Most often you will use either start, stop, or restart. So if you want to stop your network you can issue the command:

/etc/init.d/networking stop

Or if you make a change to your network and need to restart it, you could do so with the following command:

/etc/init.d/networking restart

Some of the more common init scripts in this directory are:

networking
samba
apache2
ftpd
sshd
dovecot
mysql

Of course there may be more often-used scripts in your directory - it depends upon what you have installed. The above list was taken from a Ubuntu Server 8.10 installation so a standard desktop installation would have a few less networking-type scripts.

But what about /etc/rc.local

There is a third option that I used to use quite a bit. This option is the /etc/rc.local script. This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup. Many times I will place mounting instructions for things like nfs in this script. This is also a good place to place "troubleshooting" scripts in. For instance, once I had a machine that, for some reason, samba seemed to not want to start. Even afer checking to make sure the Samba daemon was setup to initialize at boot up. So instead of spending all of my time up front with this I simply placed the line:

/etc/init.d/samba start

in the /etc/rc.local script and Samba worked like a charm. Eventually I would come back and trouble shoot this issue.

Linux is flexible. Linux is so flexible there is almost, inevitably, numerous ways to solve a single problem. Starting a system service is one such issue. With the help of the /etc/init.d system (as well as /etc/rc.local) you can pretty much rest assured your service will start.

Sample .bashrc

posted May 4, 2013, 9:19 AM by Sachchida Ojha   [ updated May 4, 2013, 9:47 AM ]

my sample .bashrc file. 
# PERSONAL $HOME/.bashrc FILE for bash-3.0 (or later)
#  This file is normally read by interactive shells only.
#+ Here is the place to define your aliases, functions and
#+ other interactive features like your prompt.
#
#  The majority of the code here assumes you are on a GNU
#+ system (most likely a Linux box) and is often based on code
#+ found on Usenet or Internet.
#
#  See for instance:
#  http://tldp.org/LDP/abs/html/index.html
#  http://www.caliban.org/bash
#  http://www.shelldorado.com/scripts/categories.html
#  http://www.dotfiles.org
#
#  The choice of colors was done for a shell with a dark background
#+ (white on black), and this is usually also suited for pure text-mode
#+ consoles (no X server available). If you use a white background,
#+ you'll have to do some other choices for readability.
#
#  This bashrc file is a bit overcrowded.
#  Remember, it is just just an example.
#  Tailor it to your needs.
#
# =============================================================== #


# If not running interactively, don't do anything
[ -z "$PS1" ] && return


#-------------------------------------------------------------
# Source global definitions (if any)
#-------------------------------------------------------------


if [ -f /etc/bashrc ]; then
      . /etc/bashrc   # --> Read /etc/bashrc, if present.
fi


#--------------------------------------------------------------
#  Automatic setting of $DISPLAY (if not set already).
#  This works for me - your mileage may vary. . . .
#  The problem is that different types of terminals give
#+ different answers to 'who am i' (rxvt in particular can be
#+ troublesome) - however this code seems to work in a majority
#+ of cases.
#--------------------------------------------------------------

function get_xserver ()
{
    case $TERM in
        xterm )
            XSERVER=$(who am i | awk '{print $NF}' | tr -d ')''(' )
            # Ane-Pieter Wieringa suggests the following alternative:
            #  I_AM=$(who am i)
            #  SERVER=${I_AM#*(}
            #  SERVER=${SERVER%*)}
            XSERVER=${XSERVER%%:*}
            ;;
            aterm | rxvt)
            # Find some code that works here. ...
            ;;
    esac
}

if [ -z ${DISPLAY:=""} ]; then
    get_xserver
    if [[ -z ${XSERVER}  || ${XSERVER} == $(hostname) ||
       ${XSERVER} == "unix" ]]; then
          DISPLAY=":0.0"          # Display on local host.
    else
       DISPLAY=${XSERVER}:0.0     # Display on remote host.
    fi
fi

export DISPLAY

#-------------------------------------------------------------
# Some settings
#-------------------------------------------------------------

#set -o nounset     # These  two options are useful for debugging.
#set -o xtrace
alias debug="set -o nounset; set -o xtrace"

ulimit -S -c 0      # Don't want coredumps.
set -o notify
set -o noclobber
set -o ignoreeof


# Enable options:
shopt -s cdspell
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s sourcepath
shopt -s no_empty_cmd_completion
shopt -s cmdhist
shopt -s histappend histreedit histverify
shopt -s extglob       # Necessary for programmable completion.

# Disable options:
shopt -u mailwarn
unset MAILCHECK        # Don't want my shell to warn me of incoming mail.


#-------------------------------------------------------------
# Greeting, motd etc. ...
#-------------------------------------------------------------

# Color definitions (taken from Color Bash Prompt HowTo).
# Some colors might look different of some terminals.
# For example, I see 'Bold Red' as 'orange' on my screen,
# hence the 'Green' 'BRed' 'Red' sequence I often use in my prompt.


# Normal Colors
Black='\e[0;30m'        # Black
Red='\e[0;31m'          # Red
Green='\e[0;32m'        # Green
Yellow='\e[0;33m'       # Yellow
Blue='\e[0;34m'         # Blue
Purple='\e[0;35m'       # Purple
Cyan='\e[0;36m'         # Cyan
White='\e[0;37m'        # White

# Bold
BBlack='\e[1;30m'       # Black
BRed='\e[1;31m'         # Red
BGreen='\e[1;32m'       # Green
BYellow='\e[1;33m'      # Yellow
BBlue='\e[1;34m'        # Blue
BPurple='\e[1;35m'      # Purple
BCyan='\e[1;36m'        # Cyan
BWhite='\e[1;37m'       # White

# Background
On_Black='\e[40m'       # Black
On_Red='\e[41m'         # Red
On_Green='\e[42m'       # Green
On_Yellow='\e[43m'      # Yellow
On_Blue='\e[44m'        # Blue
On_Purple='\e[45m'      # Purple
On_Cyan='\e[46m'        # Cyan
On_White='\e[47m'       # White

NC="\e[m"               # Color Reset


ALERT=${BWhite}${On_Red} # Bold White on red background



echo -e "${BCyan}This is BASH ${BRed}${BASH_VERSION%.*}${BCyan}\
- DISPLAY on ${BRed}$DISPLAY${NC}\n"
date
if [ -x /usr/games/fortune ]; then
    /usr/games/fortune -s     # Makes our day a bit more fun.... :-)
fi

function _exit()              # Function to run upon exit of shell.
{
    echo -e "${BRed}Hasta la vista, baby${NC}"
}
trap _exit EXIT

#-------------------------------------------------------------
# Shell Prompt - for many examples, see:
#       http://www.debian-administration.org/articles/205
#       http://www.askapache.com/linux/bash-power-prompt.html
#       http://tldp.org/HOWTO/Bash-Prompt-HOWTO
#       https://github.com/nojhan/liquidprompt
#-------------------------------------------------------------
# Current Format: [TIME USER@HOST PWD] >
# TIME:
#    Green     == machine load is low
#    Orange    == machine load is medium
#    Red       == machine load is high
#    ALERT     == machine load is very high
# USER:
#    Cyan      == normal user
#    Orange    == SU to user
#    Red       == root
# HOST:
#    Cyan      == local session
#    Green     == secured remote connection (via ssh)
#    Red       == unsecured remote connection
# PWD:
#    Green     == more than 10% free disk space
#    Orange    == less than 10% free disk space
#    ALERT     == less than 5% free disk space
#    Red       == current user does not have write privileges
#    Cyan      == current filesystem is size zero (like /proc)
# >:
#    White     == no background or suspended jobs in this shell
#    Cyan      == at least one background job in this shell
#    Orange    == at least one suspended job in this shell
#
#    Command is added to the history file each time you hit enter,
#    so it's available to all shells (using 'history -a').


# Test connection type:
if [ -n "${SSH_CONNECTION}" ]; then
    CNX=${Green}        # Connected on remote machine, via ssh (good).
elif [[ "${DISPLAY%%:0*}" != "" ]]; then
    CNX=${ALERT}        # Connected on remote machine, not via ssh (bad).
else
    CNX=${BCyan}        # Connected on local machine.
fi

# Test user type:
if [[ ${USER} == "root" ]]; then
    SU=${Red}           # User is root.
elif [[ ${USER} != $(logname) ]]; then
    SU=${BRed}          # User is not login user.
else
    SU=${BCyan}         # User is normal (well ... most of us are).
fi



NCPU=$(grep -c 'processor' /proc/cpuinfo)    # Number of CPUs
SLOAD=$(( 100*${NCPU} ))        # Small load
MLOAD=$(( 200*${NCPU} ))        # Medium load
XLOAD=$(( 400*${NCPU} ))        # Xlarge load

# Returns system load as percentage, i.e., '40' rather than '0.40)'.
function load()
{
    local SYSLOAD=$(cut -d " " -f1 /proc/loadavg | tr -d '.')
    # System load of the current host.
    echo $((10#$SYSLOAD))       # Convert to decimal.
}

# Returns a color indicating system load.
function load_color()
{
    local SYSLOAD=$(load)
    if [ ${SYSLOAD} -gt ${XLOAD} ]; then
        echo -en ${ALERT}
    elif [ ${SYSLOAD} -gt ${MLOAD} ]; then
        echo -en ${Red}
    elif [ ${SYSLOAD} -gt ${SLOAD} ]; then
        echo -en ${BRed}
    else
        echo -en ${Green}
    fi
}

# Returns a color according to free disk space in $PWD.
function disk_color()
{
    if [ ! -w "${PWD}" ] ; then
        echo -en ${Red}
        # No 'write' privilege in the current directory.
    elif [ -s "${PWD}" ] ; then
        local used=$(command df -P "$PWD" |
                   awk 'END {print $5} {sub(/%/,"")}')
        if [ ${used} -gt 95 ]; then
            echo -en ${ALERT}           # Disk almost full (>95%).
        elif [ ${used} -gt 90 ]; then
            echo -en ${BRed}            # Free disk space almost gone.
        else
            echo -en ${Green}           # Free disk space is ok.
        fi
    else
        echo -en ${Cyan}
        # Current directory is size '0' (like /proc, /sys etc).
    fi
}

# Returns a color according to running/suspended jobs.
function job_color()
{
    if [ $(jobs -s | wc -l) -gt "0" ]; then
        echo -en ${BRed}
    elif [ $(jobs -r | wc -l) -gt "0" ] ; then
        echo -en ${BCyan}
    fi
}

# Adds some text in the terminal frame (if applicable).


# Now we construct the prompt.
PROMPT_COMMAND="history -a"
case ${TERM} in
  *term | rxvt | linux)
        PS1="\[\$(load_color)\][\A\[${NC}\] "
        # Time of day (with load info):
        PS1="\[\$(load_color)\][\A\[${NC}\] "
        # User@Host (with connection type info):
        PS1=${PS1}"\[${SU}\]\u\[${NC}\]@\[${CNX}\]\h\[${NC}\] "
        # PWD (with 'disk space' info):
        PS1=${PS1}"\[\$(disk_color)\]\W]\[${NC}\] "
        # Prompt (with 'job' info):
        PS1=${PS1}"\[\$(job_color)\]>\[${NC}\] "
        # Set title of current xterm:
        PS1=${PS1}"\[\e]0;[\u@\h] \w\a\]"
        ;;
    *)
        PS1="(\A \u@\h \W) > " # --> PS1="(\A \u@\h \w) > "
                               # --> Shows full pathname of current dir.
        ;;
esac



export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n'
export HISTIGNORE="&:bg:fg:ll:h"
export HISTTIMEFORMAT="$(echo -e ${BCyan})[%d/%m %H:%M:%S]$(echo -e ${NC}) "
export HISTCONTROL=ignoredups
export HOSTFILE=$HOME/.hosts    # Put a list of remote hosts in ~/.hosts


#============================================================
#
#  ALIASES AND FUNCTIONS
#
#  Arguably, some functions defined here are quite big.
#  If you want to make this file smaller, these functions can
#+ be converted into scripts and removed from here.
#
#============================================================

#-------------------
# Personnal Aliases
#-------------------

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# -> Prevents accidentally clobbering files.
alias mkdir='mkdir -p'

alias h='history'
alias j='jobs -l'
alias which='type -a'
alias ..='cd ..'

# Pretty-print of some PATH variables:
alias path='echo -e ${PATH//:/\\n}'
alias libpath='echo -e ${LD_LIBRARY_PATH//:/\\n}'


alias du='du -kh'    # Makes a more readable output.
alias df='df -kTh'

#-------------------------------------------------------------
# The 'ls' family (this assumes you use a recent GNU ls).
#-------------------------------------------------------------
# Add colors for filetype and  human-readable sizes by default on 'ls':
alias ls='ls -h --color'
alias lx='ls -lXB'         #  Sort by extension.
alias lk='ls -lSr'         #  Sort by size, biggest last.
alias lt='ls -ltr'         #  Sort by date, most recent last.
alias lc='ls -ltcr'        #  Sort by/show change time,most recent last.
alias lu='ls -ltur'        #  Sort by/show access time,most recent last.

# The ubiquitous 'll': directories first, with alphanumeric sorting:
alias ll="ls -lv --group-directories-first"
alias lm='ll |more'        #  Pipe through 'more'
alias lr='ll -R'           #  Recursive ls.
alias la='ll -A'           #  Show hidden files.
alias tree='tree -Csuh'    #  Nice alternative to 'recursive ls' ...


#-------------------------------------------------------------
# Tailoring 'less'
#-------------------------------------------------------------

alias more='less'
export PAGER=less
export LESSCHARSET='latin1'
export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-'
                # Use this if lesspipe.sh exists.
export LESS='-i -N -w  -z-4 -g -e -M -X -F -R -P%t?f%f \
:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'

# LESS man page colors (makes Man pages more readable).
export LESS_TERMCAP_mb=$'\E[01;31m'
export LESS_TERMCAP_md=$'\E[01;31m'
export LESS_TERMCAP_me=$'\E[0m'
export LESS_TERMCAP_se=$'\E[0m'
export LESS_TERMCAP_so=$'\E[01;44;33m'
export LESS_TERMCAP_ue=$'\E[0m'
export LESS_TERMCAP_us=$'\E[01;32m'


#-------------------------------------------------------------
# Spelling typos - highly personnal and keyboard-dependent :-)
#-------------------------------------------------------------

alias xs='cd'
alias vf='cd'
alias moer='more'
alias moew='more'
alias kk='ll'


#-------------------------------------------------------------
# A few fun ones
#-------------------------------------------------------------

# Adds some text in the terminal frame (if applicable).

function xtitle()
{
    case "$TERM" in
    *term* | rxvt)
        echo -en  "\e]0;$*\a" ;;
    *)  ;;
    esac
}


# Aliases that use xtitle
alias top='xtitle Processes on $HOST && top'
alias make='xtitle Making $(basename $PWD) ; make'

# .. and functions
function man()
{
    for i ; do
        xtitle The $(basename $1|tr -d .[:digit:]) manual
        command man -a "$i"
    done
}


#-------------------------------------------------------------
# Make the following commands run in background automatically:
#-------------------------------------------------------------

function te()  # wrapper around xemacs/gnuserv
{
    if [ "$(gnuclient -batch -eval t 2>&-)" == "t" ]; then
       gnuclient -q "$@";
    else
       ( xemacs "$@" &);
    fi
}

function soffice() { command soffice "$@" & }
function firefox() { command firefox "$@" & }
function xpdf() { command xpdf "$@" & }


#-------------------------------------------------------------
# File & strings related functions:
#-------------------------------------------------------------


# Find a file with a pattern in name:
function ff() { find . -type f -iname '*'"$*"'*' -ls ; }

# Find a file with pattern $1 in name and Execute $2 on it:
function fe() { find . -type f -iname '*'"${1:-}"'*' \
-exec ${2:-file} {} \;  ; }

#  Find a pattern in a set of files and highlight them:
#+ (needs a recent version of egrep).
function fstr()
{
    OPTIND=1
    local mycase=""
    local usage="fstr: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
    while getopts :it opt
    do
        case "$opt" in
           i) mycase="-i " ;;
           *) echo "$usage"; return ;;
        esac
    done
    shift $(( $OPTIND - 1 ))
    if [ "$#" -lt 1 ]; then
        echo "$usage"
        return;
    fi
    find . -type f -name "${2:-*}" -print0 | \
xargs -0 egrep --color=always -sn ${case} "$1" 2>&- | more

}


function swap()
{ # Swap 2 filenames around, if they exist (from Uzi's bashrc).
    local TMPFILE=tmp.$$

    [ $# -ne 2 ] && echo "swap: 2 arguments needed" && return 1
    [ ! -e $1 ] && echo "swap: $1 does not exist" && return 1
    [ ! -e $2 ] && echo "swap: $2 does not exist" && return 1

    mv "$1" $TMPFILE
    mv "$2" "$1"
    mv $TMPFILE "$2"
}

function extract()      # Handy Extract Program
{
    if [ -f $1 ] ; then
        case $1 in
            *.tar.bz2)   tar xvjf $1     ;;
            *.tar.gz)    tar xvzf $1     ;;
            *.bz2)       bunzip2 $1      ;;
            *.rar)       unrar x $1      ;;
            *.gz)        gunzip $1       ;;
            *.tar)       tar xvf $1      ;;
            *.tbz2)      tar xvjf $1     ;;
            *.tgz)       tar xvzf $1     ;;
            *.zip)       unzip $1        ;;
            *.Z)         uncompress $1   ;;
            *.7z)        7z x $1         ;;
            *)           echo "'$1' cannot be extracted via >extract<" ;;
        esac
    else
        echo "'$1' is not a valid file!"
    fi
}


# Creates an archive (*.tar.gz) from given directory.
function maketar() { tar cvzf "${1%%/}.tar.gz"  "${1%%/}/"; }

# Create a ZIP archive of a file or folder.
function makezip() { zip -r "${1%%/}.zip" "$1" ; }

# Make your directories and files access rights sane.
function sanitize() { chmod -R u=rwX,g=rX,o= "$@" ;}

#-------------------------------------------------------------
# Process/system related functions:
#-------------------------------------------------------------


function my_ps() { ps $@ -u $USER -o pid,%cpu,%mem,bsdtime,command ; }
function pp() { my_ps f | awk '!/awk/ && $0~var' var=${1:-".*"} ; }


function killps()   # kill by process name
{
    local pid pname sig="-TERM"   # default signal
    if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
        echo "Usage: killps [-SIGNAL] pattern"
        return;
    fi
    if [ $# = 2 ]; then sig=$1 ; fi
    for pid in $(my_ps| awk '!/awk/ && $0~pat { print $1 }' pat=${!#} )
    do
        pname=$(my_ps | awk '$1~var { print $5 }' var=$pid )
        if ask "Kill process $pid <$pname> with signal $sig?"
            then kill $sig $pid
        fi
    done
}

function mydf()         # Pretty-print of 'df' output.
{                       # Inspired by 'dfc' utility.
    for fs ; do

        if [ ! -d $fs ]
        then
          echo -e $fs" :No such file or directory" ; continue
        fi

        local info=( $(command df -P $fs | awk 'END{ print $2,$3,$5 }') )
        local free=( $(command df -Pkh $fs | awk 'END{ print $4 }') )
        local nbstars=$(( 20 * ${info[1]} / ${info[0]} ))
        local out="["
        for ((j=0;j<20;j++)); do
            if [ ${j} -lt ${nbstars} ]; then
               out=$out"*"
            else
               out=$out"-"
            fi
        done
        out=${info[2]}" "$out"] ("$free" free on "$fs")"
        echo -e $out
    done
}


function my_ip() # Get IP adress on ethernet.
{
    MY_IP=$(/sbin/ifconfig eth0 | awk '/inet/ { print $2 } ' |
      sed -e s/addr://)
    echo ${MY_IP:-"Not connected"}
}

function ii()   # Get current host related info.
{
    echo -e "\nYou are logged on ${BRed}$HOST"
    echo -e "\n${BRed}Additionnal information:$NC " ; uname -a
    echo -e "\n${BRed}Users logged on:$NC " ; w -hs |
             cut -d " " -f1 | sort | uniq
    echo -e "\n${BRed}Current date :$NC " ; date
    echo -e "\n${BRed}Machine stats :$NC " ; uptime
    echo -e "\n${BRed}Memory stats :$NC " ; free
    echo -e "\n${BRed}Diskspace :$NC " ; mydf / $HOME
    echo -e "\n${BRed}Local IP Address :$NC" ; my_ip
    echo -e "\n${BRed}Open connections :$NC "; netstat -pan --inet;
    echo
}

#-------------------------------------------------------------
# Misc utilities:
#-------------------------------------------------------------

function repeat()       # Repeat n times command.
{
    local i max
    max=$1; shift;
    for ((i=1; i <= max ; i++)); do  # --> C-like syntax
        eval "$@";
    done
}


function ask()          # See 'killps' for example of use.
{
    echo -n "$@" '[y/n] ' ; read ans
    case "$ans" in
        y*|Y*) return 0 ;;
        *) return 1 ;;
    esac
}

function corename()   # Get name of app that created a corefile.
{
    for file ; do
        echo -n $file : ; gdb --core=$file --batch | head -1
    done
}



#=========================================================================
#
#  PROGRAMMABLE COMPLETION SECTION
#  Most are taken from the bash 2.05 documentation and from Ian McDonald's
# 'Bash completion' package (http://www.caliban.org/bash/#completion)
#  You will in fact need bash more recent then 3.0 for some features.
#
#  Note that most linux distributions now provide many completions
# 'out of the box' - however, you might need to make your own one day,
#  so I kept those here as examples.
#=========================================================================

if [ "${BASH_VERSION%.*}" \< "3.0" ]; then
    echo "You will need to upgrade to version 3.0 for full \
          programmable completion features"
    return
fi

shopt -s extglob        # Necessary.

complete -A hostname   rsh rcp telnet rlogin ftp ping disk
complete -A export     printenv
complete -A variable   export local readonly unset
complete -A enabled    builtin
complete -A alias      alias unalias
complete -A function   function
complete -A user       su mail finger

complete -A helptopic  help     # Currently same as builtins.
complete -A shopt      shopt
complete -A stopped -P '%' bg
complete -A job -P '%'     fg jobs disown

complete -A directory  mkdir rmdir
complete -A directory   -o default cd

# Compression
complete -f -o default -X '*.+(zip|ZIP)'  zip
complete -f -o default -X '!*.+(zip|ZIP)' unzip
complete -f -o default -X '*.+(z|Z)'      compress
complete -f -o default -X '!*.+(z|Z)'     uncompress
complete -f -o default -X '*.+(gz|GZ)'    gzip
complete -f -o default -X '!*.+(gz|GZ)'   gunzip
complete -f -o default -X '*.+(bz2|BZ2)'  bzip2
complete -f -o default -X '!*.+(bz2|BZ2)' bunzip2
complete -f -o default -X '!*.+(zip|ZIP|z|Z|gz|GZ|bz2|BZ2)' extract


# Documents - Postscript,pdf,dvi.....
complete -f -o default -X '!*.+(ps|PS)'  gs ghostview ps2pdf ps2ascii
complete -f -o default -X \
'!*.+(dvi|DVI)' dvips dvipdf xdvi dviselect dvitype
complete -f -o default -X '!*.+(pdf|PDF)' acroread pdf2ps
complete -f -o default -X '!*.@(@(?(e)ps|?(E)PS|pdf|PDF)?\
(.gz|.GZ|.bz2|.BZ2|.Z))' gv ggv
complete -f -o default -X '!*.texi*' makeinfo texi2dvi texi2html texi2pdf
complete -f -o default -X '!*.tex' tex latex slitex
complete -f -o default -X '!*.lyx' lyx
complete -f -o default -X '!*.+(htm*|HTM*)' lynx html2ps
complete -f -o default -X \
'!*.+(doc|DOC|xls|XLS|ppt|PPT|sx?|SX?|csv|CSV|od?|OD?|ott|OTT)' soffice

# Multimedia
complete -f -o default -X \
'!*.+(gif|GIF|jp*g|JP*G|bmp|BMP|xpm|XPM|png|PNG)' xv gimp ee gqview
complete -f -o default -X '!*.+(mp3|MP3)' mpg123 mpg321
complete -f -o default -X '!*.+(ogg|OGG)' ogg123
complete -f -o default -X \
'!*.@(mp[23]|MP[23]|ogg|OGG|wav|WAV|pls|\
m3u|xm|mod|s[3t]m|it|mtm|ult|flac)' xmms
complete -f -o default -X '!*.@(mp?(e)g|MP?(E)G|wma|avi|AVI|\
asf|vob|VOB|bin|dat|vcd|ps|pes|fli|viv|rm|ram|yuv|mov|MOV|qt|\
QT|wmv|mp3|MP3|ogg|OGG|ogm|OGM|mp4|MP4|wav|WAV|asx|ASX)' xine



complete -f -o default -X '!*.pl'  perl perl5


#  This is a 'universal' completion function - it works when commands have
#+ a so-called 'long options' mode , ie: 'ls --all' instead of 'ls -a'
#  Needs the '-o' option of grep
#+ (try the commented-out version if not available).

#  First, remove '=' from completion word separators
#+ (this will allow completions like 'ls --color=auto' to work correctly).

COMP_WORDBREAKS=${COMP_WORDBREAKS/=/}


_get_longopts()
{
  #$1 --help | sed  -e '/--/!d' -e 's/.*--\([^[:space:].,]*\).*/--\1/'| \
  #grep ^"$2" |sort -u ;
    $1 --help | grep -o -e "--[^[:space:].,]*" | grep -e "$2" |sort -u
}

_longopts()
{
    local cur
    cur=${COMP_WORDS[COMP_CWORD]}

    case "${cur:-*}" in
       -*)      ;;
        *)      return ;;
    esac

    case "$1" in
       \~*)     eval cmd="$1" ;;
         *)     cmd="$1" ;;
    esac
    COMPREPLY=( $(_get_longopts ${1} ${cur} ) )
}
complete  -o default -F _longopts configure bash
complete  -o default -F _longopts wget id info a2ps ls recode

_tar()
{
    local cur ext regex tar untar

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}

    # If we want an option, return the possible long options.
    case "$cur" in
        -*)     COMPREPLY=( $(_get_longopts $1 $cur ) ); return 0;;
    esac

    if [ $COMP_CWORD -eq 1 ]; then
        COMPREPLY=( $( compgen -W 'c t x u r d A' -- $cur ) )
        return 0
    fi

    case "${COMP_WORDS[1]}" in
        ?(-)c*f)
            COMPREPLY=( $( compgen -f $cur ) )
            return 0
            ;;
        +([^Izjy])f)
            ext='tar'
            regex=$ext
            ;;
        *z*f)
            ext='tar.gz'
            regex='t\(ar\.\)\(gz\|Z\)'
            ;;
        *[Ijy]*f)
            ext='t?(ar.)bz?(2)'
            regex='t\(ar\.\)bz2\?'
            ;;
        *)
            COMPREPLY=( $( compgen -f $cur ) )
            return 0
            ;;

    esac

    if [[ "$COMP_LINE" == tar*.$ext' '* ]]; then
        # Complete on files in tar file.
        #
        # Get name of tar file from command line.
        tar=$( echo "$COMP_LINE" | \
                        sed -e 's|^.* \([^ ]*'$regex'\) .*$|\1|' )
        # Devise how to untar and list it.
        untar=t${COMP_WORDS[1]//[^Izjyf]/}

        COMPREPLY=( $( compgen -W "$( echo $( tar $untar $tar \
                                2>/dev/null ) )" -- "$cur" ) )
        return 0

    else
        # File completion on relevant files.
        COMPREPLY=( $( compgen -G $cur\*.$ext ) )

    fi

    return 0

}

complete -F _tar -o default tar

_make()
{
    local mdef makef makef_dir="." makef_inc gcmd cur prev i;
    COMPREPLY=();
    cur=${COMP_WORDS[COMP_CWORD]};
    prev=${COMP_WORDS[COMP_CWORD-1]};
    case "$prev" in
        -*f)
            COMPREPLY=($(compgen -f $cur ));
            return 0
            ;;
    esac;
    case "$cur" in
        -*)
            COMPREPLY=($(_get_longopts $1 $cur ));
            return 0
            ;;
    esac;

    # ... make reads
    #          GNUmakefile,
    #     then makefile
    #     then Makefile ...
    if [ -f ${makef_dir}/GNUmakefile ]; then
        makef=${makef_dir}/GNUmakefile
    elif [ -f ${makef_dir}/makefile ]; then
        makef=${makef_dir}/makefile
    elif [ -f ${makef_dir}/Makefile ]; then
        makef=${makef_dir}/Makefile
    else
       makef=${makef_dir}/*.mk         # Local convention.
    fi


    #  Before we scan for targets, see if a Makefile name was
    #+ specified with -f.
    for (( i=0; i < ${#COMP_WORDS[@]}; i++ )); do
        if [[ ${COMP_WORDS[i]} == -f ]]; then
            # eval for tilde expansion
            eval makef=${COMP_WORDS[i+1]}
            break
        fi
    done
    [ ! -f $makef ] && return 0

    # Deal with included Makefiles.
    makef_inc=$( grep -E '^-?include' $makef |
                 sed -e "s,^.* ,"$makef_dir"/," )
    for file in $makef_inc; do
        [ -f $file ] && makef="$makef $file"
    done


    #  If we have a partial word to complete, restrict completions
    #+ to matches of that word.
    if [ -n "$cur" ]; then gcmd='grep "^$cur"' ; else gcmd=cat ; fi

    COMPREPLY=( $( awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ \
                               {split($1,A,/ /);for(i in A)print A[i]}' \
                                $makef 2>/dev/null | eval $gcmd  ))

}

complete -F _make -X '+($*|*.[cho])' make gmake pmake




_killall()
{
    local cur prev
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}

    #  Get a list of processes
    #+ (the first sed evaluation
    #+ takes care of swapped out processes, the second
    #+ takes care of getting the basename of the process).
    COMPREPLY=( $( ps -u $USER -o comm  | \
        sed -e '1,1d' -e 's#[]\[]##g' -e 's#^.*/##'| \
        awk '{if ($0 ~ /^'$cur'/) print $0}' ))

    return 0
}

complete -F _killall killall killps



# Local Variables:
# mode:shell-script
# sh-shell:bash
# End:

bash shell Startup Files

posted May 4, 2013, 9:00 AM by Sachchida Ojha

Invoked as an interactive login shell, or with --login

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout, if it exists.
Invoked as an interactive non-login shell

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile fileoption will force Bash to read and execute commands from file instead of ~/.bashrc.

So, typically, your ~/.bash_profile contains the line
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi



after (or before) any login-specific initializations.
Invoked non-interactively

When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi

but the value of the PATH variable is not used to search for the file name.

As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.
Invoked with name sh

If Bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

When invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first attempts to read and execute commands from /etc/profile and ~/.profile, in that order. The --noprofile option may be used to inhibit this behavior. When invoked as an interactive shell with the name sh, Bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files.

When invoked as sh, Bash enters POSIX mode after the startup files are read.

Difference between .bashrc and .bash_profile when you loginto unix system with default bash shell

posted May 4, 2013, 8:52 AM by Sachchida Ojha

This is also called a login or non-login shell

When you login (eg: type username and password) via console, either physically sitting at the machine when booting, or remotely via ssh: .bash_profile is executed to configure things before the initial command prompt.

But, if you've already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

alias script='cd /home/sachi/scripts'
alias download='cd /home/sachi/Downloads'

## pass options to free ## 
alias meminfo='free -m -l -t'

## get top process eating memory
alias psmem='ps auxf | sort -nr -k 4'
alias psmem10='ps auxf | sort -nr -k 4 | head -10'

## get top process eating cpu ##
alias pscpu='ps auxf | sort -nr -k 3'
alias pscpu10='ps auxf | sort -nr -k 3 | head -10'

## Get server cpu info ##
alias cpuinfo='lscpu'

## older system use /proc/cpuinfo ##
##alias cpuinfo='less /proc/cpuinfo' ##

## get GPU ram on desktop / laptop## 
alias gpumeminfo='grep -i --color memory /var/log/Xorg.0.log'

alias df='df -H'
alias du='du -ch'

# top is atop, just like vi is vim
alias top='atop'

alias update='yum update'
alias updatey='yum -y update'

alias iptlist='/sbin/iptables -L -n -v --line-numbers'
alias iptlistin='/sbin/iptables -L INPUT -n -v --line-numbers'
alias iptlistout='/sbin/iptables -L OUTPUT -n -v --line-numbers'
alias iptlistfw='/sbin/iptables -L FORWARD -n -v --line-numbers'
alias firewall=iptlist

# show open ports

alias ports='netstat -tulanp'

alias path='echo -e ${PATH//:/\\n}'
alias now='date +"%T'
alias nowtime=now
alias nowdate='date +"%d-%m-%Y"'

alias h='history'
alias j='jobs -l'
alias mount='mount |column -t'

alias mkdir='mkdir -pv'

# start a calculator

alias bc='bc -l'

alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'

## get rid of command not found ##
alias cd..='cd ..'

## a quick way to get out of current directory ##
alias ..='cd ..'
alias ...='cd ../../../'
alias ....='cd ../../../../'
alias .....='cd ../../../../'
alias .4='cd ../../../../'
alias .5='cd ../../../../..'

## Colorize the ls output ##
alias ls='ls --color=auto'

## Use a long listing format ##
alias ll='ls -la'

## Show hidden files ##
alias l.='ls -d .* --color=auto'

Upper to Lower Case in Vi

posted Apr 12, 2013, 2:20 PM by Sachchida Ojha   [ updated Apr 12, 2013, 2:34 PM ]

Oracle database all table names and schema name are in upper case but when you run the same file in Greenplum , all table names and schema names are in lower case.  For example I have a parameter files that contains schema in and table name for HR schema from oracle

oraclehr.txt

HR EMPLOYEES
HR DEPARTMENTS
HR LOCATIONS

In Greenplum we want it to be

hr employees
hr departments
hr locations.

to do this, you need to open the oraclehr.txt in vi and run

:%s/.*/\L&/

Conversely, :%s/.*/\U&/ will change all the characters to uppercase.

also

Open the file in vi

press Esc then

for uppercase: gggUG
for lowercase: ggguG


1-6 of 6