UnixServerAdmin

Server Administration & Management

How to limit CPU usage of any process in Linux

You can use cpulimit program that attempts to limit the cpu usage of a process. Limits are expressed in percentage and not in cpu time. cpulimit does not act on the nice value or other scheduling priority stuff, but on the real cpu usage. Also, it is able to adapt itself to the overall system load, dynamically and quickly.

Step-1 Install cpulimit

Type the following commands to install latest stable release:

# cd /tmp
# wget  http://downloads.sourceforge.net/cpulimit/cpulimit-1.1.tar.gz
# tar -zxvf cpulimit-1.1.tar.gz
# cd cpulimit-1.1
# make
# cp cpulimit /usr/local/sbin/
# rm -rf cpulimit*

Step-2 How do I use cpulimit?

To limit CPU usage of the process called firefox to 30%, enter:
# cpulimit -e firefox -l 30

To limit CPU usage of the process to 30% by using its PID, enter:
# cpulimit -p 1313 -l 30

To find out PID of the process use any of the following:
# ps aux | less
# ps aux | grep firefox
# pgrep -u vivek php-cgi
# pgrep lighttpd

You can also use absolute path name of the executable, enter:
# cpulimit -P /opt/firefox/firebox -l 30

Where,

-p : Process PID.
-e : Process name.
-l : percentage of CPU allowed from 0 to 100.
-P: absolute path name of the executable program file.

Root vs Normal User Account :–> cpulimit should run at least with the same user running the controlled process. But it is much better if you run cpulimit as root, in order to have a higher priority and a more precise control.

A Note About SMP (Multicore / MultiCpu) Systems :–> If your machine has one processor you can limit the percentage from 0% to 100%, which means that if you set for example 50%, your process cannot use more than 500 ms of cpu time for each second. But if your machine has four processors, percentage may vary from 0% to 400%, so setting the limit to 200% means to use no more than half of the available power. In any case, the percentage is the same of what you see when you run top.

July 13, 2015 Posted by | Tips & Tricks, Unix/Linux | , , | 1 Comment

How to read or view utmp, wtmp and btmp files in Linux

In Linux operating systems everything is logged some where. Most of the system logs are logged in to /var/log folder. This folder contains logs related to different services and applications. In this folder we have some files such as utmp, wtmp and btmp. These files contains all the details about login’s and logout’s which are from local as well as from remote systems and system status such as uptime etc.

utmp will give you complete picture of users logins at which terminals, logouts, system events and current status of the system, system boot time (used by uptime) etc.

wtmp gives historical data of utmp.

btmp records only failed login attempts.

# last ( Provide how logged in, when they logged in and when they logged out etc info on the screen.)

# last -f /var/log/wtmp (To open wtmp file and view its content use blow command)

# last -f /var/run/utmp (To see still logged in users view utmp file use last command)

# last -f /var/log/btmp (To view btmp file use same command)

August 10, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , , | Leave a comment

Difference between /dev/null and /dev/zero files

/dev/zero and /dev/null are two pseudo files which are useful for creating empty files. Many people consider there is no difference or puzzled with what could be the difference between two files. There is considerable difference when writing data using these hardware files.

/dev/zero: This file is used to create a file with no data but with required size(A file with all zero’s). In other words this will create a data file with all zeros in the file which will give the size to a file. If you see the strace command on /dev/zero, it’s continuous zeros which it will write to a file. So we conclude that /dev/zero is a file use full for creating a file with some required size without any meaning to the data.

/dev/null: This is one more Pseudo file which is useful in many places like redirecting unwanted output/error etc to this file. This file acts as a black hole(Which eat up everything and do not show any output). So whenever you feed some data to this file, you can not retrieve the data which is fed to it. This file even useful for creating files with zero size.

July 30, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

Why swap is double the size of the RAM in Linux

