lessfaq moved!

Posted in freebsd on December 10, 2008 by Vinod

this blog is moved to http://segfault.in

SSH login without password

Posted in ssh with tags , on December 7, 2008 by Vinod

For this you required to generate your own personal set of private/public pair. ssh-keygen is used to generate that key pair for you.

On the user’s home directory, on the localhost, type

[local-host]$ ssh-keygen -t dsa

This will ask you a passphrase. A passphrase is a sequence of words or other text used to control access to a computer system, program or data. A passphrase is similar to a password in usage, but is generally longer for added security. Once entered the passphrase you will be prompted to enter the same passphrase again for confirmation.

The private key was saved in .ssh/id_dsa and the public key .ssh/id_dsa.pub.

Now, copy the public key to the remote machine

[local-host]$ scp .ssh/id_dsa.pub user@remote:~/.ssh/id_dsa.pub

Now, login into the remote machine and go to the .ssh directory on the server side

[local-host]$ ssh user@remote
[remote-host]$
cd .ssh

Now, add the client’s public key to the known public keys on the remote machine.

[remote-host]$ cat id_dsa.pub >> authorized_keys2
[remote-host]$
chmod 640 authorized_keys2
[remote-host]$
rm id_dsa.pub
[remote-host]$
exit

Now on the localhost machine, on GNOME select System > Preferences > Sessions.
Select Startup Programs and add a new entry with this command.

eval `ssh-agent`

ssh-agent is a program that used together with OpenSSH or similar ssh programs provides a secure way of storing the passphrase of the private key.

Open terminal and run ssh-add without any arguments, it will ask your passphrase once.

ssh-add adds identities to the authentication agent, ssh-agent.

[local-host]$ ssh-add
Enter passphrase for /home/vinod/.ssh/id_dsa: <Enter your passphrase here>
Identity added: /home/vinod/.ssh/id_dsa (/home/vinod/.ssh/id_dsa)

That’s it, now login to remote server it will not ask any password or passphrase.

NB: No one else must see the content of .ssh/id_dsa, as it is used to decrypt all correspondence encrypted with the public key.

Record shell activities using script

Posted in shell with tags , , on July 16, 2008 by Vinod

Script makes a typescript of everything printed on your terminal. You can record shell activities using script.

$ script
Script started, file is typescript
$  echo “… do whatever …”
… do whatever …

Press Ctrl + D
Script done, file is typescript

$ col -bx < typescript > savefile
$ vim savefile

Making movies from image files using ffmpeg in Linux from command prompt

Posted in commands, linux with tags on July 11, 2008 by Vinod

First you need to have ffmpeg installed in your machine.

Install ffmpeg,

$ sudo apt-get install ffmpeg

If the files name are in the following sequence 1.jpg, 2.jpg, …, we can use:

$ ffmpeg -r 10 -b 1800 -i %d.jpg test.mp4

-b bitrate
Set the video bitrate in kbit/s (default = 200 kb/s).
-r fps
Set frame rate (default = 25).

It is important that the image files should be named subsequently.

For more information read man ffmpeg.

Convert pdf to jpeg in Linux from command prompt

Posted in commands, freebsd, linux with tags on July 11, 2008 by Vinod

First you need to have ImageMagick installed in your Linux machine.

To install ImageMagick in Debian, run the following command:

$ sudo aptitude install imagemagick

To convert pdf file to image use the ‘convert‘ command:

$ convert doc.pdf doc.jpeg

convert to tiff

$ convert doc.pdf doc.tiff

for more information look here.

How to mount an ISO image from command prompt?

Posted in commands, iso, linux with tags , on July 11, 2008 by Vinod


$ sudo mkdir /mnt/loop
$ sudo mount -o loop loop.iso /mnt/loop
$ ls /mnt/loop

Change the timezone from command prompt

Posted in linux with tags on July 9, 2008 by Vinod

In GNU/Linux or FreeBSD

$ sudo ln -sf /usr/share/zoneinfo/Asia/Calcutta /etc/localtime

will set the local time to ‘IST’

You can also use the environmental variable TZ to change zone

$ export TZ=Asia/Calcutta

Disable dma in Freebsd

Posted in freebsd on May 31, 2008 by Vinod

Most of the older IDE <-> CF Adapters only implement CF 1.5 and not CF 2.0. So, when FreeBSD goes to access the device, you get all kinds of timeout errors.

You can disable dma from the boot loader prompt

From the boot loader menu select the command prompt and run the following command.

set hw.ata.ata_dma=0

Now continue booting

boot

You can use atacontrol after booting. Read the man page here.


Getting root in X

Posted in linux on May 17, 2008 by Vinod

If you try to start a X window program from console as super user, you may get error messages like this.

Xlib: connection to “:0.0″ refused by server
Xlib: No protocol specified

xhost: unable to open display “:0.0″

This is because the user ‘root’ is not allowed to make connections to the X server. Use the ‘xhost‘ program to add ‘root’ to the list allowed to make connections to the X server.

As normal user run the following command.

$ xhost +local:root

and then root will be able to open x programs.

Add the following line to /root/.bashrc to avoid running the above command each time X starts.

export XAUTHORITY=/root/.Xauthority

How to create shared libraries with gcc?

Posted in gcc with tags on May 17, 2008 by Vinod

Shared libraries are libraries that are loaded by programs when they start. All programs that start afterwards automatically use the new shared library.

Code for the library:

This is the code that goes into the library. A function that calculate and return the factorial of a number.

fact.c

int
fact (int n)
{
        int fact  = 1;
        if(n == 0 || n == 1)
                return 1;
        while(n > 1) {
                fact = fact * n;
                n--;
        }
        return fact;
}

Now create a header file to define our function.

fact.h
int fact (int);

Creating Shared library:

First, create the object files that will go into the shared library using the gcc -fPIC or -fpic flag. The -fPIC and -fpic options enable “position independent code” generation, a requirement for shared libraries.

$ gcc -fPIC -Wall -g -c fact.c

Every shared library has a special name called the “soname”. The soname has the prefix “lib”, the name of the library, the phrase “.so”, followed by a period and a version number.

We can use ‘ld’, the GNU linker to create our shared library. The ld combines a number of object and archive files, relocates their data and ties up symbol references.

$ ld -shared -soname libfoo.so.1 -o libfoo.so.1.0 fact.o

Installing Shared library:

Once you’ve created a shared library, you’ll want to install it. The simple approach is simply to copy the library into one of the standard directories (e.g., /usr/lib) and run ldconfig(8).

Now copy the libfoo.so.1.0 to /usr/lib

$ sudo cp libfoo.so.1.0 /usr/lib/
$ sudo ldconfig -n /usr/lib/

Now create a symbolic link to our library.
$ sudo ln -sf /usr/lib/libfoo.so.1 /usr/lib/libfoo.so

Linking our Shared library:

This is the program that uses our ‘foo’ library.

main.c

#include <stdio.h>
#include "fact.h"

int
main(int argc, char *argv[])
{
        printf("%d\n", fact(4));
        return 0;
}

Compile:

$ gcc main.c -lfoo -o fact

Now run program:

$ ./fact
24