Sunday, November 25, 2012

HowTo: UNIX / Linux Open TCP / UDP Ports

How do I open the TCP or UDP ports under UNIX / Linux like operating systems?

A port is an application-specific or process-specific software construct serving as a communications endpoint and it is identified by its number such as TCP port number 80 . It is used by TCP and UDP of the Internet Protocol Suite. A port number is a 16-bit unsigned integer, thus ranging from 0 to 65535.
        UNIX / Linux
    +------------------+
    | Networking stack |
    |      eth0        |
    +------------------+
           |
    +------------------+
    |  Apache process  |--> Binding port 80 @ 202.54.1.1 IP
    +------------------+
In the above example Apache process associates its input and output channel file descriptors (fd) with a port number 80 and an IP address 202.54.1.1. This is known as binding. It is used to send and receive web pages via UNIX / Linux operating system's networking stack (software). In other words communication is done using application ports. When you start the Apache you open port 80 for communication. Common services such as web, mail, pop3 et all use use specifically reserved, well-known port numbers for receiving service requests from client hosts. The well-known ports are defined the Internet Assigned Numbers Authority (IANA). Type the following command to see list well-known of TCP and UDP port numbers:
$ less /etc/services
grep -w 80 /etc/services

Sample outputs:
www  80/tcp  http  # WorldWideWeb HTTP
www  80/udp    # HyperText Transfer Protocol

Privileged Ports

Typically port number less than 1024 are used by well know network servers such as Apache. Under UNIX and Linux like oses root (super user) privileges are required to open privileged ports. Almost all clients uses a high port numbers for short term use. This is also known as an ephemeral port. For example Apache use TCP port 80
  Server                         Client w/ Firefox
 +----------+                    +----------------+
 | Apache   |                    | connects using |
 | TCP Port |                    | an ephemeral   |
 | 80 @     |<-----> eth0 <----> | port #         |
 |202.54.1.2|                    | 46025          |
 +----------+                    +----------------+
The port numbers are divided into three ranges:
  1. Well Known Ports: those from 0 through 1023.
  2. Registered Ports: those from 1024 through 49151
  3. Dynamic and/or Private Ports: those from 49152 through 65535
You can increase local port range by typing the following command (Linux specific example):
# echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
You can also increase or decrease socket timeout (Linux specific example):
# echo 2000 > /proc/sys/net/ipv4/tcp_keepalive_time

Common Well Known Port Numbers

The following are used by UNIX / Windows / Linux / BSD / OS X and all other server operating systems or network devices (see /etc/services file):
  • 21: FTP Server
  • 22: SSH Server (remote login)
  • 25: SMTP (mail server)
  • 53: Domain Name System (Bind 9 server)
  • 80: World Wide Web (HTTPD server)
  • 110: POP3 mail server
  • 143: IMAP mail server
  • 443: HTTP over Transport Layer Security/Secure Sockets Layer (HTTPDS server)
  • 445: microsoft-ds, Server Message Block over TCP

How Do I See Open Ports and Socket Information Under UNIX or Linux?

You can use the netstat command:
# netstat -tulpn
FreeBSD specific example:
# sockstat -l
To list open IPv4 connections use the lsof command:
# lsof -Pnl +M -i4
The ss command is used to dump socket statistics. It allows showing information similar to netstat command. It can display more TCP and state information than other tools
# ss -s
# ss -l
# ss -pl
# ss -o state established '( dport = :smtp or sport = :smtp )'

Examples

Each TCP or UDP port is opened using a UNIX service or daemon such as Apache web server. You can also write a program using C, C++, Perl, Shell or Bash to open any port. You can also use utilities such as nc command .

Apache Server Example (open TCP port 80)

Start the Apache web server under FreeBSD as follows to open TCP port 80:
# /usr/local/etc/rc.d/apache22 forcestart
OR
# /usr/local/etc/rc.d/apache22 start
To displays listening sockets (open ports) under FreeBSD, enter:
# sockstat -l
OR
# netstat -nat | grep LISTEN
You should see port 80 opened under FreeBSD. Under CentOS or Redhat (RHEL) Linux, you can open port 80 using the following commands:
# service httpd start
# chkconfig httpd on
# netstat -tulpn | grep :80

