Skip to content

Posts from the ‘Commands’ Category

7
Jan

Create bootable USB for Network Installation

Nowadays in most of all the environments Linux systems are used, so there comes the need of installation over the network and here we  need a bootable CD (Boot CD). In common these CDs are know as “netinstall” CD’s. A boot CD is one which contains only the resources for booting up a system and the essentials packages that supports networking. Sometimes we need in a boot CD and it may be required for only time use here we can use the bootable USB (OS boot from USB).

Commonly in RHEL and CentOs the installation tree consists of a directory called “images”, here we can find the file named “boot.img” or in another name “bootdisk.img”.

We can use these images with the “dd” command to make the USB stick bootable. Consider the file as boot.img,

Steps to make bootable USB stick.

dd if=<path to boot.img> of=<usb device node>
example: dd if=boot.img of=/dev/sdb
6
Jan
find-picutr

Locating files using the find command with 15 examples

Find is a versatile tool which can be used to locate files and directories satisfying different user criteria. But the sheer number of options for this command line tool makes it at the same time both powerful and encumbering for the user. Here I will list a few combinations which one can use to get useful results using find command.

Find all HTML files starting with letter ‘a‘ in your current directory (Case sensitive)

$ find . -name a\*.html

Same as above but case insensitive search.

$ find . -iname a\*.html

Find files which are larger than 5 MB in size.

$ find . -size +5000k -type f

Here the ‘+’ in ‘+5000k’ indicates greater than and k is kilobytes. And the dot ‘.’ indicates the current directory. The -type option can take any of the following values:

    f - file
    d - directory
    l - symbolic link
    c - character
    p - named pipe (FIFO)
    s - socket
    b - block device

Find all empty files in your directory

$ find . -size 0c -type f

… Which is all files with 0 bytes size. The option -size can take the following:

    c - bytes
    w - 2 byte words
    k - kilo bytes
    b - 512 byte blocks

Note: The above command can also take the -empty parameter.

Find is very powerful in that you can combine it with other commands. For example, to find all empty files in the current directory and delete them, do the following:

$ find . -empty -maxdepth 1 -exec rm {} \;

To search for a html file having the text ‘Web sites‘ in it, you can combine find with grep as follows:

$ find . -type f -iname \*.html -exec grep -s "Web sites" {} \;

The  -s option in grep suppresses errors about non-existent or unreadable files. And {} is a placeholder for the files found. The semicolon ‘;’ is escaped using backslash so as not to be interpreted by bash shell.

Note: You can use the -exec option to combine any command in Linux with the find command. Some of the useful things you can do with it are as follows:

Compress log files on an individual basis

$ find /var -iname \*.log -exec bzip {} \;

Find all files which belong to user pravi and change its ownership to pcuser

# find / -user pravi -exec chown pcuser {} \;

Note: You can also use xargs command instead of the -exec option as follows:

$ find /var -iname \*.log | xargs bzip –

Find all files which do not belong to any user:

$ find . -nouser

Find files which have permissions rwx for user and rw for group and others :

$ find . -perm 766

List the output of the above command  find . -perm 766

$ find . -perm 766 -exec ls -l {} \;

Find all directories with name bug_files

$ find . -type d -iname \*bug_files\*

Suppose you want to find files of size between 800k and 900k, do the following:

$ find . \( -size +800k -and -size -900k \)

And how about getting a formatted output of the above command with the size of each file listed ?

$ find . \( -size +700k -and -size -1000k \) -exec du -Hs {} \; 2>/dev/null

Here, the ‘2>/dev/null’ means all the error messages are discarded or suppressed.

You can also limit your search by file system type. For example, to restrict search to files residing only in the NTFS and VFAT filesystem, do the following:

$ find / -maxdepth 2 \( -fstype vfat -or -fstype ntfs \) 2> /dev/null

These are the most common uses of the find command. You can see additional uses by reading the find manual.

rangnathan.p.s
4
Jan
Information

10 System info Commands

10 System info Commands

If you are developing an application for Linux desktop and would like to automatically find out more information about system, use the following commands in shell scripts to gather information about system. Before applying a command you must know how to use the command effectively in Shell. Here we are explaing basich linux system commands and their usage.

1.pwd

This command will help you to find out your present working direcotry.

Command Usage:

[root@l3admins ~]# pwd
/root
[root@l3admins ~]#

