Showing posts with label ssh client. Show all posts
Showing posts with label ssh client. Show all posts

Tuesday, April 2, 2013

Transfer Files From One UNIX Server To Another Server Using Windows / Linux Desktop

transfer_imagesTransfer Files From One UNIX Server To Another Server Using Windows / Linux Desktop

Linux SCP Command explained

How do I securely transfer files from one UNIX / Linux server to another UNIX server using Windows or Linux desktop clients without using ftp client?

You need to use secure sftp or scp client for Windows XP / Vista / 7. Under Linux or Apple Mac OS X desktop you can use regular OpenSSH scp / sftp client to transfer files.

Windows SSH Client


You can use free SFTP, FTP and SCP client for Windows called putty or winscp.

Linux / UNIX / OS X SSH scp Client Examples


Use the following command from the server to which you want the files to go. In this example, transfer all files (/var/www/html) from remote server called server1 to local directory called /backup:
scp -r user@server1:/var/www/html/ /backup
In the following example, transfer all files (/var/www/html) from remote server called server1 to another server called server2:
scp -r user@server1:/var/www/html/ user@server2:/var/www/html/

Say hello to rsync


I recommend using rsync command which will only push or download updated files. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. In this example, copy files from remote server called server1 into /backup directory:
rsync -avz -e ssh user@server1:/var/www/html /backup

Friday, March 15, 2013

OpenSSH



OpenSSH


 
Working with OpenSSH
Back when the Internet was a big, happy family with only a hundred or so university servers connected to each other, it was practical and feasible to login to a remote computer using an plain-text protocol like telnet. This is, for reasons that should be obvious to most all Internet users, no longer advisable. For the most part, nobody logs in to a remote computer this way. They use a tool like OpenSSH.

What is OpenSSH?
OpenSSH is a free version of SSH Communications Security's SSH protocol. It is essentially a suite of tools for making secure connections. First, there is a daemon, sshd, which listens for connections from outside and performs authentication of those connections. There are three main client programs, ssh, the client shell, scp, a command-line program for copying from one machine to another and sftp, another program for copying in an ftp-like manner. There are also various programs to manage encryption keys that are used by clients and server.

Installation of OpenSSH
So that your system can both receive and make connections via OpenSSH, you need to have the following packages installed:
openssh
openssh-server
openssh-clients

All major distributions have these packages. In fact, they are normally installed by default.

OpenSSH Configuration
The main configuration file is /etc/ssh/sshd_config. There are a few changes that you should make to this file before you open up your machine to connections from the outside world.

First, its not a good idea to permit root logins to any machine. So that this cannot occur, make sure to change the file (or uncomment the line) so that the following line appears:
PermitRootLogin No

You may also want to allow remote machines to open X sessions. To do this, change the file (or uncomment the line) so that the following line appears:
X11Forwarding yes

* Most administrators feel that a Linux machine providing services like FTP, mail and Apache should not be running X. As a general rule, the more services a machine runs, the more risk of exploits. If the server isn't doubling as a workstation, it really shouldn't be running X. This will also save on resources as well.

Bending the Rules
Normally, to use 'ssh' or 'scp', you'd have to enter a password. There is a simple way to get around this. You may be asking yourself, however: why would you want to get around this? One reason is for sending backup copies automagically to other machines. Here's how to use scp without a password to send files:
First, create a user account on one machine. For this example, let's call it 'bkups'. Then login as 'bkups' and create a public key:
ssh-keygen -t rsa

To this, just press ENTER to everything. This will have created two files: id_rsa and id_rsa.pub in the directory /home/bkups/.ssh. Now, login to the other machine and create another user called 'bkups'. Now, go back to the to the first machine, from where you want to send the backup files. You now need to copy the file /home/bkups/.ssh/id_rsa.pub back to the other machine. For best results, this should not be done as the 'bkups' user. It is easiest to to this as 'root', but if you can't 'scp' it as root, then have root make a copy and chown it to a normal user and 'scp' it to the other machine. Then login to the other machine and get 'root' privileges.

