Welcome

Welcome to TechPulp.com. Here you can find technical articles, examples, sample code snippets. We lay more emphasis on usage scenarios by giving practical examples. Please submit your comments, suggestions and requests for any new articles.

Did you know? You can also participate and start writing articles and publish here.



How to run multiple commands at once in bash shell

June 24, 2009 By: Liz Category: Bash, Command Line, Unix

All commands can be just appended using a semicolon (;) to make the bash to run all the commands one after the other as shown below.

[liz@techpulp ~]# make config; make; make release

The above command is equivalent to running three commands “make config”, “make” and “make release” one after the other. The only difference is that you don’t need wait for one command to finish to type in the next command.

However if you have a series of dependant commands that require the previous command to succeed to execute the next command, you can change the command as shown below.

[liz@techpulp ~]# make config && make && make release

In the above example, all commands are appended with “&&” operator which makes the bash to execute further commands if any command returns non-zero value. It means that if “make config” command is successful without any errors, then only bash proceeds with executing next command “make”. Otherwise it aborts further execution.

Typing too long commands is boring especially when they have to be repeated many a times. In such cases you can create a small shell script or use an alias to make your life easy.

How to solve ‘Stale NFS file handle’ error while accessing or unmounting a NFS volume

May 14, 2009 By: Neo Category: Linux, Networking

This error is typically seen in a system which has active NFS mount point. The NFS protocol doesn’t define any automatic way of communication for any change of NFS server configuration. For example, if a client system mounts an NFS volume and the Administrator at the NFS server removes the directory from the NFS exported list, The client system will not know the change of configuration at NFS server side. In such cases, the ‘df’ command starts showing this error ‘Stale NFS file handle’.

As your system is NFS client, you can unmount the previously mounted NFS volume using ‘-f‘ option which does forceful unmounting of the volume. Of course, the “-f” is intended for NFS mounted volumes but not for other volumes like partitions of local hard drive.

For example, your NFS mount point is “/nfsdir”, the error you would notice will be as follows:

[root@techpulp /]# ls -l
total 154
drwxr-xr-x   2 root   root    4096 2009-02-10 12:58 bin
drwxr-xr-x   5 root   root    1024 2009-05-08 15:33 boot
drwxr-xr-x  15 root   root    4760 2009-05-13 10:37 dev
drwxr-xr-x 139 root   root   12288 2009-05-13 10:37 etc
drwxr-xr-x   5 root   root    4096 2009-03-06 14:59 home
drwxr-xr-x  17 root   root   12288 2009-03-04 17:36 lib
drwx------   2 root   root   16384 2009-01-06 16:26 lost+found
drwxr-xr-x   2 root   root    4096 2009-05-13 10:09 media
drwxr-xr-x   2 root   root    4096 2008-09-06 15:43 mnt
drwxr-xr-x   5 root   root    4096 2009-05-13 10:09 opt
dr-xr-xr-x 231 root   root       0 2009-05-08 15:38 proc
drwxr-x---  24 root   root    4096 2009-05-13 15:32 root
drwxr-xr-x   2 root   root   12288 2009-02-10 12:58 sbin
drwxr-xr-x   2 root   root    4096 2009-01-06 16:30 selinux
drwxrwxrwt  37 root   root    4096 2009-05-14 09:54 tmp
drwxr-xr-x  13 root   root    4096 2009-01-06 16:35 usr
drwxr-xr-x  24 root   root    4096 2009-01-06 16:51 var
ls: cannot access nfsdir: Stale NFS file handle
[root@techpulp /]#

Now you can just unmount the NFS volume using “-f” command.

[root@techpulp /]# umount -f /nfsdir
umount.nfs: Server failed to unmount '192.168.45.67:/nfsdir'
[root@techpulp /]#

You can attempt to mount the NFS volume again if you are sure that it is mountable:

[root@techpulp /]# mount 192.168.45.67:/nfsdir /nfsdir -t nfs

How to change the order of network card detection in Linux

May 14, 2009 By: Neo Category: Linux