2. hostname

The kernel maintains the current (unqualified) hostname of the system. Before trying to set your hostname, it’s important to know how to tell when you’ve got it right. Here are a few of the numerous ways to obtain it:

Command Usage:

[root@l3admins ~]# hostname
l3admins
[root@l3admins ~]#

3. whoami

This will display the login name like admin,root or your user name.

Command Usage:

[root@l3admins ~]# whoami
root
[root@l3admins ~]#

4. id
This command will display all the informations about a user like user ID, memeber of which Group and prints other user identity information

Command Usage:

 [root@l3admins ~]# id root
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel) context=root:system_r:unconfined_t:SystemLow-SystemHigh
[root@l3admins ~]#

5. date

This command will display the current date and timezone of your machine

Command Usage:

[root@l3admins ~]# date
Tue Jan  4 01:01:14 PST 2011
[root@l3admins ~]#

6. W

Its a command which conveys so many informations like load, number of users logged in, Uptime for the system.

[root@l3admins ~]# w
 01:46:06 up 59 min,  3 users,  load average: 0.00, 0.00, 0.04
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       -                00:49   ?xdm?  25.17s  0.39s /usr/bin/gnome-session
root     pts/1    :0.0             00:50   43:23   0.12s  0.12s bash
root     pts/2    192.168.10.1     01:03    0.00s  0.16s  0.01s w
[root@l3admins ~]#

7. finger

This is a user information lookup program, Finger displays the userâs login name, real name, terminal name and write status (as a ââ*ââ after the terminal name if write permission is denied), idle time, login time, office location and office phone number.

Command Usage:

[root@l3admins ~]# finger root
Login: root                             Name: root
Directory: /root                        Shell: /bin/bash
On since Tue Jan  4 00:49 (PST) on :0 (messages off)
On since Tue Jan  4 00:50 (PST) on pts/1 from :0.0
   49 minutes 26 seconds idle
On since Tue Jan  4 01:03 (PST) on pts/2 from 192.168.10.1
New mail received Fri Dec 31 02:30 2010 (PST)
     Unread since Sun Dec  5 08:36 2010 (PST)
No Plan.
[root@l3admins ~]#

8. last

This command will bring you the information about the last loggedin user.

Command Usage:

[root@l3admins ~]# last
root     pts/2        192.168.10.1     Tue Jan  4 01:03   still logged in
root     pts/1        :0.0             Tue Jan  4 00:50   still logged in
root     :0                            Tue Jan  4 00:49   still logged in
root     :0                            Tue Jan  4 00:49 - 00:49  (00:00)
reboot   system boot  2.6.18-194.el5   Tue Jan  4 06:18          (-4:-23)

9. lastb

This command will give you the information about the last bad (unsuccessful) login attempts to the machine.

Command Usage:

[root@l3admins ~]# lastb
root     ssh:notty    192.168.10.1     Tue Jan  4 01:58 - 01:58  (00:00)
root     ssh:notty    192.168.10.1     Tue Jan  4 01:58 - 01:58  (00:00)

btmp begins Tue Jan  4 01:58:31 2011
[root@l3admins ~]#

10. free

Memory info (in kilobytes). “Shared” memory is the memory that can be shared between processes (e.g., executable code is “shared”). “Buffered” and “cashed” memory is the part that keeps parts of recently accessed files – it can be shrunk if more memory is needed by processes.

Command usage:

[root@l3admins ~]# free
             total       used       free     shared    buffers     cached
Mem:       1035108     655116     379992          0      88648     410024
-/+ buffers/cache:     156444     878664
Swap:      2096472          0    2096472
[root@l3admins ~]#
20
Dec

rpm basics

The RedHat Package Manager (RPM) is a popular package management system. Using RPM it’s easier to install, uninstall, and upgrade software packages. Apart from that, it’s possible to query a package for details and also can verify the integrity of installed packages.

Mainly there are two types of RPM packages

 1) Binary packages
 2) source packages

Binary package has been compiled for a particular architecture and is ready for installation, whereas source package contains source files to build a binary package.
Here am giving some examples for RPM installing, uninstalling, upgrading, querying, and verifying software packages.

Naming Convention used in RPM

Consider the Apache RPM package –> httpd-2.0.53-3.3.i386.rpm

  name--version--release--arch--.rpm

