Tuesday, June 18, 2013

CentOS / Redhat : Configure CentOS as a Software Router with two interfaces


Linux can be easily configured to share an internet connection using iptables. Al you need to two network interface cards as follows:
a) Your internal (LAN) network connected via eth0 with static ip address 192.168.0.1
b) Your external WAN) network is connected via eth1 with static ip address 10.10.10.1  ( public IP provided by ISP )
Please note that interface eth1 may have public IP address or IP assigned by ISP. eth1 may be connected to a dedicated DSL / ADSL / WAN / Cable router:

Step # 1: Enable Packet Forwarding

Login as the root user. Open /etc/sysctl.conf file
# vi /etc/sysctl.conf

Add the following line to enable packet forwarding for IPv4:
net.ipv4.conf.default.forwarding=1

Save and close the file. Restart networking:
# service network restart

Step # 2: Enable IP masquerading


In Linux networking, Network Address Translation (NAT) or Network Masquerading (IP Masquerading) is a technique of transceiving network traffic through a router that involves re-writing the source and/or destination IP addresses and usually also the TCP/UDP port numbers of IP packets as they pass through. In short, IP masquerading is used to share the internet connection.

Share internet connection

To share network connection via eth1, enter the following rule at command prompt (following useful for ppp0 or dial up connection):

# service iptables stop
# iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
# service iptables save
# service iptables restart

Make sure Iptables runs on boot

# chkconfig iptables on


Open your Windows / Mac / Linux computer networking GUI tool and point router IP to 192.168.0.1 (eth0 Linux IP). You also need to setup DNS IP such as 8.8.8.8 or to your local DNS server IP. You should now able to ping or browse the internet:

# ping google.com


CentOS | RHEL: Check If A Service Is Running Or Not


We can use service command to get the status of services running on the system. It runs a System V init script in as predictable environment as possible, removing most environment variables and with current working directory set to /.

Syntax is:

service <service-name> status

OR

/etc/init.d/<service-name> status

Example:

Find, out if a service called httpd (Apache Web Server) is running on CentOS OR RHEL. Open a terminal or login using ssh, enter:

  #  service httpd status

Sample outputs:

[root@myserver ~]# service httpd status

httpd (pid  21585) is running...

Find out status of all services

The service --status-all command runs all init scripts, in alphabetical order, with the status command:
 #  service --status-all
Sample output:-


[root@myserver ~]# service --status-all

anacron is stopped

atd is stopped

auditd is stopped

cpuspeed is stopped

crond (pid 3442) is running...

cupsd (pid 5004) is running...

gpm (pid 3316) is running...

hald is stopped

httpd is stopped

ipmi_msghandler module not loaded.

ipmi_si module not loaded.

ipmi_devintf module not loaded.

/dev/ipmi0 does not exist.

Firewall is stopped.

irqbalance (pid 3031) is running...

Usage: jboss {start|stop|restart}

Usage: jboss {start|stop|restart}

mdmpd is stopped

dbus-daemon-1 is stopped

/etc/init.d/microcode_ctl: reading microcode status is not yet supported

Server address not specified in /etc/sysconfig/netdump

netplugd is stopped

Configured devices:

lo eth0

Currently active devices:

lo eth0

NetworkManager is stopped

nscd is stopped

ntpd (pid 3117) is running...

rhnsd (pid 3498) is running...

saslauthd is stopped

sendmail (pid 3145 3135) is running...

smartd is stopped

snmpd (pid 5035) is running...

snmptrapd is stopped

sshd (pid 32692 32690 31125 31123 31007 31005 28859 28828 27800 27797 25769 25763 24868 24865 24453 24451 23420 23417 22077 22072 21991 21989 21578 21576 21185 21183 20987 20984 20757 20755 20665 20661 19843 19837 18508 18490 18447 18445 15355 15349 15268 15266 11588 11580 11106 11104 8560 8557 8494 8428 8426 8425 8408 7597 7591 7450 7448 6677 6671 5249 5233 4776 4772 4570 4563 3084 2807 2805 2307 2305 1909 1907 1548 1545) is running...

syslogd (pid 3014) is running...

klogd (pid 3018) is running...

vsftpd is stopped

winbindd is stopped

wpa_supplicant is stopped

xfs (pid 3463) is running...

xinetd (pid 4898) is running...


ps or pgrep command

You can use ps or pgrep command as follows to find out if service is running or not on RHEL/Centos:
 #  ps aux | grep 'serviceName'
 # ps aux | grep 'httpd'


Linux: Find Out Directory Size Command



To get the size of a Directory in Linux, use du command. du command is used to find the file space usage & summarize disk usage of each file/directory.

To find the size of /usr directory:

  du /usr
OR

 Pass -s option to see the total disk space summary & -h option for human readable format.

  du -sh /usr


We can also list the contents of the directory (whether file or directory) with size:-

du -sh /usr/*

Sample output:
[root@myserver ~]# du -sh /usr/*
71M     /usr/bin
8.0K    /usr/etc
8.0K    /usr/games
87M     /usr/include
122M    /usr/java
1.8M    /usr/kerberos
535M    /usr/lib
199M    /usr/lib64
12M     /usr/libexec
212M    /usr/local
17M     /usr/sbin
563M    /usr/share
57M     /usr/src
4.0K    /usr/tmp
48M     /usr/X11R6




Shell Script: Find Number Of Arguments Passed


Many times , when we create shell scripts we try to do repetitive tasks through functions. Some functions take arguments & we have to check the no. of arguments that are passed to it.

Each bash shell function has the following set of shell variables:
[a] All function parameters or arguments can be accessed via $1, $2, $3,..., $N.
[b] $* or $@ holds all parameters or arguments passed to the function.
[c] $# holds the number of positional parameters passed to the function.
[d] An array variable called FUNCNAME ontains the names of all shell functions currently in the execution call stack.

Example

Create a shell script as follows:
#!/bin/bash
# Purpose: Demo bash function
# -----------------------------
 ## Define a function called test()
test(){
  echo "Function name:  ${FUNCNAME}"
  echo "The number of positional parameter : $#"
  echo "All parameters or arguments passed to the function: '$@'"
  echo
}

## Call or invoke the function ##
## Pass the parameters or arguments  ##
test linuxtechtips
test 1 2 3 4 5
test "this" "is" "a" "test"

Run it as follows:
$ chmod +x script.name.here
$ ./script.name.here


Linux : Shell Remove Empty Lines


Many time we face situations where we have many empty lines in a file or script. Then how can we delete only those empty lines & make the file compact?

I tried following steps & they worked like charm. 
for deleting all empty lines from the file input.txt, run the following command:

sed command
    
                sed '/^$/d' input.txt > output.txt
        OR
                sed -i '/^$/d' input.txt


awk command
                awk 'NF > 0' input.txt > output.txt