Copy the file to /home/bkups/.ssh/authorized_keys and chown it to the 'bkups' user. Now, go back to the first machine again and try to use 'scp' to copy a file from 'bkups' on this machine to 'bkups' on the second. The first time it will ask you if you want to connect, but it won't ask for your password. Every subsequent time it will just copy the file without asking for a password. This makes automating your backups very easy.

OpenSSH



OpenSSH


 
Working with OpenSSH
Back when the Internet was a big, happy family with only a hundred or so university servers connected to each other, it was practical and feasible to login to a remote computer using an plain-text protocol like telnet. This is, for reasons that should be obvious to most all Internet users, no longer advisable. For the most part, nobody logs in to a remote computer this way. They use a tool like OpenSSH.

What is OpenSSH?
OpenSSH is a free version of SSH Communications Security's SSH protocol. It is essentially a suite of tools for making secure connections. First, there is a daemon, sshd, which listens for connections from outside and performs authentication of those connections. There are three main client programs, ssh, the client shell, scp, a command-line program for copying from one machine to another and sftp, another program for copying in an ftp-like manner. There are also various programs to manage encryption keys that are used by clients and server.

Installation of OpenSSH
So that your system can both receive and make connections via OpenSSH, you need to have the following packages installed:
openssh
openssh-server
openssh-clients

All major distributions have these packages. In fact, they are normally installed by default.

OpenSSH Configuration
The main configuration file is /etc/ssh/sshd_config. There are a few changes that you should make to this file before you open up your machine to connections from the outside world.

First, its not a good idea to permit root logins to any machine. So that this cannot occur, make sure to change the file (or uncomment the line) so that the following line appears:
PermitRootLogin No

You may also want to allow remote machines to open X sessions. To do this, change the file (or uncomment the line) so that the following line appears:
X11Forwarding yes

* Most administrators feel that a Linux machine providing services like FTP, mail and Apache should not be running X. As a general rule, the more services a machine runs, the more risk of exploits. If the server isn't doubling as a workstation, it really shouldn't be running X. This will also save on resources as well.

Bending the Rules
Normally, to use 'ssh' or 'scp', you'd have to enter a password. There is a simple way to get around this. You may be asking yourself, however: why would you want to get around this? One reason is for sending backup copies automagically to other machines. Here's how to use scp without a password to send files:
First, create a user account on one machine. For this example, let's call it 'bkups'. Then login as 'bkups' and create a public key:
ssh-keygen -t rsa

To this, just press ENTER to everything. This will have created two files: id_rsa and id_rsa.pub in the directory /home/bkups/.ssh. Now, login to the other machine and create another user called 'bkups'. Now, go back to the to the first machine, from where you want to send the backup files. You now need to copy the file /home/bkups/.ssh/id_rsa.pub back to the other machine. For best results, this should not be done as the 'bkups' user. It is easiest to to this as 'root', but if you can't 'scp' it as root, then have root make a copy and chown it to a normal user and 'scp' it to the other machine. Then login to the other machine and get 'root' privileges.

Copy the file to /home/bkups/.ssh/authorized_keys and chown it to the 'bkups' user. Now, go back to the first machine again and try to use 'scp' to copy a file from 'bkups' on this machine to 'bkups' on the second. The first time it will ask you if you want to connect, but it won't ask for your password. Every subsequent time it will just copy the file without asking for a password. This makes automating your backups very easy.

SSH: The VPN No One Remembers



SSH: The VPN No One Remembers


For anyone that doesn't know about VPNs, its basically the ability to use your server's resources (drives, bandwidth sometimes, etc...) remotely. So, for example, say you want to mount your server's /dev/sda6 partition to your home PC. You can use a VPN to do this, and you'll be able to browse all of those files from the luxury of your home PC.

I'm sure everyone is aware as to what SSH (specifically OpenSSH) is, especially since there's been a lot of discussion about it on LinuxForum.com as of late. But, I don't know if many people actually realize just how powerful SSH can be. If used right, you can turn a regular SSH server into a non-resource intensive, very much free VPN server. While it won't be as robust as say, OpenVPN, its definitely better than buying a whole new server just for VPN functionality, and SSH can mount remote directories as well using sshfs.


