Note: I have used my device name and number in examples, you have to change it
as your device name and number, otherwise unwanted device data will be wiped
out.
Put your USB flash drive in one of your computer's free USB ports. After a few
seconds, Linux will automatically recognize it and an icon for it will appear on
your desktop. If pen drive doesn't mounted automatically, login as root and try
to mount it, still it is not mounted there may be a problem with pen drive or it
is defective.Finding device:
Very very first thing is you should to know what device you are working with.
For that you can use any one of the following methods.Method 1: Using ls /dev/sd* command
First make sure your usb drive is not plugged in. Then run:[venu@ras ~]$ ls /dev/sd*
/dev/sda /dev/sda10 /dev/sda12 /dev/sda2 /dev/sda6 /dev/sda8
/dev/sda1 /dev/sda11 /dev/sda13 /dev/sda5 /dev/sda7 /dev/sda9
Once you have done that, put in your pen drive and run the above command again.[venu@ras ~]$ ls /dev/sd*
/dev/sda /dev/sda10 /dev/sda12 /dev/sda2 /dev/sda6 /dev/sda8 /dev/sdb
/dev/sda1 /dev/sda11 /dev/sda13 /dev/sda5 /dev/sda7 /dev/sda9 /dev/sdb1
Whatever drive (ex sdb) was not there before is your pen drive. From the above
list my pen drive is sdb. Don't be confuse with /dev/sdb and /dev/sdb1, sdb is
a drive and sdb1 is first partition in that drive.Method 2: Using df command
[venu@ras ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda10 29753556 8008860 20208884 29% /
/dev/sda12 19840892 1528664 17288088 9% /msfiles
/dev/sda11 21825108 16772836 3925716 82% /venu
tmpfs 1032232 0 1032232 0% /dev/shm
/dev/sdb1 1947636 4 1947632 1% /media/VENU
As you can see above my pen drive (/dev/sdb1) is mounted at /media/VENU. If you
have any doubt about which one is your pen drive, do same thing as in method1.
Run df command before and after inserting pen drive.Method 3: dmesg | tail
[venu@ras ~]$ dmesg | tail
sdb: assuming drive cache: write through
SCSI device sdb: 3903359 512-byte hdwr sectors (1999 MB)
sdb: Write Protect is off
sdb: Mode Sense: 0b 00 00 08
sdb: assuming drive cache: write through
sdb: sdb1
sd 8:0:0:0: Attached scsi removable disk sdb
sd 8:0:0:0: Attached scsi generic sg1 type 0
usb-storage: device scan complete
SELinux: initialized (dev sdb1, type vfat), uses genfs_contextsMethod 4: mount -l
[venu@ras ~]$ mount -l
/dev/sda10 on / type ext3 (rw) [/]
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda12 on /msfiles type ext3 (rw) [/msfiles1]
/dev/sda11 on /venu type ext3 (rw) [/venu1]
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /media/VENU type vfat (rw,nosuid,nodev,shortname=winnt,uid=500)
[VENU]Method 5: fdisk -l
fdisk command need root privileges. So login as root and run the command:[root@ras ~]# fdisk -l
Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 5099 40957686 7 HPFS/NTFS
/dev/sda2 5100 60800 447418282+ f W95 Ext'd (LBA)
/dev/sda5 5100 10198 40957686 7 HPFS/NTFS
/dev/sda10 50994 54817 30716248+ 83 Linux
/dev/sda11 54818 57622 22531131 83 Linux
/dev/sda12 57623 60172 20482843+ 83 Linux
/dev/sda13 60173 60694 4192933+ 82 Linux swap / Solaris
Disk /dev/sdb: 1998 MB, 1998519808 bytes
32 heads, 63 sectors/track, 1936 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 1936 1951456+ b W95 FAT32
fdisk -l displays all detected drives even drive is unmounted.Formatting pen drive:
First decide which file system you want. FAT32 is probably the file system you
want, but that's up to you. Ext3 may not be work on windows, It asks for fomatt-
ing.1.Once you have Identified your pen drive, unmount it.
[root@ras ~]# umount /dev/sdb12.Format pen drive now.
Depending on distribution different commands are available for formatting, but
in general most used ones that I have seen are mkdosfs or mkfs.vfat for FAT32
and mke2fs or mkfs.ext3 for Linux filesystems.For Ms-Dos or Windows file system:
Use mkdosfs -n "Label" -I /dev/sdx, Replace Label with the name you want the
pen drive to have and /dev/sdx with your drive.[root@ras ~]# mkdosfs -n "KVMREDDY" -I /dev/sdb1or
mkdosfs 2.11 (12 Mar 2005)[root@ras ~]# mkfs.vfat -n "KVMREDDY" -I /dev/sdb1
mkfs.vfat 2.11 (12 Mar 2005)
(Note: I – capital letter of i )
If you don't want any Label then no need to supply option n. switch I will
force mkdosfs to work properly but it is not mandatory, you can remove that
also.For Linux File system:
Use mkfs.ext3 -L "Label" /dev/sdx for ext3 file system or mke2fs -L 'Label'
/dev/sdx for ex2, Replace Label with the name you want the pen drive to have
and /dev/sdx with your drive.
Ext3 file system:[root@ras ~]# mkfs.ext3 -L "KVMREDDY" /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=KVMREDDY
OS type: Linux
.............................
[root@ras ~]#
ext2 file system:[root@ras ~]# mke2fs -L 'VENU' /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=VENU
OS type: Linux
...........................
[root@ras ~]#
After formatting you will be returned to the prompt then remove and insert the
pen drive to have mounted again.
Friday, November 18, 2011
How to format Flash/Pen Drive in Linux
Labels:
HowTo's
Saturday, October 8, 2011
How to Remove RPM GPG Public Key
Public keys can be erased after importing just like packages. Here's how to
remove the CentOS GPG public key:
rpm -e <GPG public key>[root@localhost]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm -e gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm -qa gpg-pubkey*
[root@localhost]#If there are duplicates exist:
For example, Import the same key two times[root@localhost]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
[root@localhost]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
[root@localhost]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
gpg-pubkey-e8562897-459f07a4
Now try to remove Public Key, You will get error message like this:[root@localhost]# rpm -e gpg-pubkey-e8562897-459f07a4
error: "gpg-pubkey-e8562897-459f07a4" specifies multiple packagesHow to solve this:
Use --allmatches switch along with -e, It removes all versions of the package
which match PACKAGE_NAME(Public Key ).[root@localhost]# rpm -e --allmatches gpg-pubkey-e8562897-459f07a4
[root@localhost]# rpm -qa gpg-pubkey*
[root@localhost]#
Labels:
HowTo's
Friday, October 7, 2011
How to Import RPM GPG Key
Note: All the commands tested on CentOs 5.x. Your output may be vary depending
on distribution and version, so your results may not always look exactly like
the listings and figures shown here. Almost all everything works well on
RHEL/CentOs/Fedora.Why to check signature of an rpm:
The signature confirms that the package was signed by an authorized party and
also confirm the integrity and origin of your file. It is extremely important
to verify the signature of the RPM files before installing them to ensure that
they have not been altered from the original source of the packages.Checking a package's Signature:
The --checksig(or -K) option checks all the digests and signatures contained
in PACKAGE_FILE to ensure the integrity and origin of the package. Note that
signatures are now verified whenever a package is read, and --checksig is useful
to verify all of the digests and signatures associated with a package.
If you wish to verify that a package has not been corrupted or tampered with,
examine only the md5sum by typing the following command at a shell prompt (where
<rpm-file> is the file name of the RPM package):
rpm -K --nosignature <rpm-file>
The message <rpm-file>: md5 OK is displayed. This brief message means
that the file was not corrupted by the download. To see a more verbose message,
replace -K with -Kvv in the command.
For demonstration purpose I downloaded createrepo package from CentOs mirror
and used in examples.[root@localhost ~]# rpm -K --nosignature createrepo-0.4.11-3.el5.noarch.rpm
createrepo-0.4.11-3.el5.noarch.rpm: sha1 md5 OK
On the other hand, how trustworthy is the developer who created the package? If
the package is signed with the developer's GnuPG key,you know that the developer
really is who they say they are.
An RPM package can be signed using Gnu Privacy Guard (or GnuPG), to help you
make certain your downloaded package is trustworthy. GnuPG is a tool for secure
communication; it is a complete and free replacement for the encryption technol-
ogy of PGP, an electronic privacy program. With GnuPG, you can authenticate the
validity of documents and encrypt/decrypt data to and from other recipients.
GnuPG is capable of decrypting and verifying PGP 5.x files as well.
During installation,GnuPG is installed by default. That way you can immediately
start using GnuPG to verify any packages that you receive from CentOs(RHEL/Fedor
a). Before doing so, you must first import CentOs's public key. If you not impo-
rted correct public key, you will get following error message.[root@localhost ~]# rpm -K createrepo-0.4.11-3.el5.noarch.rpm
createrepo-0.4.11-3.el5.noarch.rpm: (SHA1) DSA sha1 md5 (GPG) NOT OK (MISSING
KEYS: GPG#e8562897)
Here the GPG in parentheses indicates that there's a problem with the signature,
and the message at the end of the line (MISSING KEYS) shows what the problem is.
Basically, RPM asked GPG to verify the package against a key(GPG#e8562897) that
GPG didn't have, and GPG complained. It means you missed the correct public key.How to import public keys:
Digital signatures cannot be verified without a public key. An ascii armored
public key can be added to the rpm database using --import. An imported public
key is carried in a header, and key ring management is performed exactly like
package management. For example, all currently imported public keys can be
displayed by:
rpm -qa gpg-pubkey*
To verify CentOs (RHEL/Fedora) packages, you must import the CentOs(RHEL/Fedora)
GPG key. To do so, execute the following command at a shell prompt:[root@localhost ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
This will create duplicate copies if one already exists.
To display a list of all keys installed for RPM verification,execute the command[root@localhost ~]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
or
RPM has the capacity to retrieve the key from a Mirror:[root@ ~]# rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5
[root@ ~]# rpm -qa gpg-pubkey*
gpg-pubkey-e8562897-459f07a4
gpg-pubkey-e8562897-459f07a4Note: Depending on distribution and version, you have to change mirror link.
OK, public key imported, now check signature of the createrepo rpm.[root@localhost ~]# rpm -K createrepo-0.4.11-3.el5.noarch.rpm
createrepo-0.4.11-3.el5.noarch.rpm: (sha1) dsa sha1 md5 gpg OK
This means that the signature of the package has been verified, and that it is
not corrupted. If you want to know public key builder's name , execute the
command:[root@~]# rpm -qa gpg-pubkey\* --qf "%{name}-%{version}-%{release}-%{summary}\n"
gpg-pubkey-e8562897-459f07a4-gpg(CentOS-5 Key (CentOS 5 Official Signing Key)
<centos-5-key@centos.org>)
gpg-pubkey-e8562897-459f07a4-gpg(CentOS-5 Key (CentOS 5 Official Signing Key)
<centos-5-key@centos.org>)
gpg-pubkey-2689b887-42315a9a-gpg(Hewlett-Packard Company (HP Codesigning Service
))Note: For showing difference I imported HP GPG key.
If you're the curious type and you want to know more information about imported
GPG key, use the following command.
rpm -qi <gpg-pubkey>[root@localhost data]# rpm -qi gpg-pubkey-e8562897-459f07a4
Name : gpg-pubkey Relocations: (not relocatable)
Version : e8562897 Vendor: (none)
Release : 459f07a4 Build Date: Fri 07 Oct 2011 05:53:03 PM IST
Install Date: Fri 07 Oct 2011 05:53:03 PM IST Build Host: localhost
Group : Public Keys Source RPM: (none)
Size : 0 License: pubkey
Signature : (none)
Summary : gpg(CentOS-5 Key (CentOS 5 Official Signing Key) <centos-5-key@
centos.orgi>)
Description :
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: rpm-4.4.2.3 (NSS-3)
mQGiBEWfB6MRBACrnYW6yKMT+MwJlCIhoyTxGf3mAxmnAiDEy6HcYN8rivssVTJk
CFtQBlBOpLV/OW2YtKrCO2xHn46eNfnMri8FGT8g+9JF3MUVi7kiV1He4iJynHXB
+F2ZqIvHf3IaUj1ys+p8TK64FDFxDQDrGQfIsD/+pkSGx53/877IrvdwjwCguQcr
Ioip5TH0Fj0OLUY4asYVZH8EAIqFHEqsY+9ziP+2R3/FyxSllKkjwcMLrBug+cYO
LYDD6eQXE9Mq8XKGFDj9ZB/0+JzK/XQeStheeFG75q3noq5oCPVFO4czuKErIRAB
qKbDBhaTj3JhOgM12XsUYn+rI6NeMV2ZogoQCC2tWmDETfRpYp2moo53NuFWHbAy
XjETA/sHEeQT9huHzdi/lebNBj0L8nBGfLN1nSRP1GtvagBvkR4RZ6DTQyl0UzOJ
RA3ywWlrL9IV9mrpb1Fmn60l2jTMMCc7J6LacmPK906N+FcN/Docj1M4s/4CNanQ
NhzcFhAFtQL56SNyLTCk1XzhssGZ/jwGnNbU/aaj4wOj0Uef5LRGQ2VudE9TLTUg
S2V5IChDZW50T1MgNSBPZmZpY2lhbCBTaWduaW5nIEtleSkgPGNlbnRvcy01LWtl
eUBjZW50b3Mub3JnPohkBBMRAgAkBQJFnwekAhsDBQkSzAMABgsJCAcDAgMVAgMD
FgIBAh4BAheAAAoJEKikR9zoViiXKlEAmwSoZDvZo+WChcg3s/SpNoWCKhMAAJwI
E2aXpZVrpsQnInUQWwkdrTiL5YhMBBMRAgAMBQJFnwiSBYMSzAIRAAoJEDjCFhY5
bKCk0hAAn134bIx3wSbq58E6P6U5RT7Z2Zx4AJ9VxnVkoGHkVIgSdsxHUgRjo27N
F7kBDQRFnwezEAQA/HnJ5yiozwgtf6jt+kii8iua+WnjqBKomPHOQ8moxbWdv5Ks
4e1DPhzRqxhshjmub4SuJ93sgMSAF2ayC9t51mSJV33KfzPF2gIahcMqfABe/2hJ
aMzcQZHrGJCEX6ek8l8SFKou7vICzyajRSIK8gxWKBuQknP/9LKsoczV+xsAAwUD
/idXPkk4vRRHsCwc6I23fdI0ur52bzEqHiAIswNfO521YgLk2W1xyCLc2aYjc8Ni
nrMX1tCnEx0/gK7ICyJoWH1Vc7//79sWFtX2EaTO+Q07xjFX4E66WxJlCo9lOjos
Vk5qc7R+xzLDoLGFtbzaTRQFzf6yr7QTu+BebWLoPwNTiE8EGBECAA8FAkWfB7MC
GwwFCRLMAwAACgkQqKRH3OhWKJfvvACfbsF1WK193zM7vSc4uq51XsceLwgAoI0/
9GxdNhGQEAweSlQfhPa3yYXH
=o/Mx
-----END PGP PUBLIC KEY BLOCK-----
You can view above PGP public key block directly by:
vi /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
Depending on distribution, change file path.
Friday, June 24, 2011
Shell Script to Find Prime Factors of a Number
In number theory, the prime factors of a positive integer are the prime numbers
that divide that integer exactly, without leaving a remainder. The process of
finding these numbers is called integer factorization, or prime factorization.
The fundamental theorem of arithmetic says that every positive integer has a
unique prime factorization.
Example:
The prime factors of 330 are 2, 3, 5 and 11:
330 = 2 × 3 × 5 × 11
There is no other possible set of prime numbers that can be multiplied to
make 330.
You can find prime factors of a number using UNIX/Linux utility factor. This is
a very convenient and cool UNIX command. Here is its syntax:
$ factor 330
330: 2 3 5 11
$ factor 2121977
2121977: 11 11 13 19 71
Many Algorithms have been devised for determining the Prime factors of a given
number. They vary quite a bit in sophistication and complexity. It is very diff-
icult to build a general-purpose algorithm for this computationally "hard" prob-
lem, so any additional information which is known about the number in question
or its factors can often be used to save a large amount of time.
Here I am providing a shell script to find prime factors of a positive integer.
This script does most factorizations within a second. In the worst case scenario
(for some large semi-primes with more than 6-digit factors) factorization will
take a couple of minutes to hours.
#!/bin/bash
# SCRIPT: primefactors.sh
# USAGE : primefactors.sh <Positive Integer>
# PURPOSE: Produces prime factors of a given number.
#
###############################################################################
# Arguments Checking #
###############################################################################
if [ $# -ne 1 ]
then
echo "Usage: scriptname <Positive Integer>"
exit 1
fi
expr $1 + 1 &>/dev/null
if [ $? -ne 0 ]
then
echo "Sorry, You supplied non numerical value"
exit 1
fi
[ $1 -lt 2 ] && echo "Values < 2 are not prime numbers" && exit 1
num=$1
###############################################################################
# Functions #
###############################################################################
# To know how to find prime number check bellow link:
# Shell script to find prime number
# Bellow function finds supplied argument is a prime or not.
primenumber()
{
primenum=$1
for ((counter2=2;$((counter2*counter2))<=$primenum;counter2++))
do
if [ $((primenum%counter2)) -eq 0 ]
then
return 1
fi
done
return 0
}
primefind()
{
# It's good to check that the number it self is a prime or not before going to
# find prime factors of a number. Comment out bellow line and supply a prime
# number or semi-prime, you will find the difference.
# Ex: primefactors.sh 2121979
primenumber $1 && echo "$1" && exit 0
for ((counter1=$2;counter1<=$1;counter1++))
do
primenumber $counter1 && factorcheck $1 $counter1 && break
done
}
factorcheck()
{
prime=$2
newnum=$1
remainder=$((newnum%prime))
if [ $remainder -eq 0 ]
then
printf "%dx" $prime
newnum=$((newnum/prime))
primefind $newnum 2
return
else
let prime++
primefind $newnum $prime
fi
}
###############################################################################
# Main #
###############################################################################
echo -n "Prime Factors of $1: "
primefind $num 2
printf "\b \n" # \b is used for removing last x.
OUTPUT:
[root@venu ]# sh primefactors.sh
Usage: scriptname
[root@venu ]# sh primefactors.sh 21219797
Prime Factors of 21219797: 101x210097
[root@venu ]# sh primefactors.sh 212197
Prime Factors of 212197: 443x479
Running time of script doesn't depend on number,it depends on number of factors,
more factors less time and less factors more time.
[root@venu ]# time sh primefactors.sh 9999999999999999
Prime Factors of 9999999999999999: 3x3x11x17x73x101x137x5882353
real 0m1.345s
user 0m1.225s
sys 0m0.091s
[root@venu ]# time sh primefactors.sh 999999999995
Prime Factors of 999999999995: 5x251x1831x435179
real 1m32.105s
user 1m28.866s
sys 0m3.192s
[root@venu ]# time sh primefactors.sh 99999999000000000
Prime Factors of 99999999000000000: 2x2x2x2x2x2x2x2x2x3x3x5x5x5x5x5x5x5x5x5x11x7
3x101x137
real 0m0.543s
user 0m0.508s
sys 0m0.035s
Subscribe to:
Posts (Atom)