httpd –> name of the software
2.0.53 –> software version (that of the original software source)
3.3 –> package release number
i386 –> architecture (like 32 or 64 bit)

Corresponding source package is httpd-2.0.53-3.3.src.rpm (note ’src’ in the name).

RPM Instalaltion


To install a package, we mainly use the “-ivh” option

$ rpm -ivh httpd-2.0.53-3.3.i386.rpm

Preparing… ########################################### [100%]
1:httpd ########################################### [100%]

Flags used are;

-v flag can be used to introduce some verbosity,
-h flag displays hash marked progress of the installation.
-i installing

RPM Installation over Network

$ rpm -ivh ftp:////package.rpm

$ rpm -ivh http:////package.rpm

Uninstalling RPM
To Uninstall an RPM, Use the -e option

$ rpm -e httpd-2.0.53-3.3.i386.rpm

Just have to specify name of the package to be removed.

If the pakages are not removed and it shows some dependencies over other installed packages, To ignore the error and uninstall the package use the following option,

rpm -e --nodeps httpd-2.0.53-3.3.i386.rpm

–nodeps –> Do Not Check Dependencies Before Erasing Package

“This is not a recomendaded usage, since it can break other installed packages”

Hope that this will help 🙂

18
Dec

Increase semaphores count in Linux

Semaphores can be thought of as simple counters that indicate the status of a resource. This counter is a protected variable and cannot be accessed by the user directly. The shield to this variable is provided by none other than the kernel. The usage of this semaphore variable is simple. If counter is greater that 0, then the resource is available, and if the counter is 0 or less, then that resource is busy or being used by someone else. This simple mechanism helps in synchronizing multithreaded and multiprocess based applications. Semaphores were invented and proposed by Edsger Dijkstra, and still used in operating systems today for synchronization purposes. The same mechanism is now available for application developers too. Its one of the most important aspects of inter-process communication.

Increase the maximum amount of semaphores

Before increasing the Semaphore value, check whats the current value using by the system.

[l3admins@localhost ~]# /sbin/sysctl -a | grep sem
kernel.sem = 250        32000   32      128
[root@l3admins ~]#

How much IPC resources are used can be found using ‘ipcs’ command:

# ipcs -a
...
------ Semaphore Arrays --------
key        semid      owner      perms      nsems
0x00000000 201293824  Exim    600            1

To increase the semaphores execute the following command and after that update the same vale in /etc/sysctl.conf file to reflect the same after the reboot

/sbin/sysctl -w kernel.sem=250

See ‘man ipcrm for more information.

14
Dec

How to clear out the cache being used

Linux has a good memory management feature that will use up any “extra” RAM you have to cache stuff. This section of the memory being used is SUPPOSED to be freely available to be taken over by any other process that actually needs it. This will eventually reduce the performance of your system. Following command will help you.

[root@l3admins]# free -m
total       used       free     shared    buffers     cached
Mem:          3990       3181        809          0        448       2228
-/+ buffers/cache:        503       3486
Swap:         9452          0       9452
[root@l3admins]#

Use the following command as root to clear cache from your machine.

sync; echo 3 > /proc/sys/vm/drop_caches
This will only support Kernels 2.6.16 and newer.

The above command will cleared out the cache in your machine.

12
Dec

Convert deb package to rpm

DEB packages are the installers for Debian and Ubuntu like RPM for Linux.

Converting of  DEB to RPM is simple one step  process with  “alien” command in Ubuntu/Debain, for this one first we need to install alien.

Install alien command on Debian / Ubuntu

To install alien package you need root privilege.

# apt-get install alien

Once the alien package is installed we can start the converting of DEB to RPM.

# alien your-DEB-to-convert.deb

your-DEB-to-convert.rpm generated

Now you can start the installation of the RPM.

12
Dec

Convert rpm packages to deb

DEB packages are the installers for Debian and Ubuntu like RPM for Linux, if we need to install one RPM on Ubuntu/Debian we need to convert as DEB package.

Converting of RPM to DEB is simple one step process with “alien” command in Ubuntu/Debain, for this one first we need to install alien.

Install alien command on Debian / Ubuntu

To install alien package you need root privilege.
# apt-get install alien

Once the alien package is installed we can start the converting of RPM to DEB

# alien your-RPM-to-convert.rpm
 your-RPM-to-convert.deb generated

Now the .deb package is created, now you can install it in Ubuntu/Debain.