Firewall Configuration

All port numbers are encoded in the transport protocol packet header, and they can be read by other components of the network stack such as firewall. Firewall can be used for port forwarding or denying access to open port. For example, block an abusing IP address called 1.2.3.4 using UNIX firewall. In other words, Apache port is open but it may be blocked by UNIX (pf) or Linux (iptables) firewall. You also need to open port at firewall level. In this example, open tcp port 80 using Linux iptables firewall tool:
# /sbin/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables save

See also:

  1. CentOS / Redhat Linux Iptables Firewall Configuration Tutorial
  2. Redhat / CentOS / Fedora Linux Open Port
  3. FreeBSD Setting up Firewall using IPFW
  4. OpenBSD PF Firewall Script – /etc/pf.conf File

nc Command Example

The nc (or netcat utility) is used for just about anything under the sun involving TCP or UDP. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. In this example, open port 5000 using nc command:
$ nc -l 5000
On a second console or from a second UNIX / Linux machine, connect to the machine and port being listened on:
$ nc localhost 5000
OR
$ nc unix.system.ip.here 5000
In this example, send data from one computer to another:
$ nc -l 5555 > output.txt
Using a second machine, connect to the listening nc process (@ port 5555), feeding it the file which is to be transferred:
$ nc your.unix.systems.ip.here 5555 < input.txt
You can run netstat command to view open ports:
$ netstat -a
$ netstat -nat | grep LISTEN

Sample outputs:
tcp4       0      0  *.5555                 *.*                    LISTEN
tcp4       0      0  10.1.3.29.53           *.*                    LISTEN
tcp4       0      0  192.168.56.1.53        *.*                    LISTEN
tcp4       0      0  115.242.47.238.53      *.*                    LISTEN
tcp4       0      0  127.0.0.1.953          *.*                    LISTEN
tcp4       0      0  127.0.0.1.53           *.*                    LISTEN
tcp4       0      0  127.0.0.1.631          *.*                    LISTEN
tcp6       0      0  ::1.631                *.*                    LISTEN

Python Example

Create a file called echo_server.py:
#!/usr/bin/python
 
# Demo server to open port 8888
# Modified from Python tutorial docs
import socket
 
HOST = '127.0.0.1'       # Hostname to bind
PORT = 8888              # Open non-privileged port 8888
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()
 
Create a file called echo_client.py:
#!/usr/bin/python
 
# Demo client program
# Modified from Python tutorial docs
import socket
 
HOST = '127.0.0.1'     # Set the remote host, for testing it is localhost
PORT = 8000            # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Where there is love there is life')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
 
Save and close the file. Run it as follows:
$ chmod +x *.py
Start server, enter:
$ ./echo_server.py
$ netstat -nat | grep LISTEN

On a second console connect to the localhost and port being listened on using echo_client.py:
$ ./echo_client.py

HowTo: Find Symlink under UNIX / Linux

How do I find symlink(s) under UNIX and Linux operating systems?

To find all symlinks to /etc/resolv.conf, use the find command as follows:
# find /path/to/dir -lname /path/to/file
# find / -lname /etc/resolv.conf

/path/to/file is a symbolic link whose contents match shell pattern pattern. The metacharacters do not treat / or . specially. The -ilname FILE options is like -lname, but the match is case insensitive:
# find / -ilname resolv.conf

HowTo: Linux Install LibreOffice

How do I install newly released LibreOffice under Debian, Ubuntu or Fedora Linux?

LibreOffice is a productivity suite that is compatible with other major office suites, and available on a variety of platforms. It is free software and therefore free to download, use and distribute. The following software packages are intended to give you a first impression of what LibreOffice is.
WARNING! This beta release is not intended for production use! Be advised that the current beta might replace your OpenOffice.org installation. However, in my testing it did not replaced OpenOffice.org installation.

Install LibreOffice For Debian / Ubuntu Linux

Visit this page and grab the 32 or 64 bit tar ball. You can use the wget command:
$ cd /tmp
$ wget http://download.documentfoundation.org/libreoffice/testing/LO_3.3.0-beta1_Linux_x86_install-deb_en-US.tar.gz