It has a meaning and for that we should know memory hierarchy. We have different levels of memory which is useful for processing your data. They are as follows.

  • Processor/CPU registers(Bits in size)
  • L1 Cache(kbs in size)
  • L2 Cache(MBs in size)
  • L3 cache(100s of MBs in size)
  • RAM(GB’s in size)

suppose take one application which is in running state. The application data is moved to RAM for faster accessing and some of its data is moved to L3 cache for processing frequently used data and we move more frequently used app data to L2 and then L1. And we move data which is right now processed by your processor/CPU to registers which is present in processor. If you observe the memory size will gradually decrease when you go up in the ladder more frequently used data is kept at higher levels and made it self available for processing. So if suppose you want to load a bigger program which can consume all the ram/L3/L2/L1 cache its better to move already running process to some location which we can access later to process it once again. But moving this data to HDD is not preferred as it is bit slow and other solution is to increase the RAM size which bit cost. So people came with a solution called SWAP partition which can solve both these limitations.

We create swap partition to cache all the data in registers, L1, L2, L3 caches and RAM.

Then why we create two times the RAM :–>  The reason behind it is that if you club all the above memory locations, the total size will be between 1.5 times to 2 times the RAM. This is the reason behind the thumb rule to create swap size 2 times the RAM

If I create swap size more than two times the RAM, What will happen –>  As we said earlier swap is used to move data from different memories and their total size is always 1.5 times to 2 times maximum so it’s of no use if you create swap size more than 2 times the RAM. In other words, all the space more than 2 times the RAM in swap is no use.

I have RAM size of 128GB and my RAM usage never fills up, do I have to create swap partition –> No, not at all required. As mention earlier swap is a temporary space to store all your memories data for future processing, if your RAM is free then there is no use of swap partition.

July 20, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

Why we can create only up to 4 primary partitions

Actually in basic hard disk, we can create 4 partition(either primary or extended), we can create

  • 4 primary maximum or
  • 3 primary + 1 extended or
  • 2 primary + 1 extended or
  • 1 primary + 1 extended

not more then that? why and what is the reason?

The reason is because of a limitation of the MBR (Master Boot Record- the first sector of the hard disk.) The MBR is only 512bytes of size, it is needed to store the primary boot loader, and the partition table. Typically, the area reserved for partition table is only 64 bytes. And the partition table entry for one partition is 16 bytes. So, 16×4=64. The space is over. so we cant create more than this.

July 10, 2014 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

Difference between NAS & SAN

S.No. NAS – Network Attached Storage SAN – Storage Area Network
1 It can be mapped as network drives to share on that server. It can be used as a ‘disk in disk’ and volume management utilities.
2 It provides storage and a file system. It provide Block based storage.
3 Protocols –> NFS, CIFS & SMB Protocols –> SCSI, Fibre Channel, iSCSI, ATA over Ethernet & HyperSCSI
4 For small & medium business For large organizations
5 Storage Capacity –> up to few TB Storage Capacity –> Many TB
6 Multiple NAS Devices Single SAN & Multiple high performance disk arrays
7 Specialized knowledge and training is not required to configure and maintain NAS. Specialized knowledge and training is required to configure and maintain SANs.
8 NAS & SAN are not mutually exclusive. A hybrid of SAN and NAS can offer file and block level protocols from the same system to support NAS and SAN.

June 30, 2014 Posted by | SAN, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to find UUID of devices in Linux

This trick is for those who deal with many removable devices, with every device changing its name after you restart your systm. The solution is to mount devices by using their UUIDs (Universally Unique Identifier)

# blkid

/dev/sda1: UUID=”0bd49e6e-6692-410e-8408-5353905f5c42″ TYPE=”ext4″
/dev/sda2: UUID=”cea4d6d2-d624-4193-9576-67ec27b15796″ TYPE=”ext4″
/dev/sda3: UUID=”3b00985c-cf8d-4b87-be92-74186d0a2dcb” TYPE=”ext4″
/dev/sda5: UUID=”79b4e73d-d40d-428b-86a8-930506d86d8d” TYPE=”swap”