There are multiple issues you would notice when you have more than one Ethernet card (NIC) in your system. The NICs may not get detected in the order you want it to be. The on-board Ethernet port may get detected as “eth2″ while you want it to be detected as “eth0″.

Another problem is when you already have 3 Ethernet cards on your system and removed one them (more specifically the one which used to be detected as “eth1″). In such case, you system shows two Ethernet interfaces “eth0″ and “eth2″ while you might like it to be “eth0″ and “eth1″ as there are only two Ethernet interface available in the system.

Another problem is when you have replaced an old 100Mbps NIC with a new 1Gig NIC. Now the system attempts to apply the configuration (including hardware MAC address) to the new 1Gig NIC. One solution to this problem is to do the card replacement in two steps. First remove the old NIC and boot the system once to remove the old NIC from the hardware list using ‘Network Device Control’ or when kudzu prompts for it. The shut down the system and insert the new 1Gig NIC and boot the system again to add the new hardware.

In all these cases, especially in modern Linux distributions like Fedora, you should find the hardware detection rules defined by “udev”. There is a file “/etc/udev/rules.d/70-persistent-net.rules” which contains the list of interfaces and their names (eth0, eth1 etc). You can edit this file to change the name (eth0, eth1 etc) with which a card should be detected. Look at the following sample file.

[root@techpulp ~]# cat /etc/udev/rules.d/70-persistent-net.rules

# This file was automatically generated by the /lib/udev/write_net_rules
# program run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single line.

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
ATTR{address}=="00:07:e9:23:d7:c3", ATTR{type}=="1", KERNEL=="eth*",
NAME="eth0"

# PCI device 0x8086:0x100e (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
ATTR{address}=="00:07:e9:23:d7:f4", ATTR{type}=="1", KERNEL=="eth*",
NAME="eth1"

# Intel Corporation 82562ET/EZ/GT/GZ - PRO/100 VE (LOM) Ethernet Controller
# (rule written by anaconda)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*",
ATTR{address}=="00:83:36:a5:3d:81", ATTR{type}=="1", KERNEL=="eth*",
NAME="eth2"
[root@techpulp ~]#

In the above example, the system has one on-board 100Mbps NIC and two 1Gig NICs connected using PCI. You can identify them using the hardware address as well. You can edit this file and modify the “NAME” field to change the order.

How to export a NFS volume in Fedora or RedHat Linux

May 09, 2009 By: Neo Category: Fedora, Networking, RedHat

NFS stands for Network File System and is a easy way to share files across Linux systems. However the downside of NFS file sharing is that shared folder can’t be protected with a password. But you can limit access to particular IP addresses.

The Fedora Linux system comes up with NFS utilities by default. Otherwise you can install them using “yum” as shown below:

[root@techpulp ~]# yum install -y nfs-utils rpcbind

You can enable the NFS service to start at boot time using the following commands. The “rpcbind” service also needs to be enabled as NFS internally using RPC service. But if you are trying on RedHat9, you won’t find “rpcbind” service and enabling NFS service is good enough.

[root@techpulp ~]# chkconfig --level=35 rpcbind on
[root@techpulp ~]# chkconfig --level=35 nfs on

To export a directory using NFS, you need to place an entry in “/etc/exports” file. For example, adding the following line in /etc/exports file exports a directory named “/mypub” with read-only mode.

/mypub            *(ro,sync)

The “*” in the above line indicates that anybody can access this directory. You can limit access to a specific client by replacing “*” with IP address of the client system as shown below. The following entry allows access to the client system with IP address 192.168.1.1.

/mypub             192.168.1.1(ro,sync)

You can replace “ro” with “rw” if you want to client to have write access to this directory.

On the client system, you can mount the NFS directory using “mount” command. Assuming that the IP address of NFS server is 192.168.8.8, the following command run in the client system mounts the NFS exported volume in “/myimport” directory.

[root@client ~]# mount 192.168.8.8:/mypub /myimport -t nfs