Sample outputs:
Fig.01: Downloading LibreOffice
Fig.01: Downloading LibreOffice

Untar File

Type the following command:
$ tar -zxvf LO_3.3.0-beta1_Linux_x86_install-deb_en-US.tar.gz
Sample outputs:
en-US/
en-US/DEBS/
en-US/DEBS/desktop-integration/
en-US/DEBS/desktop-integration/libreoffice3.3-debian-menus_3.3-9526_all.deb
en-US/DEBS/libreoffice3-base_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core04_3.3.0-7_i386.deb
en-US/DEBS/libreoffice3-calc_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core07_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-xsltfilter_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-calc_3.3.0-7_i386.deb
en-US/DEBS/libreoffice3-math_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-extension-presentation-minimizer_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-help_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-math_3.3.0-7_i386.deb
en-US/DEBS/libreoffice3-en-us_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core03_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-extension-pdf-import_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-binfilter_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-pyuno_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-ooolinguistic_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-ooofonts_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-extension-mediawiki-publisher_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core05_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-javafilter_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core02_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-gnome-integration_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-kde-integration_3.3.0-7_i386.deb
en-US/DEBS/libreoffice3-impress_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-extension-presenter-screen_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-calc_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core06_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-impress_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-testtool_3.3.0-7_i386.deb
en-US/DEBS/libreoffice3-draw_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-graphicfilter_3.3.0-7_i386.deb
en-US/DEBS/libreoffice-ure_1.7.0-7_i386.deb
en-US/DEBS/libreoffice3-writer_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-images_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-extension-report-builder_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-draw_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-math_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-writer_3.3.0-7_i386.deb
en-US/DEBS/libreoffice3_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-core01_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-ogltrans_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-impress_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-draw_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-res_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-base_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-binfilter_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-base_3.3.0-7_i386.deb
en-US/DEBS/lobasis3.3-en-us-writer_3.3.0-7_i386.deb
en-US/licenses/
en-US/licenses/LICENSE_en-US.html
en-US/licenses/LICENSE_en-US
en-US/readmes/
en-US/readmes/README_en-US.html
en-US/readmes/README_en-US
en-US/update

Install LibreOffice

First, update existing OpenOffice.org, enter:
$ cd en-US
$ ./update

Finally, install the same, enter:
$ cd DEBS
$ sudo dpkg -i *.deb

Sample outputs:
Preparing to replace libreoffice3 3.3.0-7 (using libreoffice3_3.3.0-7_i386.deb) ...
Unpacking replacement libreoffice3 ...
Selecting previously deselected package libreoffice3-base.
Unpacking libreoffice3-base (from libreoffice3-base_3.3.0-7_i386.deb) ...
Selecting previously deselected package libreoffice3-calc.
Unpacking libreoffice3-calc (from libreoffice3-calc_3.3.0-7_i386.deb) ...
Selecting previously deselected package libreoffice3-draw.
Unpacking libreoffice3-draw (from libreoffice3-draw_3.3.0-7_i386.deb) ...
Selecting previously deselected package libreoffice3-en-us.
Unpacking libreoffice3-en-us (from libreoffice3-en-us_3.3.0-7_i386.deb) ...
Selecting previously deselected package libreoffice3-impress.
...
..
...
(output truncated)
Setting up lobasis3.3-ogltrans (3.3.0-7) ...
Setting up lobasis3.3-ooofonts (3.3.0-7) ...
Setting up lobasis3.3-ooolinguistic (3.3.0-7) ...
Setting up lobasis3.3-pyuno (3.3.0-7) ...
Setting up lobasis3.3-testtool (3.3.0-7) ...
Setting up lobasis3.3-writer (3.3.0-7) ...
Setting up lobasis3.3-xsltfilter (3.3.0-7) ...
Setting up libreoffice3 (3.3.0-7) ...
Setting up libreoffice3-draw (3.3.0-7) ...
Setting up libreoffice3-en-us (3.3.0-7) ...
Setting up libreoffice3-impress (3.3.0-7) ...
Setting up libreoffice3-math (3.3.0-7) ...
Setting up libreoffice3-writer (3.3.0-7) ...
Setting up lobasis3.3-base (3.3.0-7) ...
Setting up lobasis3.3-binfilter (3.3.0-7) ...
Setting up lobasis3.3-calc (3.3.0-7) ...
Setting up libreoffice3-base (3.3.0-7) ...
Setting up libreoffice3-calc (3.3.0-7) ...
Processing triggers for menu ...