Which will show the UUIDs of devices, Now you can make an entry in the “/etc/fstab” file to avoid any problems.
This tip can also be applied in case of SAN storage, where every Logical Unit Number (LUN) needs to be mounted.

June 20, 2014 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

How to find Hardware information on Live (running) Servers & Systems

dmidecode is a tool for dumping a computer DMI (some say SMBIOS) table contents in a human-readable format. This table contains a description of the system hardware components, as well as other useful pieces of information such as serial numbers  and  BIOS  revision.  You can retrieve this information without having to probe for the actual hardware.  While this is a good point in terms of report speed and safeness, this also makes the presented information possibly unreliable.

The  DMI  table  does not  only describe what the system is currently made of, it also can report the possible evolutions (such as the fastest supported CPU or the maximal amount of memory supported).

SMBIOS stands for System Management BIOS, while DMI stands for Desktop Management Interface. Both standards are  tightly related and developed by the DMTF (Desktop Management Task Force).

# yum install dmidecode

# dmidecode –type bios [About BIOS Details]

# dmidecode –type system [About System Information]

# dmidecode –type baseboard [About NIC & Storage Card Information]

# dmidecode –type chassis [About Chassis Information]

# dmidecode –type processor [About Processor Information]

# dmidecode –type memory [About Memory Information]

# dmidecode –type cache [About Cache Information]

# dmidecode –type connector [About USB Information]

# dmidecode –type slot [About Slot Information]

June 10, 2014 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

How to kill Multiple Processes In Linux

There are many cases where you want to kill multiple processes that match a certain pattern in their command line strings. For example, suppose you want to kill all processes that are running commands with keyword “javaxyz” in their arguments.

# ps aux | grep javaxyz

Here is a single command that will kill all processes at once that are matched with grep.

# kill -9 `ps aux | grep javaxyz | grep -v grep | awk ‘{print $2}’`

The command line inside a pair of backtick characters (i.e., ps aux …. ‘{print $2}’) will print out a list of process IDs that are matched with grep. The result is then used by the outer command kill. The “grep -v grep” is to exclude a self match (i.e., grep command itself) from a list of matched processes.

One caveat with this command is that when you are running it in a shell script, make sure to use bash, not sh.

If you are running the following script with sh: you will get “kill: Illegal number:” error. The command line inside a pair of backticks is returning a multi-line response, and it appears that sh is not able to handle it. But bash can. So the following script should be okay.

#!/bin/bash
kill -9 `ps aux | grep javaxyz | grep -v grep | awk ‘{print $2}’`

May 30, 2014 Posted by | Security, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to disable WhatsApp Blue Ticks for Read Messages

Recently, WhatsApp quietly introduced a new feature that lets users know that their messages have been read, with the double grey ticks appearing in front of the messages turning blue and now, just as quietly, the mobile messaging service is letting users of its Android app disable the feature with the rollout of WhatsApp version 2.11.444. This version is available for users on WhatsApp’s website for now, and will eventually be rolled out to all users.

Here’s how you can disable the blue coloured read receipts’ with the message timestamp on WhatsApp’s Android app:-

1. Make sure that your smartphone is running on Android 2.1 or a newer version.

2. Go to settings menu and enable ‘Download from Unknown Sources‘ in the Security tab

3. Go to the WhatsApp website and download the APK (application) file available under http://www.whatsapp.com/android/

4. Once the APK file is downloaded to your device, tap the ‘Install‘ option.

5. Now that WhatsApp has been updated, select Settings –> Account –> Privacy.

6. Under the Privacy tab, uncheck the Read Receipts option.

whatsapp

This feature however doesn’t apply to group messages and will let other participants know when you’ve read a message. Also, once you disable this feature, you won’t be able to view blue double-check marks when you send a message as well. 


