How to manage LVM in Linux

How to manage LVM in Linux

Creating Dynamic Disc

We are going to create three discs on a USB drive, each disc will have 512 MB. First, verify if we have connected the USB drive as /dev/sdc/:

# fdisk -l

If we have a USB drive connected as /dev/sdc:

# fdisk /dev/sdc

Command d can remove existing logical volumes on the USB drive.

Enter commands n, p, 1-3, +512M to create 3 logical volumes. For example, we create the first volume /dev/sdc1:

Command (m for help): p
Command (m for help): n
Partition number (1-3): 1
First sector (34-41943006, default 41940992): 34
Last sector, +sectors or +size{K,M,G,T,P}: +512M
Command (m for help): p

Command w saves new partition table and changes can not be undone.

Reload partition table:

# partprobe

Format created discs:

# pvcreate /dev/sdc1
# pvcreate /dev/sdc2
# pvcreate /dev/sdc3

Verify:

# pvdisplay

Create volume group DATA:

# vgcreate DATA /dev/sdc1 /dev/sdc2 /dev/sdc3

To be sure verify group capacity:

# vgdisplay

Create physical volume from 100% of physical volume capacity.

# lvcreate -l +100%FREE -n new_volume DATA

Format the new volume:

# mkfs -t ext3 /dev/DATA/new_volume

Create a directory for mount:

# mkdir /mnt/dynamic

Mount the new volume:

# mount /dev/DATA/new_volume /mnt/dynamic

Mount verification:

# df -hT

Mount verification using a graphical interface:

# system-config-lvm

Dynamic Resize Dynamic Disk

Create primary logical partition /dev/sdc4, that use the rest of the USB drive:

# fdisk /dev/sdc
Command (m for help):n
Command (m for help):4
Command (m for help):p
<enter>
<enter>

Command w saves a new partition table and it can not be undone.

Verify partition table:

# partprobe

Add new disk to physical LVL partition:

# pvcreate /dev/sdc4

Verify:

# pvdisplay

Extend volume group:

# vgextend DATA /dev/sdc4

Verify extended volume group:

# vgdisplay

Extend logical volume with 1 GB:

# lvextend -L+1G /dev/DATA/new_volume

Resize size of disk:

# resize2fs /dev/DATA/new_volume

Remount:

# mount -o remount /dev/DATA/new_volume

Verify:

# df -hT

Removing Dynamic Disc

Unmount logical volume:

# umount /dev/DATA/new_volume

Remove logical volume:

# lvremove /dev/DATA/new_volume

Remove volume group:

# vgremove /dev/DATA

Remove physical volumes:

# pvremove /dev/sdc1
# pvremove /dev/sdc2
# pvremove /dev/sdc3
# pvremove /dev/sdc4

Verification using a graphical interface:

# system-config-lvm

Linux How to Zip

Linux How to Zip

To create a compressed file and save disk space use following the following syntax:

zip options name.zip file1 file2 file3 folder

The zip program compresses one or more files into the name.zip archive. Zip has options from “1” to “9”. Option -1 use for fast compression and -9 for better compress ratios.

zip -9 name.zip file1 file2

If you want to insert a new file to the existing zip archive use options “u” which means update.

zip -u name.zip newFile

To decompress the zip archive use command:

unzip name.zip

To decompress to a specific directory use the “d” option:

unzip name.zip -d /directory

Command tar is a great tool too. You can compress files with tar:

tar -zcvf name.tgz file1 file2 file3 folder

To unzip compressed file type:

tar -zxvf name.tgz

NANO Tutorial for Linux

NANO Tutorial for Linux

Start nano:

nano textFile.txt

Show help:

Ctrl + G

Hide help:

Ctrl + X

Save file:

Ctrl + O

Save and exit:

Ctrl + X then answer Y

Exit without saving:

Ctrl + X then answer N

Go to previous/next line:

Ctrl + P/Ctrl + N

Go to previous/next screen:

Ctrl + Y/Ctrl + V

Go to the line and column number:

Ctrl + _

Insert another file into the current file:

Ctrl + R, enter path to the file

Search for string or regular expression:

Ctrl + W

Cut the current line and store it in the cutbuffer:

Ctrl + K

Uncut from the cutbuffer to the current line:

Ctrl + U

Copy the current line to cut buffer:

Alt + 6 or Alt + ^

How to Install Software in Linux

How to Install Software in Linux

Shared libraries

Print command dependencies of shared libraries

# ldd /bin/ls

Add new shared library to program icq

# ldconfig -n /opt/icq/lib

Actual shared library in cache (/etc/ld.so.cache)

# ldconfig -p

Get settings of shared libraries

