Pages

Sunday, January 06, 2008

How to disable root access from ssh

By default, usually ssh allow us to login as root. If we are concerned a lot about security, we'll want to disable root access from ssh. Belows are steps to disable direct root access from ssh:

  1. login to your server, and gain root access (by sudo or su - )

  2. Edit ssh configuration file. Usually the file is located at /etc/ssh/sshd_config

  3. Find the line: PermitRootLogin yes, then replace the line into: PermitRootLogin no

  4. If you'd like to only enable protocol version 2, find the line: Protocol 2,1 then replace it into: Protocol 2

  5. Save the files

  6. restart ssh:
    /etc/rc.d/init.d/sshd restart

That's all :)

Saturday, January 05, 2008

How to ssh/scp w/o password prompt

Sometimes, we'd like to automate a file transfer process between main server and backup server. We can use scp to transfer files but usually scp requires input for password prompt. Belows are quick steps to remove the password prompt for scp/ssh.Basically, there are two steps needed:
  1. create public and private key in the ssh client
  2. copy or append the public key content to the ssh server

If you are using SSH version 2, follow the following steps:
  1. at the client machine, login as the user who will execute the ssh or scp, then create pair of public and private key using dsa to ~/.ssh/id_dsa with the following command:
    $ ssh-keygen -t dsa -f .ssh/id_dsa
    (If there's a prompt asking for password, leave it blank. There will be two files created on ~/.ssh/ : id_dsa (private key) and id_dsa.pub (public key))
  2. Copy or append the content of id_dsa.pub (public key) above to the ssh server at the home directory of the ssh user: ~/.ssh/authorized_keys2

If you are using SSH version 1, follow the following steps (very similar):
  1. At the client machine, login as the user who will execute the ssh or scp, then create pair of public and private key using rsa to ~/.ssh/id_rsa with the following command:
    $ ssh-keygen -t rsa -f .ssh/id_rsa
    (If there's a prompt asking for password, leave it blank, there will be two files created.)
  2. Copy or append the content of id_rsa.pub (public key) above to the ssh server at the home directory of the ssh user: ~/.ssh/authorized_keys

That's all :)

Update:
Don't forget to set the file permission for the public key file in ssh server:
chmod 600 ~/.ssh/authorized_keys or chmod 600 ~/.ssh/authorized_keys2

That's all :)

Friday, January 04, 2008

How to use vi style in Bash

If you are vi fans, probably you want to have vi command style in bash shell.

Simply add the following command in your ~/.profile_bash file:
set -o vi

That's all :)

Thursday, January 03, 2008

How to get yesterday time

Sometimes, we need to create a simple script in linux which involves retrieving system time several days/hours/minutes ago.

Belows are example use of date command:

To get yesterday time:
$ date --date="1 days ago"
or
$ date --date="-1 days"


To get one month ago:
$ date --date="1 months ago"
or
$ date --date="-1 months"


To get one hour ago:
$ date --date="1 hours ago"
or
$ date --date="-1 hours"


To get one minutes in the future:
$ date --date="+1 minutes"


To get one seconds in the future:
$ date --date="+1 seconds"


We can also combine it, example to get one month and two hours ago:
$ date --date="1 month 2 hours ago"
or
$ date --date="-1 month -2 hours"

There are many other ways to express the particular time.

That's all :)

Wednesday, January 02, 2008

Bash keyboard shortcut

Below is list of keyboard shortcut for bash shell.

Why we need keyboard shortcut? The short answer is: to save time!
Especially, when we are accessing bash remotely (eg: by ssh) and the network access pretty slow, we will need a keyboard shortcut such as deleting the entire command line, moving to the beginning of the command, etc to save a lot of time.

Below are some of keyboard shortcuts for bash. Maybe in certain conditions it does not work properly.

  • ALT + b : move the cursor backward one word.

  • ALT + f : move the cursor forward one word.

  • ALT + d : cut one word after the cursor.

  • ALT + u : uppercase one word after the cursor.

  • CTRL + a : go to the first character of the line.

  • CTRL + b : move back one character.

  • CTRL + c : Sending signal SIGINT the current process you run (usually the process will be killed).

  • CTRL + d : Sending EOF, Exit/logout the current shell.

  • CTRL + e : go to the last character of the line.

  • CTRL + f : move forward one character.

  • CTRL + h : clear one character before the cursor. (similar with backspace)

  • CTRL + k : cut all characters after the cursor.

  • CTRL + l : clear screen.

  • CTRL + r : search for previously used commands.

  • CTRL + t : swap the last two characters before the cursor.

  • CTRL + u : cut all characters before the cursor.

  • CTRL + w : delete one word before the cursor.

  • CTRL + y : recall the last cut character by CTRL+u or CTRL+k or ALT+d (just like paste)

  • CTRL + z : put the current process you run into background process. To restore it, use command fg.

  • ESC + t : swap the last two words before the cursor.

  • TAB : autocomplete command/files or folders name.


That's all :)