Installation Location

LibreOffice is located at /opt/libreoffice3/ and binaries are stored in /opt/libreoffice3/program/ directory. You need to type the following command to use the same:
$ cd /opt/libreoffice3/program/
Various components:
  1. simpress - LibreOffice Impress is a presentation program, similar to Microsoft PowerPoint and Apple Keynote
  2. sdraw - LibreOffice Draw is a vector graphics editor application, similar to Microsoft similar features to desktop-publishing software.
  3. scalc - LibreOffice Calc is a spreadsheet application, similar to Microsoft Excel and Lotus 1-2-3.
  4. swriter - LibreOffice Writer is a word processor application, similar to Microsoft Word and WordPerfect.
  5. sbase - LibreOffice Base is a database management program, similar to Microsoft Access.
  6. smath - LibreOffice Math is a tool for creating and editing mathematical formula, similar to Microsoft Equation Editor.
  7. soffice - LibreOffice
To start LibreOffice Writer, enter:
$ /opt/libreoffice3/program/swriter filename
OR
$ ./swriter
Sample outputs:
Fig.02: Libreoffice Writer a word processor in action
Fig.02: Libreoffice Writer a word processor in action

Alternatively, you can type the following and pick program as you like:
./soffice
Sample outputs:
LibreOffice Screenshot
LibreOffice Screenshot

A Note About Fedora Linux (RPM) Installation

Type the following commands to download the same for RPM based distros such as Fedora Linux. All builds are English-only (en-US). Also note that the LibreOffice branding and renaming is new and work in progress. You may still see old graphics, icons or old websites references in help systems. Use the wget command to download rpm tar ball, enter:
$ wget http://download.documentfoundation.org/libreoffice/testing/LO_3.3.0-beta1_Linux_x86_install-rpm_en-US.tar.gz
Use the tar command to untar it, enter:
tar -zxvf LO_3.3.0-beta1_Linux_x86_install-rpm_en-US.tar.gz
Sample outputs:
en-US/
en-US/RPMS/
en-US/RPMS/desktop-integration/
en-US/RPMS/desktop-integration/libreoffice3.3-freedesktop-menus-3.3-9526.noarch.rpm
en-US/RPMS/desktop-integration/libreoffice3.3-mandriva-menus-3.3-9526.noarch.rpm
en-US/RPMS/desktop-integration/libreoffice3.3-redhat-menus-3.3-9526.noarch.rpm
en-US/RPMS/desktop-integration/libreoffice3.3-suse-menus-3.3-9526.noarch.rpm
en-US/RPMS/libreoffice-ure-1.7.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-help-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-extension-presentation-minimizer-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-base-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-writer-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-testtool-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-binfilter-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-images-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-base-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-draw-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-math-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-impress-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-gnome-integration-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-impress-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-en-US-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-calc-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-ogltrans-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-pyuno-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-math-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-calc-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core06-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-kde-integration-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-ooolinguistic-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-draw-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-calc-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-res-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-binfilter-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-math-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-impress-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-writer-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core02-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core03-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-base-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-extension-pdf-import-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-javafilter-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core04-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-ooofonts-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-extension-mediawiki-publisher-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-en-US-writer-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-extension-report-builder-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core07-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-extension-presenter-screen-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core05-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-graphicfilter-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-xsltfilter-3.3.0-9526.i586.rpm
en-US/RPMS/libreoffice3-draw-3.3.0-9526.i586.rpm
en-US/RPMS/lobasis3.3-core01-3.3.0-9526.i586.rpm
en-US/licenses/
en-US/licenses/LICENSE_en-US.html
en-US/licenses/LICENSE_en-US
en-US/readmes/
en-US/readmes/README_en-US.html
en-US/readmes/README_en-US
en-US/update
Type the following commands:
# cd en-US
# ./update
# cd RPMS
# rpm -ivh *.rpm