# cat /etc/ld.so.conf.d /etc/ld.so.conf.d/*

RPM Package Manager (RPM)

Install

# rpm -i package.rpm

Uninstall (erase)

# rpm -e package.rpm

Upgrade

# rpm -U package.rpm

Upgrade only if an older version is installed

# rpm -F package.rpm

Print list of all installed packages

# rpm -qa

Find verbose information about package

# rpm -qv package

Print information about a package

# rpm -ql package

Display only the config files in package

# rpm -qc package

Display install destination folder before installation

# rpm -qpl package.rpm

Print changelog before installation

# rpm -qp --changelog package.rpm

Print name of package which binds to file

# rpm -qf /etc/exports

Integrity verification of all packages

# rpm -Va

Debian Package (dpkg)

Install

# dpkg -i package.deb

Uninstall (remove)

# dpkg -r package

Uninstall (include config files)

# dpkg -r --purge package

Display package’s files content

# dpkg -c package.deb

Display status of specified package

# dpkg -s package.deb

List files installed to your system from the package

# dpkg -L package

Print partially installed packages and give suggestions on how to fix them

# dpkg -C package

List of all installed packages

# dpkg -l

Source code

Extraction

# tar –xzf /path/name/package.tar.gz

# tar –xjf /path/name/package.tar.bz2

Prepare for compile

# ./configure

Start compile

# make

Install

# make install

Linux: How to Connect External Hard Drive

Linux: How to Connect External Hard Drive

Diagnostic Commands

Display from log directory what was recently connected

# tail -20 /var/log/messages

Display list of the partition tables

# fdisk -l

Display device parameters

# hdparm /dev/sda

More detailed information

# hwbrowser

# more /etc/sysconfig/hwconf

Connection Process

Run fdisk

# fdisk /dev/sda

Get list of existing partitions

Command (m for help): p

Delete partition which exist on the drive

Command (m for help): d

Create new partition

Command (m for help): n

Verification before save

Command (m for help): p

Save new table to drive

Command (m for help): w

Update partition table in system

# partprobe

Alternative to partprobe is hdparm -z

# hdparm -z /dev/sda

Format created partition to ext3 file system

# mkfs.ext3 /dev/sda1

Verification if the partition is not mounted

# df -hT

Mount new-created partition to directory /mnt/usb. If a directory does not exist, first created it.

# mkdir /mnt/usb

# mount /dev/sda1 /mnt/usb

# df -hT

Display disk partitions

# parted /dev/sda unit GB print free

Linux How to Generate Random Password

Linux How to Generate Random Password

Generating password is relatively simple and can be done in different ways. You can install tools to generate passwords, a few examples:

  • mkpasswd (Debian/Ubuntu)
  • makepasswd (Debian/Ubuntu)
  • pwgen (CentOS/Fedora/RHEL)

When you don’t have installed these tools, here are useful commands:

date +%s | sha256sum | base64 | head -c 32 ; echo

We used time in seconds as a input to hash function sha-256 and print first 32 chars. You can replate sha256sum with other hash function (md5sum).

strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 32 | tr -d '\n'; echo

/dev/urandom is the built-in feature which generate random chars.

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32; echo

Another way using /dev/urandom, even simpler.

You can also create the script:

generatePasswd () {
local l=$1
[ "$l" == "" ] && l=16
tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}

generatePasswd "$1"

Linux How to Change Hostname

Linux How to Change Hostname

Hostname identifies a computer on the network and it is visible in the terminal. We set up hostname during the installation operating system, but sometimes we want to change it.

Use command “hostname” to change hostname:

hostname new_hostname

Will set the hostname of the system to new_hostname. Open a new terminal to verify change hostname.

If you want to change the hostname permanent use the command “sysctl kernel.hostname” to change it:

sysctl kernel.hostname=NewHostname

Output: kernel.hostname = Newhostname

Command hostname without any parameter prints the current hostname of the system.

Linux How to Extend Swap Space

Linux How to Extend Swap Space

Display swap usage summary

# swapon -s

Show how many space swap needs in megabytes

# free -m

Create new swap disk.

# fdisk /dev/sdb

Format new disk to swap format

# mkswap /dev/sdb1

Add new part of swap

# swapon /dev/sdb1

Verify if there is more space in swap space

# free -m

Append next line to file /etc/fstab.

/dev/sdb2 swap swap defaults 0 0

If you need old swap use

# swapoff /dev/sda6

Linux How to Execute Script

Linux How to Execute Script

There are many ways how to execute a script, the first one is to execute the script by specifying the interpreter:

bash script.sh
sh script.sh

For debugging use option “-x” to see what is being done.

bash -x script.sh

As an interpreter, you can use sh, ksh, csh, bash etc.

