How to Display IP Address on RHEL/CentOS

In this post, i will share the simple command to display the CentOS and Redhat Enterprise linux(RHEL) ip address. This command has been tested on Linux CentOS 5.8 server and may working on other CentOS and RHEL version as well. There are two alternative command to display ip address :

1. Display ip address using “ip address show” command :

[root@centos58 ~]# ip address show
1: lo:  mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0:  mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:a1:17:ad brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.53/24 brd 192.168.1.255 scope global eth0

2. Display the ip address using “ifconfig” command :

[root@centos58 ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:A1:17:AD
          inet addr:192.168.1.53  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72408 errors:0 dropped:0 overruns:0 frame:0
          TX packets:64177 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:24607729 (23.4 MiB)  TX bytes:6678283 (6.3 MiB)
          Interrupt:75 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

How to Grep Multiples Lines and using Specific Keyword on Linux

grep is a command line text search utility originally written for Unix or linux. In linux you can grep multiple lines before or after matching the keywords. This examples has been tested on linux CentOS 6.2, but it may workings on other linux version such as Redhat Enterprise Linux 5 (RHEL5) or (RHEL6).

Get grep command help :

[root@centos62 ~]# grep --help

Example :

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM

Assumed that you have exported all log for 29 March 2012 from /var/log/messages into 29032012.txt as below :

[root@centos62 ~]# more /var/log/messages | grep "Mar 29" > 29032012.txt

1. How to Grep Multiples Lines

Please grep “cubic” with -B1 and -A4 :

[root@centos62 ~]# grep -B1 -A4 "cubic" 29032012.txt

The output will return like this :

Mar 29 21:04:16 centos62 kernel: usbhid: v2.6:USB HID core driver
Mar 29 21:04:16 centos62 kernel: TCP cubic registered
Mar 29 21:04:16 centos62 kernel: Initializing XFRM netlink socket
Mar 29 21:04:16 centos62 kernel: NET: Registered protocol family 17
Mar 29 21:04:16 centos62 kernel: Using IPI No-Shortcut mode
Mar 29 21:04:16 centos62 kernel: registered taskstats version 1

2. How to Grep using Specific Keyword :
Grep only keyword “BIOS-e820” from 29032012.txt file as below :

[root@centos62 ~]# more 29032012.txt | grep "BIOS-e820"

The output will return like this :

Mar 29 21:04:16 centos62 kernel: BIOS-e820: 0000000000000000 - 000000000009f800 (usable)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000000009f800 - 00000000000a0000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000000ca000 - 00000000000cc000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000000dc000 - 00000000000e0000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000000e4000 - 0000000000100000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 0000000000100000 - 000000003fef0000 (usable)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000003fef0000 - 000000003feff000 (ACPI data)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000003feff000 - 000000003ff00000 (ACPI NVS)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000003ff00000 - 0000000040000000 (usable)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000e0000000 - 00000000f0000000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000fec00000 - 00000000fec10000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000fee00000 - 00000000fee01000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000fffe0000 - 0000000100000000 (reserved)

How to Install and Configure Samba SWAT on CentOS 5.5

SWATSWAT is the Samba Web Administration Tool that enable us to configure Samba from Web browser. In this post i will show simple steps to get Samba SWAT running on your CentOS 5.5 server. With SWAT, you can easily manage your samba server without messing with command line. You just need to install xinetd first, and then proceed to install samba-SWAT.

1. Install xinetd

    [root@server ~]# yum -y install xinetd
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * addons: ossm.utm.my
     * base: ossm.utm.my
     * epel: mirror01.idc.hinet.net
     * extras: ossm.utm.my
     * updates: ossm.utm.my
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package xinetd.i386 2:2.3.14-10.el5 set to be updated
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ==========================================================================================
     Package            Arch             Version                       Repository        Size
    ==========================================================================================
    Installing:
     xinetd             i386             2:2.3.14-10.el5               base             124 k
    
    Transaction Summary
    ==========================================================================================
    Install       1 Package(s)
    Upgrade       0 Package(s)
    
    Total download size: 124 k
    Downloading Packages:
    xinetd-2.3.14-10.el5.i386.rpm                                      | 124 kB     00:02
    Running rpm_check_debug
    Running Transaction Test
    Finished Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing     : xinetd                                                             1/1
    
    Installed:
      xinetd.i386 2:2.3.14-10.el5
    
    Complete!
    

2. Start xinetd

    [root@server ~]# /etc/rc.d/init.d/xinetd start
    Starting xinetd:                                           [  OK  ]
    

3. Install SWAT

    [root@server ~]# yum -y install samba-swat
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * addons: ossm.utm.my
     * base: ossm.utm.my
     * epel: mirror01.idc.hinet.net
     * extras: ossm.utm.my
     * updates: ossm.utm.my
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package samba-swat.i386 0:3.0.33-3.29.el5_6.2 set to be updated
    --> Processing Dependency: samba = 3.0.33-3.29.el5_6.2 for package: samba-swat
    --> Running transaction check
    ---> Package samba.i386 0:3.0.33-3.29.el5_6.2 set to be updated
    --> Processing Dependency: samba-common = 3.0.33-3.29.el5_6.2 for package: samba
    --> Running transaction check
    ---> Package samba-common.i386 0:3.0.33-3.29.el5_6.2 set to be updated
    --> Processing Dependency: libsmbclient = 3.0.33-3.29.el5_6.2 for package: samba-common
    --> Running transaction check
    ---> Package libsmbclient.i386 0:3.0.33-3.29.el5_6.2 set to be updated
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ==========================================================================================
     Package               Arch          Version                       Repository        Size
    ==========================================================================================
    Installing:
     samba-swat            i386          3.0.33-3.29.el5_6.2           updates          8.2 M
    Installing for dependencies:
     samba                 i386          3.0.33-3.29.el5_6.2           updates           16 M
    Updating for dependencies:
     libsmbclient          i386          3.0.33-3.29.el5_6.2           updates          908 k
     samba-common          i386          3.0.33-3.29.el5_6.2           updates          6.7 M
    
    Transaction Summary
    ==========================================================================================
    Install       2 Package(s)
    Upgrade       2 Package(s)
    
    Total download size: 32 M
    Downloading Packages:
    (1/4): libsmbclient-3.0.33-3.29.el5_6.2.i386.rpm                   | 908 kB     00:16
    (2/4): samba-common-3.0.33-3.29.el5_6.2.i386.rpm                   | 6.7 MB     02:01
    (3/4): samba-swat-3.0.33-3.29.el5_6.2.i386.rpm                     | 8.2 MB     02:28
    (4/4): samba-3.0.33-3.29.el5_6.2.i386.rpm                          |  16 MB     05:11
    ------------------------------------------------------------------------------------------
    Total                                                      55 kB/s |  32 MB     09:59
    Running rpm_check_debug
    Running Transaction Test
    Finished Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Updating       : libsmbclient                                                       1/6
      Updating       : samba-common                                                       2/6
      Installing     : samba                                                              3/6
      Installing     : samba-swat                                                         4/6
      Cleanup        : libsmbclient                                                       5/6
      Cleanup        : samba-common                                                       6/6
    
    Installed:
      samba-swat.i386 0:3.0.33-3.29.el5_6.2
    
    Dependency Installed:
      samba.i386 0:3.0.33-3.29.el5_6.2
    
    Dependency Updated:
      libsmbclient.i386 0:3.0.33-3.29.el5_6.2     samba-common.i386 0:3.0.33-3.29.el5_6.2
    
    Complete!
    

4. Configure swat as below. Change Line 10 and 14 only.

    [root@server ~]# vi /etc/xinetd.d/swat
    
    # default: off
    # description: SWAT is the Samba Web Admin Tool. Use swat \
    #              to configure your Samba server. To use SWAT, \
    #              connect to port 901 with your favorite web browser.
    service swat
    {
            port            = 901
            socket_type     = stream
            wait            = no
            only_from       = 127.0.0.1 192.168.2.0/16
            user            = root
            server          = /usr/sbin/swat
            log_on_failure  += USERID
            disable         = no
    }
    

5. Restart xinetd

    [root@server ~]# /etc/rc.d/init.d/xinetd restart
    Stopping xinetd:                                           [  OK  ]
    Starting xinetd:                                           [  OK  ]
    

How to Open the File in Read Only Mode on Linux

Question :
How to open a file on read only mode on linux?

Answer :
This command has been tested on Redhat Enterprise linux 6 (RHEL6) server. To open the file with vim in read only mode on linux, you need to run “vim -R” as below. Vim is enhanced version of vi editor that viewing the text file with coloring. “vim -R” is good command if you want to avoid accidentally deleted any important line as in read only.

Example :

[root@rhel6 ~]# vim -R /etc/passwd

read only mode

How Display Logged in User Information and Terminal number on Linux Server

In this post i will show on how to display logged in user information and terminal number on Linux server. This post provides practical examples for future references. This steps has been tested on Redhat Linux Enterprise server 6 and may working CentOS server as well.

1. “who am i” command is use to display the username of currently logged.

[root@rhel6 ~]# who am i
root     pts/1        2012-04-17 06:14 (192.168.1.52)

2. who command will display all the user currently logged in all terminals.

[root@rhel6 ~]# who
root     tty1         2012-04-17 04:29
root     pts/0        2012-04-17 04:29 (192.168.1.52)
root     pts/1        2012-04-17 06:14 (192.168.1.52)
root     pts/2        2012-04-17 06:01 (192.168.1.52)

3. tty command is used to display the terminal number of currently logged in terminals.

[root@rhel6 ~]# tty
/dev/pts/1

4. The w command shows who is logged in to the system and what they are doing.

[root@rhel6 ~]# w
 06:18:39 up  1:50,  4 users,  load average: 0.02, 0.02, 0.00
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                04:29    1:48m  4.80s  4.78s ping www.google.com
root     pts/0    192.168.1.52     04:29    1:49m  0.02s  0.02s -bash
root     pts/1    192.168.1.52     06:14    0.00s  0.05s  0.01s w
root     pts/2    192.168.1.52     06:01   26.00s  0.11s  0.03s -bash

How to Check Memory and I/O with vmstat on Linux CentOS 5/CentOS 6/RHEL 5/RHEL 6 server – System Performance

vmstat command is a utility that will provides interesting information abaout processes, memory, i/O and CPU activity. When you run vmstat utility command without any arguments, you will see the output as below. This post will guide you on how to check memory and I/O with vmstat on Linux CentOS 5/CentOS 6/RHEL 5/RHEL 6 server to optimize the linux system performance.

[root@rhel6 ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 888252  28976  63004    5    0     0    11   15   18  0  0 99  0  0

Below are the description of above vmstat output :
1. The procs fields show the number of processes
– Waiting for run time (r)
-Blocked (b)
-Swapped out (w)

2. The memory fields show the kilobytes of swap memory,free memory, buffered memory and Cached memory

3. The swap fields show the kilobytes per second of memory
-Swapped in from disk (si)
-Swapped out to disk (so)

4. The io fields show the number of blocks per second
-Sent to block devices (bi)
-Received from block devices (bo)

5. The system shows the number of
-Interrupts per second (in)
-Context switches per second (cs)

6. The cpu field shows the percentage of total CPU time as
-User time (us)
-System time (sy)
-Idle (id) time

To prints the vmstat version, run this command :

[root@rhel6 ~]# vmstat -V
procps version 3.2.8

We can let the vmstat to run automatically by execute this command :

[root@rhel6 ~]# vmstat 

Where nsec is the number of seconds you want it to wait before another update.

To run vmstat on every 10 seconds :

[root@rhel6 ~]# vmstat 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 888072  29088  63004    4    0     0    11   14   18  0  0 99  0  0
 0  0      0 888064  29088  63004    0    0     0     0   12   15  0  0 100  0  0
 0  0      0 888064  29088  63004    0    0     0     0   13   16  0  0 100  0  0

Other vmstat usage as below :

usage: vmstat [-V] [-n] [delay [count]]
              -V prints version.
              -n causes the headers not to be reprinted regularly.
              -a print inactive/active page stats.
              -d prints disk statistics
              -D prints disk table
              -p prints disk partition statistics
              -s prints vm table
              -m prints slabinfo
              -t add timestamp to output
              -S unit size
              delay is the delay between updates in seconds.
              unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)
              count is the number of updates.

How to Track System Activity With top Command on Linux CentOS 5/CentOS 6/RHEL 5/ RHEL 6 server – System Performance

Top command is utility to monitor system activity interactively. When you run top from shell window, it will display all the activity processes and updates the screen. In this post i will show the usage of top command on CentOS 5/CentOS 6/RHEL 5/ RHEL 6 server to keep track system activity and to optimize the system performance.

Display top command on RHEL 6 :

[root@rhel6 ~]# top
top - 18:54:19 up 13:29,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  93 total,   1 running,  90 sleeping,   2 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1031320k total,   225804k used,   805516k free,    31120k buffers
Swap:  2064376k total,        0k used,  2064376k free,   116716k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1375 root      20   0 97768 9796 5768 S  0.3  0.9   0:03.43 httpd
 3974 root      20   0  2632 1076  868 R  0.3  0.1   0:00.08 top
    1 root      20   0  2828 1392 1196 S  0.0  0.1   0:01.81 init
    2 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kthreadd
    3 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 migration/0
    4 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    5 root      RT   0     0    0    0 S  0.0  0.0   0:00.00 watchdog/0
    6 root      20   0     0    0    0 S  0.0  0.0   0:00.25 events/0
    7 root      20   0     0    0    0 S  0.0  0.0   0:00.00 cpuset
    8 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khelper
    9 root      20   0     0    0    0 S  0.0  0.0   0:00.00 netns
   10 root      20   0     0    0    0 S  0.0  0.0   0:00.00 async/mgr
   11 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pm
   12 root      20   0     0    0    0 S  0.0  0.0   0:00.00 sync_supers
   13 root      20   0     0    0    0 S  0.0  0.0   0:00.00 bdi-default
   14 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kintegrityd/0
   15 root      20   0     0    0    0 S  0.0  0.0   0:00.02 kblockd/0
   16 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpid
   17 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_notify
   18 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kacpi_hotplug
   19 root      20   0     0    0    0 S  0.0  0.0   0:00.01 ata/0
   20 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ata_aux
   21 root      20   0     0    0    0 S  0.0  0.0   0:00.00 ksuspend_usbd
   22 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khubd
   23 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kseriod
   25 root      20   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd
   26 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kswapd0
   27 root      25   5     0    0    0 S  0.0  0.0   0:00.00 ksmd
   28 root      20   0     0    0    0 S  0.0  0.0   0:00.00 aio/0
   29 root      20   0     0    0    0 S  0.0  0.0   0:00.00 crypto/0
   34 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pciehpd
   36 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kpsmoused
   37 root      20   0     0    0    0 S  0.0  0.0   0:00.00 usbhid_resumer
   67 root      20   0     0    0    0 S  0.0  0.0   0:00.00 kstriped
  267 root      20   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  268 root      20   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_1
  279 root      20   0     0    0    0 S  0.0  0.0   0:00.20 mpt_poll_0

Display top command on CentOS 5.7 :

[root@CentOS57 ~]# top
top - 18:57:39 up 10:04,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  73 total,   1 running,  70 sleeping,   2 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.8%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1034700k total,    86916k used,   947784k free,     7292k buffers
Swap:  2096472k total,        0k used,  2096472k free,    36104k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      15   0  2160  676  584 S  0.0  0.1   0:00.93 init
    2 root      RT  -5     0    0    0 S  0.0  0.0   0:00.00 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/0
    4 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 events/0
    5 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khelper
    6 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kthread
    9 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kblockd/0
   10 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kacpid
  173 root      17  -5     0    0    0 S  0.0  0.0   0:00.00 cqueue/0
  176 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 khubd
  178 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 kseriod
  244 root      21   0     0    0    0 S  0.0  0.0   0:00.00 khungtaskd
  245 root      22   0     0    0    0 S  0.0  0.0   0:00.00 pdflush
  246 root      15   0     0    0    0 S  0.0  0.0   0:00.01 pdflush
  247 root      17  -5     0    0    0 S  0.0  0.0   0:00.00 kswapd0
  248 root      17  -5     0    0    0 S  0.0  0.0   0:00.00 aio/0
  466 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kpsmoused
  492 root      10  -5     0    0    0 S  0.0  0.0   0:00.00 mpt_poll_0
  493 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 mpt/0
  494 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  497 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 ata/0
  498 root      19  -5     0    0    0 S  0.0  0.0   0:00.00 ata_aux
  505 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kstriped
  514 root      10  -5     0    0    0 S  0.0  0.0   0:00.17 kjournald
  544 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kauditd
  577 root      21  -4  3004 1468  504 S  0.0  0.1   0:00.62 udevd
 1730 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kmpathd/0
 1731 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 kmpath_handlerd
 1790 root      11  -5     0    0    0 S  0.0  0.0   0:00.00 kjournald
 1868 root      13  -5     0    0    0 S  0.0  0.0   0:00.00 iscsi_eh
 1896 root      18  -5     0    0    0 S  0.0  0.0   0:00.00 cnic_wq
 1911 root      15  -5     0    0    0 S  0.0  0.0   0:00.00 ib_addr
 1918 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 ib_mcast
 1919 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 ib_inform
 1920 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 local_sa
 1923 root      20  -5     0    0    0 S  0.0  0.0   0:00.00 iw_cm_wq

By default, top will update its screen in every seconds. You can change this interval by using d seconds options as below :

To update the screen every 5 seconds, run the “top d 5” command :

[root@rhel6 ~]# top d 5
top - 18:56:51 up 13:32,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  94 total,   1 running,  90 sleeping,   3 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.7%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1031320k total,   226064k used,   805256k free,    31136k buffers
Swap:  2064376k total,        0k used,  2064376k free,   116716k cached

To update the screen every 10 seconds, run the “top d 10” command :

[root@rhel6 ~]# top d 10
top - 18:57:21 up 13:32,  2 users,  load average: 0.00, 0.00, 0.00
Tasks:  95 total,   1 running,  90 sleeping,   4 stopped,   0 zombie
Cpu(s):  0.0%us,  0.1%sy,  0.0%ni, 99.7%id,  0.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1031320k total,   226448k used,   804872k free,    31144k buffers
Swap:  2064376k total,        0k used,  2064376k free,   116716k cached

The screen updates every 5 to 20 seconds interval is more usefull tha the default setting, 1 second. This because update every 1 second will lists itself in its own output as the main resource consumer. If you press the h key while top is running, you will see the following output screen :

How to Backup and Restore MySQL Database on CentOS/RHEL

In this post, i will share on how to backup and restore MySQL database on Linux server. I have tested this on my CentOS and RHEL servers.

MySQLDump — A Database Backup Program, Click for more info.
The mysqldump client is a backup program originally written by Igor Romanenko. It can be used to dump a database or a collection of databases for backup or transfer to another SQL server (not necessarily a MySQL server). The dump typically contains SQL statements to create the table, populate it, or both. However, mysqldump can also be used to generate files in CSV, other delimited text, or XML format.

What is MySQLDump and how to use it :
The MySQLDump is handy utility that can be used to quickly backup the MySQL Database to the text files. To use the MySQLDump utility it is required to logon to the System running the MySQL Databse. You can use Telnet to remotely logon to the system if you don’t have the physical access to the machine.

Assumed that the MySQL server has been installed with the version/release as below :

Name        : mysql
Arch        : i686
Version     : 5.1.52
Release     : 1.el6_0.1

Login to mysql to check the existing databases :

[root@centos62 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.52 Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

Show available databases :

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| phpmyadmin         |
+--------------------+
3 rows in set (0.00 sec)

mysql> exit
Bye

1. Backup MySQL database to <databasebackupfiles>.sql

<databasename> = Target existing database name
<databasebackupfiles> = Preferred backed up file name (Optional and user defined)

# mysqldump -u root -p <databasename> > <databasebackupfiles>.sql
Enter password: <Type your mysql password and press enter>

1.1 As Example, i will back up the database named “mysql” to mysql19022012.sql file. This will taking backed up all the tables including the data :

[root@centos62 ~]# mysqldump -u root -p mysql > mysql19022012.sql
Enter password:

1.2 View the backed up file :

[root@centos62 ~]# ls | grep mysql
mysql19022012.sql

1.3 View the content on mysql19022012.sql :

[root@centos62 ~]# more mysql19022012.sql

2. Restore the backed up database named “mysql19022012.sql” to new database named “mysqlnew” as below :

# mysql -u root -p <mysqlpassword> <databasename> < <databasebackupfiles>.sql

<mysqlpassword> = MySQL password
<databasename> = Database name that will be restore
<databasebackupfiles> = Backed up database file, normally .sql file

2.1 Create the new database mysqlnew :

mysql> CREATE DATABASE mysqlnew;
Query OK, 1 row affected (0.01 sec)

2.2 As example, i will restore the mysql19022012.sql backed up file to database named “mysqlnew” :

[root@centos62 ~]# mysql -u root -p password mysqlnew < mysql19022012.sql

or

[root@centos62 ~]# mysql -u root -p mysqlnew < mysql19022012.sql
Enter password:

How to Install telnet on CentOS

Question : When i try to perform the troubleshooting using telnet command, the following error appeared. How to fix this ?

[root@centos62 ~]# telnet localhost 25
-bash: telnet: command not found

Answer : Simply install telnet using this command :

[root@centos62 ~]# yum install telnet
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ipserverone.com
 * epel: ftp.cuhk.edu.hk
 * extras: centos.ipserverone.com
 * rpmforge: ftp-stud.fht-esslingen.de
 * updates: centos.ipserverone.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package telnet.i686 1:0.17-47.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==========================================================================================
 Package         Arch          Version                Repository                     Size
==========================================================================================
Installing:
 telnet          i686          1:0.17-47.el6          CentOS6.2-Repository           56 k

Transaction Summary
==========================================================================================
Install       1 Package(s)

Total download size: 56 k
Installed size: 102 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : 1:telnet-0.17-47.el6.i686                                              1/1

Installed:
  telnet.i686 1:0.17-47.el6

Complete!

How to Install gcc on CentOS 6.2

In this post, i will show you on how to install gcc package. The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. The compiler is designed to support multiple front-ends and multiple back-ends by translating first into Register Transfer Language and from there into assembly code for the target architecture. Assume that you have installed minimal installation type CentOS 6.2 and has setup your own local yum repository. You can use internet CentOS yum repository instead if you have direct internet connection to your server.

How to Setup Local Yum Repository from CD-ROM/DVD-ROM image on CentOS 6.2

Simply run yum command to install the gcc.

    [root@centos6 ~]# yum install gcc
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.hostemo.com
     * extras: mirrors.hostemo.com
     * updates: mirrors.hostemo.com
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package gcc.i686 0:4.4.6-3.el6 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ============================================================================================
     Package       Arch           Version                  Repository                      Size
    ============================================================================================
    Installing:
     gcc           i686           4.4.6-3.el6              CentOS6.2-Repository           8.2 M
    
    Transaction Summary
    ============================================================================================
    Install       1 Package(s)
    
    Total download size: 8.2 M
    Installed size: 15 M
    Is this ok [y/N]: y
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing : gcc-4.4.6-3.el6.i686                                                     1/1
    
    Installed:
      gcc.i686 0:4.4.6-3.el6
    
    Complete!
    

How to Modify Date and Timestamps Using touch on CentOS

This post will guide you on how to modify date and time stamps using “touch” command. The touch command is easy to use and there are two reason to use it.

1. To create a file.
2. To update a file’s modification date.

The touch command is part file utilities package and has several options.

How to modify date and timestamps using touch ?
Touch can update a file’s date and time. you can even use touch to change a file’s date or time to the past or the future, for example:

a)Change date to past time:

    [root@server ~]# touch -t 0101141400 newtestfile1
    [root@server ~]# ls -l --full-time newtestfile1
    -rw-r--r-- 1 root root 0 2001-01-14 14:00:00.000000000 +0800 newtestfile1
    

Description:
The –full-time option and long format listing of the ls command show that the file newtestfile1 now has a timestamp of (2.00PM, 14 January 2001)

b)Change date to future time:

    [root@server ~]# touch -t 1212261400 newtestfile1
    [root@server ~]# ls -l --full-time newtestfile1
    -rw-r--r-- 1 root root 0 2012-12-26 14:00:00.000000000 +0800 newtestfile1
    

Description:
The –full-time option and long format listing of the ls command show that the file newtestfile1 now has a timestamp of (2.00PM, 26 December 2012)

How to Create a File With a touch Command on CentOS

This post will guide you on how to create a file using “touch” command. The touch command is easy to use and there are two reason to use it.

1. To create a file.
2. To update a file’s modification date.

The touch command is part file utilities package and has several options.

How to create new files using touch ?

    [root@server ~]# touch newtestfile1
    [root@server ~]# ls -l
    total 0
    -rw-r--r-- 1 root root 0 Jan 11 10:43 newtestfile1
    

Descriptions :
As you can see, touch creates a file with a length, or size, of zero