All binaries are located at /opt/libreoffice3/program directory. To start LibreOffice, enter:
$ /opt/libreoffice3/program/soffice

Can I Run LibreOffice And OpenOffice.Org?

Yes, you can run the both as they are installed in different directories:
  • OpenOffice.Org Location: /usr/lib/openoffice/program/
  • LibreOffice Location: /opt/libreoffice3/program/
To start OO calc, enter:
/usr/lib/openoffice/program/scalc
To start LibreOffice calc, enter:
/opt/libreoffice3/program/scalc
Sample outputs:
Fig.04: OpenOffice.Org vs LibreOffice
Fig.04: OpenOffice.Org vs LibreOffice

HowTo: Linux Rename a RAID Array From md0 to md2

I am moving a raid array called /dev/md0 from serverA to serverB. On serverB /dev/md0 is already in use. How do I rename a RAID array from /dev/md0 to /dev/md2?

You can move a RAID array (software based RAID array) to another system. However, if /dev/md0 is already is use on serverB, you can rename /dev/md0 as /dev/m2 (or next available md device). In this example:







  1. /dev/md0 is original software based RAID array.
  2. /dev/md0 is made of the two partitions called /dev/sdc1 and /dev/sdd2.
  3. I am going to rename /dev/md0 as /dev/md2 i.e. set /dev/md2 as the new device name.
WARNING! These examples may crash your computer if executed. Make a backup - it cannot be stressed enough how important it is to make a backup of your system before you do this.

Type the following commands on serverA

# mdadm --detail /dev/md0
Sample outputs:
 
/dev/md0:
        Version : 0.90
  Creation Time : Sat Jan  1 05:30:03 2000
     Raid Level : raid1
     Array Size : 2490176 (2.37 GiB 2.55 GB)
  Used Dev Size : 2490176 (2.37 GiB 2.55 GB)
   Raid Devices : 2
  Total Devices : 2
Preferred Minor : 0
    Persistence : Superblock is persistent
 
    Update Time : Wed Nov 21 01:43:40 2012
          State : clean
 Active Devices : 2
Working Devices : 2
 Failed Devices : 0
  Spare Devices : 0
 
           UUID : 8c229b6a:c20a3bfa:2d164f4f:84bee133 (local to host nas03)
         Events : 0.43537
 
    Number   Major   Minor   RaidDevice State
       0       8        1        0      active sync   /dev/sdc1
       1       8       17        1      active sync   /dev/sdd1
 
Note down the output, especially version number 0.90. It is recommended that you use the same version on the serverB. Next, stop the /dev/md0 on serverA, enter:
# mdadm --stop /dev/md0
The --stop option deactivate array /dev/md0, releasing all resources. Either use option #1 or option #2 to reassemble the RAID array.

Option #1: Rename a RAID

Next, you need to reassemble a pre-existing array as /dev/md2:
# mdadm --assemble /dev/md2 --super-minor=0 --update=super-minor /dev/sdc1 /dev/sdd1
The above command renames /dev/md0 indicated by the option --super-minor=0 as /dev/md2. The array is now ready to move into the serverB.

Option #2: Rename a RAID

The --super-minor option is only relevant for v0.90 metadata, and should not normally be used. Using --uuid is much safer. First, find out UUID for all devices, run:
# mdadm -Es
Sample outputs:
ARRAY /dev/md0 UUID=8c229b6a:c20a3bfa:2d164f4f:84bee133
ARRAY /dev/md1 UUID=b9cf66f0:f4e3e168:2d164f4f:84bee133
ARRAY /dev/md/2 metadata=1.2 UUID=e8e12adc:e0a02bdf:1cd25903:6c2f2b02 name=nas03:2
Type the following command reassemble the RAID device as /dev/md2:
# mdadm --uuid=8c229b6a:c20a3bfa:2d164f4f:84bee133 --update=super-minor --assemble /dev/md2
The array is now ready to move into the serverB.

Type the following commands on serverB

Attach /dev/sdc and /dev/sdd to the serverB and boot the server. The new server will use /dev/md2 immediately without any problem. You may need to update mdadm.conf file.
# cp -v /etc/mdadm/mdadm.{conf,bakup-nov-21-2012-by-nixcraft}
# mdadm -Es > /etc/mdadm/mdadm.conf

