June 28

Linux: lsof command

To identify and monitor who is running what applications, which the application is listening to what port, established what connections, we can use lsof. lsof (List Open Files) are use for listing all current opened files. Besides the file name, it includes info such as who open it, what command use to open it, what type of file is it etc.

To list all Internet or network related opened files.

lsof -i

output:

COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
ktorrent 5220 mysurface 14u IPv4 13129 TCP *:6881 (LISTEN)
ktorrent 5220 mysurface 15u IPv6 13152 UDP *:6881
ktorrent 5220 mysurface 16u IPv4 41606 TCP 192.168.1.101:43234->122.49.147.27:32006 (SYN_SENT)
ktorrent 5220 mysurface 17u IPv4 41597 TCP 192.168.1.101:59485->host-63-239-252-1.tetonwireless.com:6881 (SYN_SENT)
ktorrent 5220 mysurface 18u IPv4 36757 TCP 192.168.1.101:42724->203-36-217-77.dsl.dodo.com.au:25423 (ESTABLISHED)

You might facing some latency while listing them, the reason is, lsof cleverly replace your IP with DNS, so it need times to resolve DNS. To ask it stop to act smart and display with only numerical IP addresses, specified -n.

lsof -i -n

You may want to list open files with root permission, which returns you more entries.

sudo lsof -i -n

lsof list both IPv6 and IPv4 related files by default. You specified -i4 if you want entries with IPv4 only, same thing to IPv6, specified -i6.

sudo lsof -i4 -n

Okay, If you just want to list files which triggers by a specific user only, you can do this:

sudo lsof -i -n -a -u toydi

By: mysurface

Category: Linux | Comments Off on Linux: lsof command
June 20

Linux: Find Video Card Memory RAM Size Using The Command Line

1. lspci command – It is a utility for displaying information about all PCI buses in the system and all devices connected to them.
2. /var/log/Xorg.0.log – Xorg log file
___________________________________________________________
lspci Command Example To Find Out Graphics Card Memory Size

Open a terminal and type the lspci command
$ lspci
Output:

00:00.0 Host bridge: Intel Corporation 82845G/GL[Brookdale-G]/GE/PE DRAM Controller/Host-Hub Interface (rev 03)
00:02.0 VGA compatible controller: Intel Corporation 82845G/GL[Brookdale-G]/GE Chipset Integrated Graphics Device (rev 03)
00:1d.0 USB Controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #1 (rev 02)
00:1d.1 USB Controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #2 (rev 02)
00:1d.2 USB Controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) USB UHCI Controller #3 (rev 02)
00:1d.7 USB Controller: Intel Corporation 82801DB/DBM (ICH4/ICH4-M) USB2 EHCI Controller (rev 02)
00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev 82)
00:1f.0 ISA bridge: Intel Corporation 82801DB/DBL (ICH4/ICH4-L) LPC Interface Bridge (rev 02)
00:1f.1 IDE interface: Intel Corporation 82801DB (ICH4) IDE Controller (rev 02)
00:1f.3 SMBus: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) SMBus Controller (rev 02)
00:1f.5 Multimedia audio controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M) AC’97 Audio Controller (rev 02)
01:00.0 Network controller: RaLink RT2561/RT61 rev B 802.11g
01:02.0 Multimedia video controller: Brooktree Corporation Bt878 Video Capture (rev 11)
01:02.1 Multimedia controller: Brooktree Corporation Bt878 Audio Capture (rev 11)
01:05.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+ (rev 10)

You need to use string called devices in the specified domain – 00:02.0 VGA (highlighted in red color to display memory info):
$ lspci -v -s 00:02.0
Sample outputs:

00:02.0 VGA compatible controller: Intel Corporation 82845G/GL[Brookdale-G]/GE Chipset Integrated Graphics Device (rev 03) (prog-if 00 [VGA])
Subsystem: Giga-byte Technology Unknown device 2562
Flags: bus master, fast devsel, latency 0, IRQ 177
Memory at d0000000 (32-bit, prefetchable) [size=128M]
Memory at d8200000 (32-bit, non-prefetchable) [size=512K]
Capabilities:

This listing shows an Intel video card with 128 MB of video RAM. If you cannot find devices in the specified domain use following command and look for your display card name and memory field:
$ lspci -v | less
Xorg Log File

Open a command-line terminal (select Applications > Accessories > Terminal), and then type:
$ grep -i –color memory /var/log/Xorg.0.log
Sample outputs:

(–) Aug 02 12:16:20 NVIDIA(0): Memory: 1048576 kBytes
(II) Aug 02 12:16:21 NVIDIA: Using 768.00 MB of virtual memory for indirect memory access.
(==) NVIDIA(0): Disabling shared memory pixmaps

The above output indicate that my Nvidia card has 1024MB ram. The following is taken from my Macbook, which has Intel GM965 Video card. It shares memory from main RAM:
$ grep -i memory /var/log/Xorg.0.log
Sample outputs:

[2318869.434] (II) intel(0): detected 15868 kB stolen memory.
[2318869.472] (II) intel(0): I830CheckAvailableMemory: 1966080 kB available
[2318869.486] (II) intel(0): Attempting memory allocation with tiled buffers.
[2318870.197] (II) intel(0): Fixed memory allocation layout:
[2318870.197] (II) intel(0): 0x00f7f000: end of stolen memory
[2318870.197] (II) intel(0): 0x00f7f000-0x0fffffff: DRI memory manager (246276 kB)
[2318870.197] (II) intel(0): BO memory allocation layout:
[2318870.197] (II) intel(0): 0x00f7f000: start of memory manager
[2318870.197] (II) intel(0): 0x10000000: end of memory

By: V. Gite
Additional Options:

lspci -v | grep VGA -A12

Category: Linux | Comments Off on Linux: Find Video Card Memory RAM Size Using The Command Line
June 14

Linux: Securely Erase A Hard Drive

shred -vfz -n 100 /dev/hda

Here’s what this does. “shred” is the command that tells the operating system that you want to not only erase all the files on the drive but also to over-write those files with random bits. The “f” in the “-vfz” command forces the write by changing the permissions on the drive wherever necessary. The “z” in the “-vfz” command writes zeros to the drive. The “-n” command is followed by a number (“100″ in the example above). This tells the operating system how many times to write random data to the drive. The more times you write over the data you had on the drive, the lower the odds that someone will be able to recover your files. Writing over your data 100 times is likely overkill; 3 to 5 times is how many times I typically wrote over data using Eraser (which does the same thing). Finally, the “/dev/hda” portion of the command tells the computer which drive to wipe.

By: Ryan

Category: Linux | Comments Off on Linux: Securely Erase A Hard Drive
June 13

Linux: History Command Examples

When you are using Linux command line frequently, using the history effectively can be a major productivity boost. In fact, once you have mastered the 15 examples that I’ve provided here, you’ll find using command line more enjoyable and fun.
1. Display timestamp using HISTTIMEFORMAT

Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.

# export HISTTIMEFORMAT=’%F %T ‘
# history | more
1 2008-08-05 19:02:39 service network restart
2 2008-08-05 19:02:39 exit
3 2008-08-05 19:02:39 id
4 2008-08-05 19:02:39 cat /etc/redhat-release

2. Search the history using Control+R

I strongly believe, this may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully. Press Control+R and type the keyword. In the following example, I searched for red, which displayed the previous command “cat /etc/redhat-release” in the history that contained the word red.

# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`red’: cat /etc/redhat-release
[Note: Press enter when you see your command,
which will execute the command from the history]
# cat /etc/redhat-release
Fedora release 9 (Sulphur)

Sometimes you want to edit a command from history before executing it. For e.g. you can search for httpd, which will display service httpd stop from the command history, select this command and change the stop to start and re-execute it again as shown below.