If you want to execute the script without specifying an interpreter, you need to set execute (+x) permission:

chmod +x script.sh

Then you can execute the script following way:

./script.sh

Another way how to run the script:

source script.sh

The script doesn’t need to execute permission in this example. If the script makes any changes to the environment, it will be visible after running the script, because commands in the script are executed in the current shell.

Best Linux Text Editors

Best Linux Text Editors

You can choose between several text editors in Linux. Each editor has advantages and advantages.

1. Vi/Vim
Vi is a powerful and the most popular command-line-based editor. Commonly used for writing code and editing configuration files. First of all, the advantage is availability. Vi is always installed on any distribution. The second advantage is the consumption of system resources. One of the cons is non-intuitive, but short commands.

Vi has 3 modes: command, input, and last line mode. Command mode is the default.

2. Nano
Nano is WYSIWYG (what you see is what you get) editor and is installed by default in Ubuntu and many other Linux distributions. Action/commands are done in a CTRL and Key manner, for example, CTRL + X save a file. Features: Autoconf support, case-sensitive search function, auto-indent ability, regular expression search and replace.

3. Gedit
Gedit is the default text editor for the GNOME desktop environment. Gedit’s aim is simple and easy to use for beginner Linux users. Useful features are syntax highlighting, clipboard support, brackets matching, search and replace with support of regular expressions

4. GNU Emacs
Emacs is the extensible self-documenting editor. It provides an interpreter for Emacs Lisp. Main function: text editing including a project planner, mail and newsreader, debugger interface, calendar.

5. Leaf Pad
GTK+ based editor is popular among new Linux users because it is easy to use. It supports the codeset option, auto codeset detection, and Drag & Drop function. It does not provide syntax coloring.

How to Make Software RAID in Linux

How to Make Software RAID in Linux

Add a new partition on the first physical disc:

# fdisk /dev/sda

Using the same option add a new partition on the second physical disc:

# fdisk /dev/sdb

Create a new RAID volume md0 type of RAID1 (raid level1):

# mdadm --create /dev/md0 --level=1 --raiddevices=2 /dev/sda7 /dev/sdb2

Check and verify addition:

# watch cat /proc/mdstat

Format the new RAID volume to ext3 filesystem:

# mkfs -t ext3 /dev/md0

Verify created RAID:

# mdadm --query /dev/md0

For more details enter:

# mdadm --query --detail /dev/md0

Create a directory for mounting RAID volume:

# mkdir /mirror

Mount RAID volume md0:

# mount /dev/md0 /mirror

Verify:

# df -hT

Let’s try to create directory /etc and file numbers.txt:

# cp –a /etc /mirror
# seq 10000000 > /mirror/cisla.txt

Verify:

# ls –ltr /mirror
# tail /mirror/subor.txt

You can anytime monitor volume [UU]:

# cat /proc/mdstat

We need to configure automatic mounting volume md0 after the system starts:

# cat /etc/mtab | grep md0
# joe /etc/fstab

Append next lines at the end of file /etc/fstab:

/dev/md0 /mirror ext3 defaults 0 0

Append next lines at the end of file /etc/mdadm.conf. If any error occured, the system will send an email about it.

DEVICE /dev/sd[a-z]*
ARRAY /dev/md0 level=raid1 devices=/dev/sda7,/dev/sdb2
MAILADDR root@localhost

Turn the monitoring service on:

# service mdmonitor start

Try to make disc down:

# yes “We love linux.“ > /mirror/linux.txt

If you pull out the disc during the yes command you should get an email with the error message:

# mutt

We can see one disc is not available [_U]:

# cat /proc/mdstat

Removing partitions that physically don’t exist:

# mdadm /dev/md0 –r detached

Removing partitions that physically exist:

# mdadm --manage /dev/md0 --remove /dev/sdb2

Connect physical disc and monitor how it is being synchronized:

# mdadm --manage /dev/md0 --add /dev/sdb2
# watch cat /proc/mdstat

Test RAID functionality. We should see “active raid1”:

# cat /proc/mdstat

verify RAID:

# mdadm --detail /dev/md0

Disable RAID:

# umount /dev/md0
# mdadm --manage --stop /dev/md0

Test RAID functionality again, we should not see “active raid1”:

# cat /proc/mdstat

RAID: removing

Unmount mounted partition:

# umount /mirror

Stop RAID (to start it again use –assemble –scan):

# mdadm --stop /dev/md0

Remove RAID:

# mdadm --remove /dev/md0

RAID partitions are readable, we can mount it:

# mount /dev/sdb2 /mnt

We should see details about RAID:

# ls –ltrh /mnt

Unmount RAID partition:

# umount /dev/sdb2