This example explains basic usage of “/etc/exports” file on NFS server. However you can use “man exports” to find more options that can be used while exporting a NFS volume. Such options include allowing access to single NFS exported volume to multiple clients with different access permissions, allowing access to IP subnets, allowing access based on fully qualified domain names etc.

/               master(rw) trusty(rw,no_root_squash)
/projects       proj*.local.domain(rw)
/usr            *.local.domain(ro) @trusted(rw)
/home/neo       neo.techpulp.com(rw,all_squash,anonuid=500,anongid=500)
/pub            (ro,insecure,all_squash)
/srv/www        -sync,rw server @trusted @external(ro)

Fedora 11 (LEONIDAS) gets ready to hit the road by end of May

May 08, 2009 By: Neo Category: Fedora

The upcoming Fedora 11 release, named Leonidas, is scheduled to be released towards end of May. This release comes up with all latest versions of desktops like KDE, GNOME, XFCE and popular applications like Firefox, OpenOffice.org etc. The following are the notable features in this release.

  • EXT4 as default file system
  • KDE 4.2, GNOME 2.26, Xfce 4.6, OpenOffice.org 3.1.0, Firefox 3.1, Thunderbord 3
  • Improvement in time taken for start up and shut down (20 sec start up)
  • Simplified volume control
  • Native Windows cross compiler

How to get a shell with root access in Ubuntu similar to “su” in RedHat or Fedora Linux

May 08, 2009 By: Neo Category: Ubuntu

In RedHat or Fedora systems, you will separate passwords and separate user accounts for root and non-root user.

But in Ubuntu, the default user already has root privileges but not allowed to run any command that requires root privileges directly. Instead the user needs to use “sudo” command to invoke any command that requires root privileges. However this is little bit inconvenient if one (especially a RedHat/Fedora user who is used to “su” command) is sick of “sudo” command and wants a full shell access with root privileges.

In Ubuntu, you need to provide self password whenever “sudo” command prompts for password.

There are two alternatives to get a full blown shell with root privileges in Ubuntu.

Method1:

neo:/home/neo> sudo bash
Password:
root:/home/neo>

Method 2:

neo:/home/neo> sudo -i
Password:
root:/home/neo>

What is the difference between a macro and an inline function in C

May 08, 2009 By: Neo Category: C/C++

The fundamental difference between a macro and an inline function is that they are handled in different phases of binary creation. A macro is expanded during pre-processing stage by the “cpp” command. The “cpp” command is the C preprocessor. On the other hand, the inline functions are handled by the actual compiler “gcc“.

As the preprocessor is less intelligent, it does blind code replacements wherever it finds usage of a macro. A function described in a macro form doesn’t look nice in terms of code readability. Also arguments taken by a macro do not carry their type and often cause compilation errors that are not so obvious to fix.

Where as an inline function is more powerful than a macro and it is like any other function in the C source code with a keyword “inline” attached to it. Let us look at simple inline function given below.

inline int sum(int a, int b)
{
	return (a+b);
}

All inline functions behave like functions when no optimization flag is set during compilation. That means you will find a symbol with the names of all inline functions in the target binary (ELF) file.

But when optimization flag like “-O2” is set during compilation, the compiler replaces all the occurrences of inline function calls with its code much like a macro expansion.

The use of inline functions avoid the overhead of a function call and still maintains the code readability. An inline function can also be treated as a parameterized macro.

How to merge two files using text mode in Linux

May 08, 2009 By: Nick Category: Linux, Tools, Unix, VI

The Linux provides a command “vimdiff” which can open two files using standard “vim” editor but in two separate logical windows. This is a text-mode based tool. This tool highlights the differences between two files so that the user can inspect the differences and change or move the contents from one window to the other.

First of all, the two file which need merging have to be opened with “vimdiff” as shown below.

vimdiff file1.c file2.c

User can type “Ctrl w w” key combination to switch between windows i.e effectively between two files. You can use standard “vim” commands to copy and paste or delete some text of both files. If user needs to move certain portion of text from one file to other, he/she needs to first copy the text in the source file, switch to the other window using “Ctrl w w” key combination and then use the standard “p” command to paste the contents.