The feature that enabled the users to see when their messages were read didn’t go down too well with the users and with this update, WhatsApp seems to be taking steps to please users who were not happy with the new feature.

May 10, 2014 Posted by | Security, Tips & Tricks | , , | Leave a comment

How to Fix rpmdb: unable to join the environment

This post will probably help you to fix an error occurring when you try to upgrade some package like this:

rpmdb: unable to join the environment
error: db4 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages index using db3 – Resource temporarily unavailable (11)
error: cannot open Packages database in /var/lib/rpm
warning: /root/webmin-1.360-1.noarch.rpm: V3 DSA signature: NOKEY, key ID 11f63c51
rpmdb: unable to join the environment
error: db4 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages database in /var/lib/rpm
rpmdb: unable to join the environment
error: db4 error(11) from dbenv->open: Resource temporarily unavailable
error: cannot open Packages database in /var/lib/rpm

How To Fix :-

# rm -f /var/lib/rpm/__db*     (Remove Old Lock Files running this command)

# rpm -vv –rebuilddb     (Now you have to rebuild RPM database)

# yum clean all

# yum update all

April 30, 2014 Posted by | Tips & Tricks, Unix/Linux | , , | 2 Comments

How to add Cluster or Node in Proxmox VE

There are three Clusters or Notes as Following :-

Cluster-1 or Node-1 :- 192.168.1.10
Cluster-2 or Node-2 :- 192.168.1.11
Cluster-3 or Node-3 :- 192.168.1.12

All settings can be done via command pvecm (Proxmox Virtual Environment cluster manager)

Step-1 :- Create the Master Cluster

Login via ssh to the first Proxmox VE node or cluster. Use a unique name for your Cluster, this name cannot be changed later.

node-1# pvecm create node-1

node-1# pvecm status (To check the state of cluster)

Step-2 :- Adding nodes to the Cluster

Login via ssh to the other Proxmox VE nodes. Please note, the node cannot hold any same VM´s. (If yes you will get conflicts with identical VMID´s – to workaround, use vzdump to backup and to restore to a different VMID after the cluster configuration).

node-2# pvecm add 192.168.1.10 (Add a node)

node-2# pvecm status (To check or display the state of cluster:)

Version: 6.2.0
Config Version: 2
Cluster Name: node-1
Cluster Id: 6639
Cluster Member: Yes
Cluster Generation: 8
Membership state: Cluster-Member
Nodes: 2
Expected votes: 2
Total votes: 2
Node votes: 1
Quorum: 2
Active subsystems: 5
Flags:
Ports Bound: 0
Node name: node-2
Node ID: 2
Multicast addresses: 239.192.25.9
Node addresses: 192.168.1.11

node-2# pvecm nodes (Display the nodes of cluster)

Node  Sts   Inc   Joined               Name
1   M    156   2013-09-05 10:39:09  node-1
2   M    156   2013-09-05 10:39:09  node-2
3   M    168   2013-09-05 11:24:12  node-3

node-1# pvecm delnode node-2 (Remove a cluster node)

February 10, 2014 Posted by | Proxmox, Virtualization | , , | 4 Comments

Software RAID issue after reboot of OS

After successful installation of OS, if there are some problem to boot os in one Hard-drive, as grub issue like kernel panic or grub error 17 or other issue

1. Reboot the machine in Rescue mode

Boot: linux rescue

# chroot /mnt/sysimages
OR
# mkdir /a
# mount /dev/sdaX /a
# cd /a

# cat /proc/mdstat (UU means both disk are live)

2. Run the following commands to hotadd and rebuild the array

# mdadm /dev/md0 –add /dev/sda1
# mdadm /dev/md1 –add /dev/sda2
# mdadm /dev/md2 –add /dev/sda3

# grub-install /dev/md0    OR

# grub (GRUB shell type above command to reinstall the boot loader on both drives & reboot.)

grub> device (hd0) /dev/sda
grub> root (hd0,0)
grub> setup (hd0)