# [Press Ctrl+R from the command prompt,
which will display the reverse-i-search prompt]
(reverse-i-search)`httpd’: service httpd stop
[Note: Press either left arrow or right arrow key when you see your
command, which will display the command for you to edit, before executing it]
# service httpd start

3. Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.

Use the up arrow to view the previous command and press enter to execute it.
Type !! and press enter from the command line
Type !-1 and press enter from the command line.
Press Control+P will display the previous command, press enter to execute it

4. Execute a specific command from history

In the following example, If you want to repeat the command #4, you can do !4 as shown below.

# history | more
1 service network restart
2 exit
3 id
4 cat /etc/redhat-release

# !4
cat /etc/redhat-release
Fedora release 9 (Sulphur)

5. Execute previous command that starts with a specific word

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ‘ps aux | grep yp’.

# !ps
ps aux | grep yp
root 16947 0.0 0.1 36516 1264 ? Sl 13:10 0:00 ypbind
root 17503 0.0 0.0 4124 740 pts/0 S+ 19:19 0:00 grep yp

6. Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, only 450 command will be stored in the bash history.

# vi ~/.bash_profile
HISTSIZE=450
HISTFILESIZE=450

7. Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .commandline_warrior file instead of .bash_history file. I’m yet to figure out a practical use for this. I can see this getting used when you want to track commands executed from different terminals using different history file name.

# vi ~/.bash_profile
HISTFILE=/root/.commandline_warrior

If you have a good reason to change the name of the history file, please share it with me, as I’m interested in finding out how you are using this feature.
8. Eliminate the continuous repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.

# pwd
# pwd
# pwd
# history | tail -4
44 pwd
45 pwd
46 pwd [Note that there are three pwd commands in history, after
executing pwd 3 times as shown above]
47 history | tail -4

# export HISTCONTROL=ignoredups
# pwd
# pwd
# pwd
# history | tail -3
56 export HISTCONTROL=ignoredups
57 pwd [Note that there is only one pwd command in the history, even after
executing pwd 3 times as shown above]
58 history | tail -4

9. Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.

# export HISTCONTROL=erasedups
# pwd
# service httpd stop
# history | tail -3
38 pwd
39 service httpd stop
40 history | tail -3

# ls -ltr
# service httpd stop
# history | tail -6
35 export HISTCONTROL=erasedups
36 pwd
37 history | tail -3
38 ls -ltr
39 service httpd stop
[Note that the previous service httpd stop after pwd got erased]
40 history | tail -6

10. Force history not to remember a particular command using HISTCONTROL

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.

# export HISTCONTROL=ignorespace
# ls -ltr
# pwd
# service httpd stop [Note that there is a space at the beginning of service,
to ignore this command from history]
# history | tail -3
67 ls -ltr
68 pwd
69 history | tail -3

11. Clear all the previous history using option -c

Sometime you may want to clear all the previous history, but want to keep the history moving forward.

# history -c

12. Subtitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.

In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.

# ls anaconda-ks.cfg
anaconda-ks.cfg
# vi !!:$
vi anaconda-ks.cfg

In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).

# cp anaconda-ks.cfg anaconda-ks.cfg.bak
anaconda-ks.cfg
# vi !^
vi anaconda-ks.cfg

13. Substitute a specific argument for a specific command.

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.

# cp ~/longname.txt /really/a/very/long/path/long-filename.txt
# ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt

In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.

# ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt

14. Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.

# export HISTSIZE=0
# history
# [Note that history did not display anything]

15. Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

# export HISTIGNORE=”pwd:ls:ls -ltr:”
# pwd
# ls
# ls -ltr
# service httpd stop

# history | tail -3
79 export HISTIGNORE=”pwd:ls:ls -ltr:”
80 service httpd stop
81 history
[Note that history did not record pwd, ls and ls -ltr]

By: R. Natarajan

Category: Linux | Comments Off on Linux: History Command Examples
June 13

Linux: Moving Ubuntu To Another Hard Drive(Disk)

Moving to a Bigger Disk

As time moves on and storage becomes bigger and cheaper it’s quite common to want to replace the disks in your MythTV box. There are a few options. You can add more disks, but this is restricted by the number of SATA connectors on your motherboard. You can replace your disk and reinstall, but if you have a working installation, why go to the trouble? The option I chose recently was to take a system with one 80GB disk (OS) and one 1.5TB disk (recordings) and replace the 80GB disk with a new 2TB one. That involved copying the smaller disk onto the new one and creating a new partition on the remaining space.

The procedure detailed here will obviously work on any Linux box. It has nothing specifically to do with MythTV.
Procedure Overview

The basic procedure involves installing the old and new disks into the chassis, booting from an Ubuntu Live CD and copying the raw data across. We will use the ddrescue utility to do this. This will copy the entire disk, regardless of how much of it is used. Once this is done, the new disk should boot up as an exact replacement of the old one. We will then create a new partition on the remaining free space. I ended up with a new OS disk with a 72MB partition and a 1.8TB partition, plus the old 1.5TB storage disk.
Procedure – Cloning an Ubuntu Install Onto a Bigger Disk
Boot From a Live CD

It’s not a good idea to clone a partition which is in use, so we will boot from a Live CD. This will give us a fully working Ubuntu system with access to all disks in the PC.

Download an Ubuntu Live CD image and burn it. You can also use a USB image if you don’t have have a CD drive.

Power down your PC and remove all drives apart from the old OS disk (what we will call the source disk). Install the new empty disk (which we will call the target), then boot from the CD.
Identity Which Disks are Present

This is VERY IMPORTANT! We must identify the device numbers of the source and targets disks. Running the following command will give us this information:

sudo fdisk -l | grep Disk
Disk /dev/sda: 80.4 GB
Disk /dev/sdb: 2000.4 GB

As you can see above, the small 80GB source drive in my system is /dev/sda and the target 2TB drive is /dev/sdb. The rest of the commands in these instructions will use these device names. Make sure you change these if your system is different!

If you are still unsure, the following command will give you some more information on the drives present:

sudo lshw -C disk

Write Empty Partition Table onto the New Disk

Now we will create an empty partition table on the new disk:

sudo cfdisk -z /dev/sdb
W (write empty partition table)

Install ddrescue

It is possible to do the disk copy using the simple dd utility. However, it does no error checking. The ddrescue utility is much better for this activity as it will not copy any bad sectors to the new disk. It also gives a better indication of what’s going on. We will need to install ddrescue on the temporary Live CD version of Ubuntu we are running. Ubuntu makes this very easy.

First, enable the universe repositiory by uncommenting the entries in /etc/apt/sources.list

sudo nano /etc/apt/sources.lst

Remove the hash from the start of each line containing the word universe, eg:

deb http://archive.ubuntu.com/ubuntu maverick universe
deb-src http://archive.ubuntu.com/ubuntu maverick universe
deb http://archive.ubuntu.com/ubuntu maverick-updates universe
deb-src http://archive.ubuntu.com/ubuntu maverick-updates universe
deb http://security.ubuntu.com/ubuntu maverick-security universe
deb-src http://security.ubuntu.com/ubuntu maverick-security universe

Update the repository index and install the package:

sudo apt-get update
sudo apt-get install ddrescue

Clone the Disk

Here we will use ddrescue to make an exact copy of the first disk on the second. Again, make sure your device names are correct (see above). An 80GB disk takes about an hour to copy.

sudo ddrescue -v /dev/sda /dev/sdb

Test the New Disk

Once ddrescue has done its stuff, the new disk should work exactly like the old one. Shut down the system and remove the boot CD. I recommend taking the original drive out and trying to boot from the new one. Hopefully, your Linux install should function exactly as it did before, except you now have plenty unused space ready for a new partition and filesystem.
Create Additional Partition

Now we will use the cfdisk utility to create an ext4 partition on the free space of the new drive (which is now /dev/sda):

sudo cfdisk /dev/sda

Once that is done, you will need to create a filesystem on the new partition and mount it before it can be used. We first use fdisk again to get the partition number.

sudo fdisk -l

Device Boot Start End Blocks Id System
/dev/sda1 * 1 9541 76638051 83 Linux
/dev/sda2 9542 9729 1510110 5 Extended
/dev/sda3 9730 243201 1875363840 83 Linux
/dev/sda5 9542 9729 1510078+ 82 Linux swap / Solaris

On my system, it’s the big /deb/sda3 partition.

You may now create a Linux ext4 filesystem (again, make sure you change the device name to match your system!):

sudo /sbin/mke2fs -t ext4 /dev/sda3

Get UUID of new partition

OK, we’re nearly there. The last step is to set the system up to mount the disk every time it boots. Ubuntu no longer uses device names for this, is uses the unique ID of the partition. This is so that the system still works as intended if the device names change for any reason. Run the following to get the UUID of the disks on your system:

ls /dev/disk/by-uuid -alh
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 3317c035-c5f9-4f92-95ef-6d0afe8cb59f -> ../../sdb2
lrwxrwxrwx 1 root root 10 2011-05-09 07:15 73b51bb7-c8c3-438f-99f2-05c7ff3471b0 -> ../../sda3
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 ad739750-08bc-46f4-8cdf-12d7bbb0798d -> ../../sda1
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 b6dae2c7-49f3-441f-8123-64a4603ae379 -> ../../sda5
lrwxrwxrwx 1 root root 10 2011-05-05 22:28 fff90199-0b52-47d2-97c8-0bf85577c3cc -> ../../sdb1

/dev/sda3 on my system has UUID 73b51bb7-c8c3-438f-99f2-05c7ff3471b0

Create a directory as the mount point for the partiton. I will be using /mnt/store2

sudo mkdir /mnt/store2

Edit the /etc/fstab file:

sudo nano /etc/fstab

Add the following line (replace the UUID of the partition and the mount directory with the ones for your system, see above):

UUID=73b51bb7-c8c3-438f-99f2-05c7ff3471b0 /mnt/store2 ext4 defaults,errors=remount-ro,relatime 0 1

To test it, either reboot or run the command:

sudo mount /mnt/store2

By: G. Parker

Category: Linux | Comments Off on Linux: Moving Ubuntu To Another Hard Drive(Disk)