Possible caveats when renaming a RAID array

  1. You may need to update your grub.conf.
  2. This procedure may work fine but after reboot /dev/md2 may not be recognized at all. To avoid this problem use uuid when reassembling the RAID array.
  3. Make sure you update mdadm.conf on both serverA and serverB.
Recommended readings:
  • mdadm man page

MySQL: Connect From an Other System / Computer

How do I connect to my MySQL database server from an other server (say Apache or Tomcat app server) in same VLAN under CentOS / Fedora / RHEL / Redhat Linux?

First, you need to turn on the remote access for your database server.

Sample Setup

Consider the following sample setup:
                              +----------- server1 192.168.1.6
                              |
                              +------------ tomcat1 192.168.1.7
                              |
 +------------------+         |
 | MySQL Server     | --------+------------ apache2 192.168.1.8
 | 192.168.1.5:3306 | --------+
 +------------------+         |
     LAN      192.168.1.0/24  +------------- pc1 192.168.1.51
                              |
                              |
                              +-------------- pc25 192.168.1.76
You need to allow access to 192.168.1.5 from apache server located at 192.168.1.8.

Step #1: Configure MySQL Server For Remote Access

Open a terminal or login to 192.168.1.5 using the ssh command:
$ ssh root@192.168.1.5
Edit /etc/my.cnf, enter:
# vi /etc/my.cnf
Modify or append as follows:
 
# make sure the following line is deleted or commented out
# skip-networking
bind-address    = 192.168.1.5
 
Save and close the file. Restart the mysql server, enter:
# service mysqld restart

Make Sure TCP Port # 3306 is Opened For Business

Verify that the TCP port 3306 is open, enter:
# netstat -tulpn | grep :3306

Step #2: Linux Firewall Configuration For TCP Port # 3306

You need to open TCP port # 3306 at the firewall level, enter:
# iptables -A INPUT -i eth0 -s 192.168.1.8 -p tcp --destination-port 3306 -j ACCEPT
# service iptables save

Step #3: Configure Database Remote Access

You need to grant access to an existing database called salesdb from remote IP called 192.168.1.8 using a username called foo. First, connect to mysql server as root user, enter:
# mysql -u root -p mysql
Type the following command At mysql> prompt, enter:
mysql> update db set Host='192.168.1.8' where Db='salesdb';
mysql> update user set Host='192.1681.8' where user='foo';
mysql> \q

Login to 192.168.1.8 and type the following command to test mysql server remote access:
$ mysql -u foo -h 192.168.1.5 -p salesdb
Sample outputs:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27720995
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> 

Linux dd Command Show Progress Copy Bar With Status

I'm using dd command for block level copy and just found out that there's no built in way to check the progress. How do I use the Linux or Unix dd command while coping /dev/sda to /deb/sdb and display a progress bar when data goes through a pipe?

You need to use the pv command which allows you to see the progress of data through a pipeline. You need to install pv command as described here.







Once installed, type the following commands to see the status bar. Please note that if standard input is not a file and no size was given with the -s option, the progress bar cannot indicate how close to completion the transfer is, so it will just move left and right to indicate that data is moving. It will also show average MB/s rate:

Examples

WARNING! These examples may crash your computer and may result into data loss if not executed with care.
Copy /dev/sda to to /deb/sdb:
 
pv -tpreb /dev/sda | dd of=/dev/sdb bs=64M
 
OR
 
pv -tpreb /dev/sda | dd of=/dev/sdb bs=4096 conv=notrunc,noerror
 
Sample outputs:
Fig.01: pv and dd in action
Fig.01: pv and dd in action

You can create a progress bar and display using the dialog command as follows:
 
(pv -n /dev/sda | dd of=/dev/sdb bs=128M conv=notrunc,noerror) 2>&1 | dialog --gauge "Running dd command (cloning), please wait..." 10 70 0
 
Sample outputs:
HowTo: Check The Status of dd Command In Progress under Unix like operating systems
Fig.02: Show the Status of dd Command in progress using pv and dialog command