grub> device (hd1) /dev/sdb
grub> root (hd1,0)
grub> setup (hd1)

grub> quit

This is show on /boot/grub/device.map

grub> find /grub/stage1 (Find out which are the hard disks on which you can install grub)

This file show in /boot/grub/device.map

grub> device (hd0) /dev/sda
grub> root (hd0,0)
grub> setup (hd0)

January 20, 2014 Posted by | RAID, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Check or Monitoring Software RAID in Linux

# mdadm

# cat /proc/mdstat

# mdadm –detail /dev/md0 OR /dev/md1

# mdadm -E /dev/md0 (Spare rebuilding means sync & recovery data to each drive to other)

# mdadm -R /dev/md1 (Recovery & transfer in speed 20-40 Mbps depend on CPU & Hard-drive)

# mdadm /dev/md0 -add /dev/sdb1 [Add partition (RAID Device) /dev/sdb1 into /dev/md0]

January 10, 2014 Posted by | RAID, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Create Software RAID-1 Mirroring

# yum install mdadm

# fdisk /dev/sdb     (Create Software RAID Type)

# fdisk /dev/sdc     (Create Software RAID Type)

# mdadm –zero-superblock /dev/sdb /dev/sdc     (If device contains a valid md superblock, the block is overwritten with zeroes)

# mdadm –create /dev/md0 –level=1 –raid–devices=2 /dev/sdb1 /dev/sdc1     (Create RAID-1 using /dev/sdb1 & /dev/sdc1)

# mkfs.ext3 /dev/md0     (format /dev/md0 as ext3 file system)

# mkdir /raid1

# mount /dev/md0 /raid1

# df -hT

# vim /etc/fstab     (For automatically mount after reboot)

/dev/md0    /raid1    ext3    noatime,rw    0    0

# cat /proc/mdstat OR

# watch -n 2 cat /proc/mdstat (For RAID Status)

December 30, 2013 Posted by | RAID, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to mount USB Pendrive only by name not /dev/sdb1

# fdisk -l (show USB Pendrive device, Suppose its /dev/sdb1)

# cd /etc/udev/rules.d/

# vim 99-usb.rules

KERNEL = “sdb1”, NAME=UnixServerAdmin

# reboot (Restart the machine to apply it)

# mount /dev/sdb1 /mnt
Not mount device is not exit

#mount /dev/UnixsServerAdmin /mnt

Its mount, Have Fun !!!

December 20, 2013 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Compile Linux Kernel

Configure Yum Repository in local machine, So we can install following packages :-

# yum install kernel-header* redhat-rpm-config-* unidef* rpm-build*

# yum install ncurse* (for menuconfig)

# yum install gcc* (for gconfig)

# yum install qt* glibc* gtk* (for xconfig)

# useradd brewbuilder

# groupadd brewbuilder

# tar -xvf  kernel-2.x.tar.gz

# cd kernel-2.x

# make menuconfig OR make gconfig OR make xconfig

# make (Build the actual kernel & modules)

# make modules_install (Installthe modules under /lib/modules/<kernel-version>)

# make install (Install the Kernel in/boot & adds a GRUB entry in the file)

Check grub entry in /etc/grub.conf file and Rrboot the machine with new compile kernel.

December 10, 2013 Posted by | Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to enable case insensitive in Bash terminal

# set completion-ignore-case on

OR

You need to add set completion-ignore-case on to below file and append text as follows :-

# vim /etc/inputrc

set completion-ignore-case on

November 30, 2013 Posted by | Tips & Tricks, Unix/Linux | , , | Leave a comment

How to Extend or increase size of LVM partitions in RHEL/CentOS

# /usr/sbin/lvextend -L+50G  /dev/mapper/VolGroup00-LogVol04

  Extending logical volume LogVol04 to 149.75 GB
  Logical volume LogVol04 successfully resized

# /usr/sbin/vgdisplay

# /sbin/resize2fs /dev/mapper/VolGroup00-LogVol04