How To Start

This guide is pretty short because the steps are rather easy. There is an assumption that you have an already-working SSH install, however. What we are going to do is take that install, and build on it.

What I did personally for my set up, because I wanted to have two different access lists, is create a new SSH config file. For this, I just did the following;
cp /etc/ssh/sshd_config /etc/ssh/proxy_config

 

The reason for doing this is because I wanted to leave my SSH configuration separate from a proxy (and thus have two instances of SSH running, but the footprint is very minimal). I took out all of the commented stuff from the new proxy_config. Below are the most important parts to focus on:
Port ####

 


Of course change the "####" part, but change this from the regular SSH server.

PermitRootLogin no

 


You should never have this enabled to begin with, and just in case you flub up on your proxy account creation you'll want to make sure something bad doesn't happen.

PermitTunnel yes

 


I'm kind of on the fence about this one personally, it used to work without needing this but now its needed. Basically this lets you bind to the SSH server and make it act as a proxy server of sorts.

AllowUsers user1 user2 etc...
AllowGroups group1 group2 etc...

 


You can use one or both of these, but I'd highly recommend not using neither (as then it'd mean anyone can log into it). This is the meat and bones of the ACL of this proxy system. For the joy of not breaking anything, I only used AllowGroups and set it to my proxy group. Basically what happens is that SSH checks this list for each user authentication request, and if the user (or the user isn't in the specified group), SSH says "no entry!" and refuses the connection.


Testing
Now, assuming you made the appropriate changes to your firewall(s) and created any needed accounts or groups (highly advisable to NOT assign the user a shell, by the way), you should be ready to go. You can either copy & edit the start up script in similar fashion to the sshd_config file, or simply run this command:
/usr/sbin/sshd -f /etc/ssh/sshd_proxy

Side Note
Before continuing, I'd like to say something. If you decide to go the more flexible route and just copy & edit the SSH start up script, make sure you edit the PIDFILE variable, and whatever you name the pid (i.e.: sshd_proxy.pid), make sure you copy and rename the SSH file in /etc/conf.d/sshd to that as well. For example, if the pid is sshd_proxy.pid, your command will look like:
cp /etc/conf.d/sshd /etc/conf.d/sshd_proxy

 


Then edit that file change the name of the SSH config file. This might sound confusing, but when you look at it, it makes a lot more sense, I promise.


Connecting Remotely
Your SSH proxy all set up? The proxy running on the correct port? Good, now the coupe de grace. Fire up a terminal, and run the following command:
ssh -fND localhost:local_port_number -p port_proxy_is_running_on proxy_username@remote_server_hostname_or_ip

 


Making the appropriate changes, this will run the connection in the foreground (remove the "f" to make it run in the background, this is done to make sure everything runs smoothly). If all goes well, you'll see nothing happen, as in it'll look like its hung or frozen. For local_port_number, you should choose one that isn't used, and is higher than 1024. What you do now is use hostname localhost and port local_port_number (whatever it may be) for any programs you want to connect to via proxy (browser, IM client, e-mail program, etc...).


Just like any other proxy, the data for any programs using this proxy will be fed to the proxy server, so programs will always see the proxy's IP address as being yours. So for example, say we want to use local port 5555, the proxy is listening on port 9999, the proxy username is bob, and the server IP is 255.244.222.111. The command will look like this:
ssh -fND localhost:5555 -p 9999 bob@255.244.222.111

SSH: The VPN No One Remembers



SSH: The VPN No One Remembers


For anyone that doesn't know about VPNs, its basically the ability to use your server's resources (drives, bandwidth sometimes, etc...) remotely. So, for example, say you want to mount your server's /dev/sda6 partition to your home PC. You can use a VPN to do this, and you'll be able to browse all of those files from the luxury of your home PC.

I'm sure everyone is aware as to what SSH (specifically OpenSSH) is, especially since there's been a lot of discussion about it on LinuxForum.com as of late. But, I don't know if many people actually realize just how powerful SSH can be. If used right, you can turn a regular SSH server into a non-resource intensive, very much free VPN server. While it won't be as robust as say, OpenVPN, its definitely better than buying a whole new server just for VPN functionality, and SSH can mount remote directories as well using sshfs.


How To Start

This guide is pretty short because the steps are rather easy. There is an assumption that you have an already-working SSH install, however. What we are going to do is take that install, and build on it.

What I did personally for my set up, because I wanted to have two different access lists, is create a new SSH config file. For this, I just did the following;
cp /etc/ssh/sshd_config /etc/ssh/proxy_config

 

The reason for doing this is because I wanted to leave my SSH configuration separate from a proxy (and thus have two instances of SSH running, but the footprint is very minimal). I took out all of the commented stuff from the new proxy_config. Below are the most important parts to focus on:
Port ####

 


Of course change the "####" part, but change this from the regular SSH server.

PermitRootLogin no

 


You should never have this enabled to begin with, and just in case you flub up on your proxy account creation you'll want to make sure something bad doesn't happen.

PermitTunnel yes

 


I'm kind of on the fence about this one personally, it used to work without needing this but now its needed. Basically this lets you bind to the SSH server and make it act as a proxy server of sorts.

AllowUsers user1 user2 etc...
AllowGroups group1 group2 etc...

 


You can use one or both of these, but I'd highly recommend not using neither (as then it'd mean anyone can log into it). This is the meat and bones of the ACL of this proxy system. For the joy of not breaking anything, I only used AllowGroups and set it to my proxy group. Basically what happens is that SSH checks this list for each user authentication request, and if the user (or the user isn't in the specified group), SSH says "no entry!" and refuses the connection.


Testing
Now, assuming you made the appropriate changes to your firewall(s) and created any needed accounts or groups (highly advisable to NOT assign the user a shell, by the way), you should be ready to go. You can either copy & edit the start up script in similar fashion to the sshd_config file, or simply run this command:
/usr/sbin/sshd -f /etc/ssh/sshd_proxy

Side Note
Before continuing, I'd like to say something. If you decide to go the more flexible route and just copy & edit the SSH start up script, make sure you edit the PIDFILE variable, and whatever you name the pid (i.e.: sshd_proxy.pid), make sure you copy and rename the SSH file in /etc/conf.d/sshd to that as well. For example, if the pid is sshd_proxy.pid, your command will look like:
cp /etc/conf.d/sshd /etc/conf.d/sshd_proxy

 


Then edit that file change the name of the SSH config file. This might sound confusing, but when you look at it, it makes a lot more sense, I promise.


Connecting Remotely
Your SSH proxy all set up? The proxy running on the correct port? Good, now the coupe de grace. Fire up a terminal, and run the following command:
ssh -fND localhost:local_port_number -p port_proxy_is_running_on proxy_username@remote_server_hostname_or_ip

 


Making the appropriate changes, this will run the connection in the foreground (remove the "f" to make it run in the background, this is done to make sure everything runs smoothly). If all goes well, you'll see nothing happen, as in it'll look like its hung or frozen. For local_port_number, you should choose one that isn't used, and is higher than 1024. What you do now is use hostname localhost and port local_port_number (whatever it may be) for any programs you want to connect to via proxy (browser, IM client, e-mail program, etc...).


Just like any other proxy, the data for any programs using this proxy will be fed to the proxy server, so programs will always see the proxy's IP address as being yours. So for example, say we want to use local port 5555, the proxy is listening on port 9999, the proxy username is bob, and the server IP is 255.244.222.111. The command will look like this:
ssh -fND localhost:5555 -p 9999 bob@255.244.222.111

Set up SSH Keys



Set up SSH Keys


 


There are two machines A and B. The idea is to login (via ssh) from A to B without typing the password that machine B would normally ask for. A is called the client and B is called the server.
A single command on the client A does this (replace SERVERB by the correct machine name or IP address of server B ):


ssh-keygen -t dsa -f ~/.ssh/identity && cat  ~/.ssh/identity.pub | ssh SERVERB 'sh -c "cat -  >>~/.ssh/authorized_keys2 && chmod 600  ~/.ssh/authorized_keys2"'

 



Press enter twice. Then it will ask you for password when u run this command but this will be the last time it will ask. After this, you can jump to step 4 below.

Generate your public and private keys on client A, by running:


ssh-keygen -t dsa -f ~/.ssh/mykey

 




Just hit enter twice for the passphrase query.

Next, copy your public key on client A to Server B. Run the command:


cat ~/.ssh/mykey.pub | ssh SERVERB 'sh -c "cat -  >>~/.ssh/authorized_keys && chmod 600  ~/.ssh/authorized_keys"'

 




It will ask you for your password. Don't worry, this is the last time you will have to type it in.

This puts your public key on server B, in a special file that holds trusted public keys, and gives it appropriate permissions so that its readable by you only.

Move your private key to a file ssh looks for by default and make it secret by running this on client A:


mv ~/.ssh/mykey ~/.ssh/identity && chmod 600 ~/.ssh/identity

 




Since this file is like your password, it must be readable by you only.

You are all set. Now run:


ssh SERVERB

 




And server B should let you login without password.


Set up SSH Keys



Set up SSH Keys


 


There are two machines A and B. The idea is to login (via ssh) from A to B without typing the password that machine B would normally ask for. A is called the client and B is called the server.
A single command on the client A does this (replace SERVERB by the correct machine name or IP address of server B ):


ssh-keygen -t dsa -f ~/.ssh/identity && cat  ~/.ssh/identity.pub | ssh SERVERB 'sh -c "cat -  >>~/.ssh/authorized_keys2 && chmod 600  ~/.ssh/authorized_keys2"'

 



Press enter twice. Then it will ask you for password when u run this command but this will be the last time it will ask. After this, you can jump to step 4 below.

Generate your public and private keys on client A, by running:


ssh-keygen -t dsa -f ~/.ssh/mykey

 




Just hit enter twice for the passphrase query.

Next, copy your public key on client A to Server B. Run the command:


cat ~/.ssh/mykey.pub | ssh SERVERB 'sh -c "cat -  >>~/.ssh/authorized_keys && chmod 600  ~/.ssh/authorized_keys"'

 




It will ask you for your password. Don't worry, this is the last time you will have to type it in.

This puts your public key on server B, in a special file that holds trusted public keys, and gives it appropriate permissions so that its readable by you only.

Move your private key to a file ssh looks for by default and make it secret by running this on client A:


mv ~/.ssh/mykey ~/.ssh/identity && chmod 600 ~/.ssh/identity

 




Since this file is like your password, it must be readable by you only.

You are all set. Now run:


ssh SERVERB

 




And server B should let you login without password.


Tuesday, March 8, 2011

System Monitoring Tools Every SysAdmin Should Know




Linux Change Your Own Password


Login using the ssh client or open a command-line terminal (select Applications > Accessories > Terminal), than type the following command:
$ passwd


Rename an Account [ User ID ]


You need to use the usermod command. It can modify the system account files (such as /etc/passwd) to reflect the changes that are specified on the command line. The syntax is as follows:


In this example, rename login id from yuva to yahoo. Open a command-line terminal (select Applications > Accessories > Terminal), and then type the following command


 sudo usermod -l yahoo yuva


To verify new changes, enter:

$ id yahoo

20 Linux System Monitoring Tools Every SysAdmin Should Know

#1: top - Process Activity Command


The top program provides a dynamic real-time view of a running system i.e. actual process activity. By default, it displays the most CPU-intensive tasks running on the server and updates the list every five seconds.


#2: vmstat - System Activity, Hardware and System Information


The command vmstat reports information about processes, memory, paging, block IO, traps, and cpu activity.
# vmstat 3

#3: w - Find Out Who Is Logged on And What They Are Doing


w command displays information about the users currently on the machine, and their processes.

# w username
# w yuva