Wednesday, February 11, 2009

ssh/scp without password

Whenever you need to use scp to copy files, it asks for passwords. Same with rsync as it (by default) uses ssh as well. Usually scp and rsync commands are used to transfer or backup files between known hosts or by the same user on both the hosts. It can get really annoying the password is asked every time. I even had the idea of writing an expect script to provide the password. Of course, I didn't. Instead I browsed for a solution and found it after quite some time. There are already a couple of links out there which talk about it. I am adding to it...

Lets say you want to copy between two hosts host_src and host_dest. host_src is the host where you would run the scp, ssh or rsyn command, irrespective of the direction of the file copy!

  1. On host_src, run this command as the user that runs scp/ssh/rsync

    $ ssh-keygen -t rsa

    This will prompt for a passphrase. Just press the enter key. It'll then generate an identification (private key) and a public key. Do not ever share the private key with anyone! ssh-keygen shows where it saved the public key. This is by default ~/.ssh/id_rsa.pub:

    Your public key has been saved in /.ssh/id_rsa.pub

  1. Transfer the id_rsa.pub file to host_dest by either ftp, scp, rsync or any other method.

  1. On host_dest, login as the remote user which you plan to use when you run scp, ssh or rsync on host_src.

  2. Copy the contents of id_rsa.pub to ~/.ssh/authorized_keys

    $ cat id_rsa.pub >>~/.ssh/authorized_keys
    $ chmod 700 ~/.ssh/authorized_keys

    If this file does not exists, then the above command will create it. Make sure you remove permission for others to read this file. If its a public key, why prevent others from reading this file? Probably, the owner of the key has distributed it to a few trusted users and has not placed any additional security measures to check if its really a trusted user.

  1. Note that ssh by default does not allow root to log in. This has to be explicitly enabled on host_dest. This can be done by editing /etc/ssh/sshd_config and changing the option of PermitRootLogin from no to yes. Don't forget to restart sshd so that it reads the modified config file. Do this only if you want to use the root login.

Well, thats it. Now you can run scp, ssh and rsync on host_src connecting to host_dest and it won't prompt for the password. Note that this will still prompt for the password if you are running the commands on host_dest connecting to host_src. You can reverse the steps above (generate the public key on host_dest and copy it to host_src) and you have a two way setup ready!

Thursday, February 5, 2009

Tuesday, February 3, 2009

root password

Ubuntu Default Root Password or the sudo way

The first time I installed Ubuntu on a computer, I was rather used to debian and went through the whole install process easily.

When I rebooted my system and open a text-console, my first action was to su - in order to gain root's grants. What a surprise when I was promted for a password. Damn, What was it?!?

Through the installation process, I was asked to enter a username, I entered my usual username, type and confirmed my password, but I was never asked to enter the super-user root's password!?!

On a usual debian install, you are actually asked for root password and eventually you may want to create a first normal user(which is actually safer).

Ubuntu being a bit more user friendly, thought it was easier for the user to be promted for his password when he wants to do administration actions such as installing software, updating the system... To do this, ubuntu uses the sudo package which allow a user to gain root access for an action. In order to make sure that the user in front of the computer is actually the real user (not somebody using your access), the user is prompted for his password.

In Ubuntu, by default, there is not really a root user, but only root access which can be gained using sudo, for instance, to update your repository list:

:~$sudo apt-get update

Root is created without predefined password, it does not have a password, but it does not have an empty password either, you just can't login!!!

I actually like to be logged as root when I've got many this to do. I find sudo usefull when only one or two actio are to be done, but when doing administration task, typing sudo all the time just drive me cray :).

One way to gain root prompt is to launch a shell as root. Therefore, using sudo you can type:

:~$ sudo sh
Password:
sh-3.1#

And there you are, you have a root shell.

If you want to be able to login as root, you need to define a password for root:

:~$ sudo passwd
Password:
Enter new UNIX password:
Retype new UNIX password:

Sunday, February 1, 2009

事故处理应急

高速上事故,首先打开紧急灯,确认自己是否受伤,车位置是否安全,然后记下对方车
型,颜色,车牌,做这一切不需要下车。打911电话告知事故地点,有无受伤,一般911
会要求你把车挪开,不要block traffic。如果事故地点不危险可以不挪,警察一般3~
5分钟到。如果危险,和对方打手势交流后在保证安全的情况下挪到shouder上,等警察
的同时交换insurance信息,driver ID。如果报告911有人受伤,警察和ambulance会很快到,你的车警察会给你拖走,警察收集的driver insurance表会给你送到医院,ambulance费用自理或者由你的medical insurance cover部分。

追尾100%判后面人的责任。

警察分开询问两个人,你没责任,不问你也正常。

警察报告一般10天左右以后可以去警察局花钱买到,只是事故现场描述,不判定责任。

记得一定现场第一时间查看警察给你的表,上面有对方insurance信息,policy #,如果
填写不清楚立刻找警察确认,不然你没法claim.

有些人会故意提供过期的或虚假的insurance卡,如果可能记得现场打电话给insurance
确认,这时候如果记录了driver ID就很重要

如果在大城市local发生事故,警察根本就不会来,这时候如果有witness帮忙会简单得
多。