resize2fs 1.39 (29-May-2006)
Filesystem at /dev/mapper/VolGroup00-LogVol04 is mounted on /var; on-line resizing required
Performing an on-line resize of /dev/mapper/VolGroup00-LogVol04 to 39256064 (4k) blocks.
The filesystem on /dev/mapper/VolGroup00-LogVol04 is now 39256064 blocks long.

Its Done !!!

November 10, 2013 Posted by | LVM, Tips & Tricks, Unix/Linux | , , , | Leave a comment

How to Shrink or reduce size of LVM partitons in RHEL/CentOS

1. Check disk partitions size

# df -hT

Filesystem    Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00 ext3 34G 6.6G 26G 21% /
/dev/mapper/VolGroup00-LogVol03 ext3 34G 29G 27G 52% /usr
/dev/mapper/VolGroup00-LogVol04 ext3 97G 89G 2.7G 98% /var
/dev/sda1 ext3 99M 27M 67M 29% /boot
tmpfs tmpfs 56G 0 56G 0% /dev/shm
/dev/mapper/VolGroup00-LogVol02 ext3 56G 9.1G   65G 13% /backup

2. Unmount the partitions that want to shrink or reduce

# umount /dev/mapper/VolGroup00-LogVol02

3. Check a Linux ext2/ext3/ext4 file system partition.

# /sbin/e2fsck -f /dev/mapper/VolGroup00-LogVol02

e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
/lost+found not found.  Create<y>? yes
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/VolGroup00-LogVol02: ***** FILE SYSTEM WAS MODIFIED *****
/dev/mapper/VolGroup00-LogVol02: 2887/20971520 files (14.9% non-contiguous), 3019764/20971520 blocks

4. Resize the partition to 30 GB

# /sbin/resize2fs -p /dev/mapper/VolGroup00-LogVol02 30G

resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/mapper/VolGroup00-LogVol02 to 7864320 (4k) blocks.
Begin pass 2 (max = 1690914)
Relocating blocks             XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 3 (max = 640)
Scanning inode table          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Begin pass 4 (max = 201)
Updating inode references     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/mapper/VolGroup00-LogVol02 is now 7864320 blocks long.

5. Reduce the size of a logical volume upto 30 GB

# /usr/sbin/lvreduce -L 30G /dev/mapper/VolGroup00-LogVol02

  WARNING: Reducing active logical volume to 30.00 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
  Do you really want to reduce LogVol02? [y/n]: y
  Reducing logical volume LogVol02 to 30.00 GB
  Logical volume LogVol02 successfully resized

6. Recheck  again, Linux ext2/ext3/ext4 file system partition.

# /sbin/e2fsck -f /dev/mapper/VolGroup00-LogVol02

e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/VolGroup00-LogVol02: 2887/7864320 files (14.9% non-contiguous), 2606289/7864320 blocks

7. Resize the partition to 30 GB

# /sbin/resize2fs -p /dev/mapper/VolGroup00-LogVol02

resize2fs 1.39 (29-May-2006)
The filesystem is already 7864320 blocks long.  Nothing to do!

8. Now, Mount the partition.

# mount /dev/mapper/VolGroup00-LogVol02  /backup

9. Again, Check disk partitions size

# df -hT

Filesystem    Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00 ext3 34G 6.6G 26G 21% /
/dev/mapper/VolGroup00-LogVol03 ext3 34G 29G 27G 52% /usr
/dev/mapper/VolGroup00-LogVol04 ext3 97G 89G 2.7G 98% /var
/dev/sda1 ext3 99M 27M 67M 29% /boot
tmpfs tmpfs 56G 0 56G 0% /dev/shm
/dev/mapper/VolGroup00-LogVol02 ext3 30G 9.1G   19G 33% /backup

Its Done !!!

October 30, 2013 Posted by | LVM, Tips & Tricks, Unix/Linux | , , , | Leave a comment