HowTo: Check Swap Usage in Linux

How do I check swap (paging) usage under Linux operating systems using command line options?

Swap space (also known as paging) is nothing but computer memory management involving swapping regions of memory to and from storage.







You can see swap usage summary by device using any one of the following commands. You may have to login as root user to use the following commands. The maximum useful size of a swap area depends on the architecture and the kernel version. For Linuux kernels after v2.3.3+ there is no such limitation on swap size.

Option #1: /proc/swaps file

Type the following command to see total and used swap size:
# cat /proc/swaps
Sample outputs:
Filename    Type  Size Used Priority
/dev/sda3                               partition 6291448 65680 0

Option #2: swapon command

Type the following command:
# swapon -s
Sample outputs:
Filename    Type  Size Used Priority
/dev/sda3                               partition 6291448 65680 0

Option #3: free command

Use the free command as follows:
# free -g
# free -k
# free -m

Sample outputs:
             total       used       free     shared    buffers     cached
Mem:         11909      11645        264          0        324       8980
-/+ buffers/cache:       2341       9568
Swap:         6143         64       6079

Option #4: vmstat command

Type the following vmstat command:
# vmstat
# vmstat 1 5

Sample outputs:
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 1  9 1209512 101352   1504 127980    0    3    11    20   60   55  3  1 95  1
 2 11 1209640 101292   1508 134132  844  424  5608   964 23280 15012  2  8 20 70
 0 10 1210052 108132   1532 125764  648  660 10548   916 22237 18103  3 10 11 77
 1 13 1209892 106484   1500 128052  796  240 10484   980 24024 12692  2  8 24 67
 1  9 1209332 113412   1500 124028 1608  168  2472   620 28854 13761  2  8 20 70
Note down the following output from swap field:
  1. si: Amount of memory swapped in from disk (/s).
  2. so: Amount of memory swapped to disk (/s).

Option #5: top/atop/htop command

Type the following commands:
# atop
# htop
# top

Sample outputs (from top command):
top - 02:54:24 up 15:24,  4 users,  load average: 0.45, 4.84, 6.75
Tasks: 266 total,   1 running, 264 sleeping,   0 stopped,   1 zombie
Cpu(s):  3.2%us,  1.4%sy,  0.0%ni, 94.4%id,  1.0%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:   8120568k total,  7673584k used,   446984k free,     4516k buffers
Swap: 15859708k total,  1167408k used, 14692300k free,  1151972k cached
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
13491 vivek     20   0 1137m 279m 6692 S   10  3.5  19:17.47 firefox
 5663 vivek     10 -10 1564m 1.1g  59m S    8 14.5   5:10.94 vmware-vmx
 2661 root      20   0  352m 185m 8604 S    6  2.3  65:40.17 Xorg
 3752 vivek     20   0 3566m 2.6g  12m S    6 33.6  63:44.35 compiz
 4798 vivek     20   0  900m  50m 4992 S    2  0.6   0:11.04 chrome
 5539 vivek     20   0 1388m 838m 780m S    2 10.6   1:45.78 VirtualBox
 6297 root      20   0     0    0    0 S    2  0.0   0:00.15 kworker/2:0
 6646 root      20   0 19252 1404  936 R    2  0.0   0:00.01 top
    1 root      20   0  8404  644  608 S    0  0.0   0:03.32 init
    2 root      20   0     0    0    0 S    0  0.0   0:00.03 kthreadd
    3 root      20   0     0    0    0 S    0  0.0   0:02.30 ksoftirqd/0
    6 root      RT   0     0    0    0 S    0  0.0   0:00.00 migration/0
    7 root      RT   0     0    0    0 S    0  0.0   0:00.24 watchdog/0
   37 root       0 -20     0    0    0 S    0  0.0   0:00.00 cpuset
   38 root       0 -20     0    0    0 S    0  0.0   0:00.00 khelper
   39 root      20   0     0    0    0 S    0  0.0   0:00.00 kdevtmpfs
   40 root       0 -20     0    0    0 S    0  0.0   0:00.00 netns
Sample outputs from htop command:
Linux: Swap Memory Usage Command
Fig.01: Linux: Swap Memory Usage Command