User can modify and save one of or both the files. To save file, user can move to appropriate window and type the standard “:w” command to save it.

Using the “vimdiff” command you can even open and merge up to four files at one.

You can even merge a local and a file residing in an remote host using ssh as shown below.

[nick@techpulp ~]# vimdiff scp://nick@neo.techpulp.com//share/source/bingo.c ./bingo.c

How to enable auto login for SSH

April 23, 2009 By: Neo Category: FreeBSD, Linux, NetBSD, Networking, OpenBSD

This article assumes that the host name of server is “server1.techpulp.com” and that of client machine is “client1.techpulp.com“. You can replace these with your own domain names or IP addresses to suit your needs.

Login to the server system to which you would like password-less login.

[neo@client1 ~]$ ssh neo@server1.techpulp.com
neo@server1.techpulp.com's password:
[neo@server1 ~]$

Generate a RSA key pair in the server as shown below. Just press ENTER key when it prompts for passphrase. The following example may be exactly as shown below and may vary based on the version of ssh-keygen present in your server system. But it prompts you for same input as shown below. Basically it generates two files “id_rsa” and “id_rsa.pub“.

[neo@server1 ~]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/neo/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/neo/.ssh/id_rsa.
Your public key has been saved in /home/neo/.ssh/id_rsa.pub.
The key fingerprint is:
31:07:e7:70:71:ea:41:40:c0:f8:39:52:0a:f8:96:b4 neo@server1.techpulp.com
The key's randomart image is:
+--[ RSA 2048]----+
| .   o.o=.=..    |
|. o . o  B o     |
| o + + .o =      |
|  E o +  = .     |
| .   . .S .      |
|                 |
|                 |
|                 |
|                 |
+-----------------+
[neo@server1 ~]$

Now append id_rsa.pub to your authorized_keys file a shown below. Note that you are still doing this in the server system.

[neo@server1 ~]$ cd ~/.ssh
[neo@server1 ~]$ cat id_rsa.pub >> authorized_keys
[neo@server1 ~]$ chmod 600 authorized_keys

Now logout of the server and come back to the client system. Then copy the “id_rsa” file from the server system to client system in a specific directory as shown below.

[neo@server1 ~]$ exit
[neo@client1 ~]$ mkdir -p ~/.ssh
[neo@client1 ~]$ cd ~/.ssh
[neo@client1 .ssh]$ scp neo@server1.techpulp.com:.ssh/id_rsa ./
neo@server1.techpulp.com's password:
.ssh/id_rsa                                          100% 2587     2.5KB/s   00:00
[neo@client1 .ssh]$ chmod 600 id_rsa

That’s it. Now you are all set to try password-less login to the server system. But remember the file “id_rsa” is a secret file and it is not supposed to be accessible to anyone else. If someone gets your “id_rsa” file, they can also login to the server without password as you do. That is the reason behind changing the access permissions of that file to 600.

Now let us test the password-less login from client1 to server1. The same applies to any file transfer using “scp” or “sftp“.

[neo@client1 ~]$ ssh neo@server1.techpulp.com
[neo@server1 ~]$

How to safely remove a file that contains sensitive data in Linux

April 16, 2009 By: Neo Category: Command Line

Typically a file removal operation, in Linux or any other operating system, doesn’t actually erase all contents of the file. Though the file is logically deleted and doesn’t appear to the be present, its contents still present in the hard drive. Any raw disk reading software or a data recovery tools can detect such contents to gain access to sensitive information such as passwords, credit card numbers etc.

Let us assume that you have stored your passwords and credit card numbers in a plain text file, even once temporarily. It is always wise to overwrite the file contents many a times before removing the file so that it becomes harder for raw disk software or any expensive hardware prober to recover any data.

Linux provides a command “shred” that can be used to overwrite a specific file repeatedly and optionally delete it.

shred mypassword.txt
rm password.txt

You can use “-u” option provided by shred to do the job with a single command.

shred -u mypassword.txt