Sunday, August 24, 2008

How to enable remote desktop connection in linux server and VPS

yum -y groupinstall "KDE (K Desktop Environment)
yum -y install vnc vnc-server firefox x11-xorg
Now, you'll see VNC server is running by executing: "ps -ef |grep vncserver" -- good. Now kill the VNC process like so: "pkill -9 vnc" and then delete the temp sockets by: "rm -rf /tmp/.X1*" -- be careful when running rm -rf as root, you could have a typo and do rm -rf / by accident and destroy your VPS!

At this point you have to go into the /.vnc directory, edit the xstartup file and delete the last line (usually has twm & or something), and replace it with: "startkde &" (without the quotes) -- this tells VNC to startup KDE instead of the default window manager, twm (or whatever the last line was).

Your pretty much done at this point, go ahead and execute the command 'vncserver' again, and VNC will startup, using the password specified earlier and create a default VNC instance on VNC port 1.

Give it a few minutes, depending on your VPS specs, to let KDE start up for the first time. Start up your RealVNC client on your PC, and put in :1, and it should ask for your password that you put in earlier. If you dont see the KDE desktop, check your xstartup file again in /.vnc and make sure the last line is 'startkde &'.



Cheers!!!

Thursday, August 14, 2008

Rsync with incremental backup


If you want to do incremental backup between two servers. Let say there is a folder /root in
your local server which you want rsync to the /tmp/ribs via SSH
You have to first define switch for rsync command -racv -e than you have to tell user
and the protocal. Here we use ssh protocal and root user so "ssh -l root" than you have
to define the IP of the remote server that is 192.168.1 and than tell where you want
to backup on remote server. We define path /tmp/ribs now you will be able to backup the
root folder of local machine to the remote server /tmp/ribs

rsync -racv -e "ssh -l root" /root 192.168.1.1:/tmp/ribs

Switch
  • rsync - this syncs the local directory to with the server directory.
  • -e "ssh" - this tells rsync to use ssh if your want to pass in other ssh options such as port you can do that in the quotes: -e "ssh -p 12345"
  • -rca recursive, checksum, and archive
  • --delete-after - this will delete files on the server if you delete them locally.
Now you can create a shell script
#!/bin/sh
rsync -racv -e "ssh -l root" /root 192.168.1.1:/tmp/ribs

Now sure make the file is executable: chmod ug+x backupscript.sh

Next type crontab -e this will open up your scheduled tasks list, in most cases it will open an empty file. Add the following to the file:

*9 * * * root /root/backupscript.sh
Here we tell that to run this script everyday 9AM. This script will be run as root and the script
located in the /root folder by name /backupscript.sh will be executed

That's will schedule the script to run at AM.


Cheers!!



Wednesday, August 13, 2008

Passwordless login to the linux servers

If you have multiple servers and do not want to type password everytime you login to the server.
You can create the passwordless sessions using public key private key encryption.
Public Key to be stored on the remote server
private key to be stored in local machine
You have to download the puttygen.exe for generating the public private key pair.
Select RSA / DSA using the encryption required

Now
  1. Download PuTTY
  2. Download PuTTYgen
  3. Open PuTTYgen
    • Select SSH-2 RSA
    • Click ‘Generate’
  4. Save the Private Key
    • Click ‘Save private key’ (you do not have enter a password)
    • Save the private key in location easy to remember.
  5. Copy public-key
    • Select all text in the public key area.
    • right-click and select copy
  6. Configure the server settings in Putty
    • Open Putty
    • Click Session (left column)
      • Enter the server hostname or IP address
      • Enter a name under ‘Saved Sessions’
    • Click Connection > Data (left column)
      • Enter ‘root’ for the auto-login username
    • Click Connection > SSH (left column)
      • Select ‘2′ as the Preferred SSH protocol version.
    • Click Connection > SSH > Auth (left column)
      • Browse to the private key from step 4.
    • Click Session (left column)
      • Click Save
  7. Open a session with the server
    • Open PuTTY
    • Select the session saved earlier.
    • Click ‘Load’
    • Click ‘Open’
    • Login
  8. Add client public key.
    • You should still have the public-key in the clip-board from step 5.
    • Open the authorized_keys file on the linux server
      • [root@server]#vi ~/.ssh/authorized_keys2
      • Press the ‘i’ key to insert in vi.
      • Go to the bottom of the file and right-click on the putty screen (This should insert the public key generated with PuTTYgen)
      • Press the ‘esc’ key to get out of insert mode in vi.
      • Press ‘:’ to enter command mode in vi
      • Type ‘wq’ to write and quit vi
      I use nano editor if you do not have nano installed than run
    • #yum install -y nano
    • Now nano ~/.ssh/authorized_keys2
    • Copy the public key from puttygen and paste in this file. make sure you have all the matter in one single line or it won't work. I spent lot of time figuring it out.

  9. Now, you should be finished. Let’s test.
    • Open PuTTY
    • Select the session saved earlier.
    • Click ‘Load’
    • Click ‘Open’
  10. If you were able to login without entering your username and password you are finished!!If not, please continue to troubleshoot.

Troubleshooting: If you do not have a /root/.ssh folder, we will have to create one and set the permissions:

[root@server]#mkdir ~/.ssh
[root@server]#chmod 700 ~/.ssh

If you do not have a authorized_keys2 file, we will need to create one and set the permissions:

[root@server]#vi ~/.ssh/authorized_keys2
[root@server]#chmod 644 ~/.ssh/authorized_keys2

If you get an error that the key was rejected, you need to make sure the permissions are set correctly on the .ssh directory and authorized_keys2 file.

[root@server]#chmod 700 ~/.ssh
[root@server]#chmod 644 ~/.ssh/authorized_keys2

You are all set.

Enjoy!!!

How to secure copy

scp is the solution to do that

scp -r /source/folder root@IP:/folder

how to rsync folder from server to server

How to rsync
Lets say you have folder /vz/private/220 on local machine and you want to rsync to server 192.168.1.1 you have to run following command.


rsync -r -a -v -e "ssh -l root" /vz/private/220 192.168.1.1:/vz/private/220

This will sync the local folder and subfolder of 220 to the remote server in /vz/private/220

Cheers!!