Showing posts with label putty. Show all posts
Showing posts with label putty. Show all posts

Some advanced usages on Putty and OpenSSH

Jephe Wu - http://linuxtechres.blogspot.com

Objective: explore advanced usage on Putty on Windows and OpenSSH on Linux
Environment: Putty, OpenSSH


Usage:
1.  use putty or openssh client to create a secure socks web proxy tunnel
You can create socks proxy server by using putty or openssh.

Putty:
create a normal ssh session, enable compression and ssh version 2, under tunnel menu, create a auto and dynamic source port 8080.
then configure your favorite browser to use socks5 proxy at 127.0.0.1:8080,try socks4 proxy if socks5 doesn't work. Configure 'no proxy for' part with 'localhost,127.0.0.1'.

Note: For data you retrieved through browser, most of them is text or HTML data, the compression rate is very high, so, enable compression is better.

OpenSSH:
The following command uses compression, SSH2, Quite, Force pseudo-tty allocation, Redirect stdin from /dev/null, and use 'master' mode for ssh client for connection sharing. 

ssh -C2qTnN -D 8080 jephe@server.domain.com
 or

ssh -C2qTnN -L 8080:localhost:3333 jephe@server.domain.com
(ssh tunnel and local forwarding to ssh server at port 3333(squid proxy)  
 
You can try to access http://www.whatismyip.com/ to get the external source IP. 

login as root to your ssh server, then use above command to ssh into the destination server to create socks proxy tunnel, then use 127.0.0.1:8080 as socks5 proxy to access Internet.

DNS query issue:
when using above socks5 proxy, by default, firefox/thunderbird requires your local Windows pc must be able to resolve DNS request. If not, you can make changes for firefox/thunderbird to enable remote DNS (open the about:config page, and change network.proxy.socks_remote_dns to true), which also secure DNS queries)

Multiple tabs:
network.http.max-persistent-connections-per-proxy 25

Note: some socksifier program
a. http://widecap.com/
b. http://www.proxycap.com/
c. http://tsocks.sourceforge.net/(Linux)

2.  use http proxy software in ssh proxycommand option to ssh into Internet server directly
In office environment, you might not be able to direct ssh into some servers on the Internet. If the ssh server is listening at port 80 or 443, usually squid proxy server is allowing that, if not, you might need to do something to establish ssh connection, either by changing the destination server port to port 80/443 or enabling the squid to allow port 22.

For openssh/cygwin, you can use http://proxytunnel.sourceforge.net/ to use squid proxy server to tunnel your ssh connection.
In /etc/ssh/ssh_config, put
Host jephe
    Proxycommand /usr/bin/proxytunnel -p 10.0.0.1:8080 -d jephe.domain.com:22
   
then you can use 'ssh -v username@jephe -R 2222:localhost:22 -R 3389:10.0.0.2:3389' to do remote port forwarding to RDP/ssh to your office pc from home.

Another options is to use corkscrew -  http://www.agroman.net/corkscrew/
 
Please refer to another article for details at How to access office server and admin desktop from home - http://linuxtechres.blogspot.com/2010/12/how-to-access-office-server-and-admin.html

3. use nc in proxycommand option to directly ssh into server on Internet through firewall 
Case: You are not able to ssh directly to Internet , you have to ssh into firewall/proxy server, then you can ssh to Internet from firewall itself.

Solution: use nc to make it one step only.
on /etc/ssh/ssh_config, put the following line
Host external_ssh_server.domain.com
        ServerAliveInterval 60
        ServerAliveCountMax 600
    ProxyCommand ssh jephe@firewall_ip nc %h %p



Then run 'ssh username@jephe' to ssh directly to host on the Internet. 
 Note:
You might need to configure public key authentication without password for firewall and external_ssh_server.domain.com, otherwise, you might get something like 'write pipe error'.



Note: OpenSSH 5.4 onwards supports netcat mode with option -W host:port. See 
http://www.openssh.org/txt/release-5.4 


* Added a 'netcat mode' to ssh(1): "ssh -W host:port ..." This connects
   stdio on the client to a single port forward on the server. This
   allows, for example, using ssh as a ProxyCommand to route connections
   via intermediate servers.



The following example for using nc is by Fabian Arrotin from  http://planet.centos.org/
You need to ssh/scp from your pc to HostC directly. (normal path: your pc->hostA->hostB->hostC)
==================================

Host HostB
Hostname the.known.fqdn.as.resolvable.by.HostA
User arrfab
ForwardAgent yes
Port 22
ProxyCommand ssh remoteuser@HostA.with.ssh.access nc %h %p



And what if you need to reach HostC, which itself is only reachable by HostB ? Let’s just define a new Host section in the ~/.ssh/config and another ProxyCommand !

Host HostC
Hostname the.known.fqdn.as.resolvable.by.HostB
User arrfab
ForwardAgent yes
Port 22
ProxyCommand ssh remoteuser@HostB nc %h %p


====================================


4. use zmodem transfer with leputty (http://leputty.sourceforge.net/)

It's much faster for you to upload/download files directly with putty, without opening winscp to do it.


You can use leputty, configuring sz/rz path for default putty settings so that all session created later on will have this settings automatically.


When you need to upload file from Windows pc to server, just use 'Zmodem upload' in Leputty.
When you need to download a file, ssh into server with Leputty, then type in 'sz filename', then click on menu 'Zmodem receive' to transfer to the predefined directory on Windows PC.


It's faster then using winscp.



References:
a. https://calomel.org/firefox_ssh_proxy.html

How to access office server and admin desktop from home

Jephe Wu - http://linuxtechres.blogspot.com 

Objective: assume you have a  admin Windows xp pro desktop in office which is running 24 hours.  You need to access company Linux servers and the admin Windows pc from home.
Environment:  assume your office has lease line connecting to Internet, also 2 level of squid proxy server (parent proxy concept) running on the LAN for users to access Internet, Windows XP pro admin desktop pc and CentOS servers.


Connection diagram:
office lan - squid proxy - squid parent proxy
--internet firewall -[cloud] - Singapore ISP(starthub)--
home cable modem - wireless router - home Linux server + home Windows pc


Method 1: Directly SSH, VNC or RDP port forwarding on Internet firewall
You can do port forwarding for ssh,VNC or RDP on Internet firewall directly to your Linux admin server and Windows xp pro admin pc, use ssh public key authentication.

Other ways: VNC listening viewer mode, openvpn, hosted vpn such as LogMeIn Hamachi.



Method 2:  Teamviewer or logmein free edition
You can just let the admin Windows pc to have Internet connection, then install teamviewer or logmein on that PC.


Method 3: openssh/putty + proxytunnel +[Apache mod_proxy]
a. openssh + proxytunnel
Assume your 2 squid proxy has enabled port 22 for SSL
acl SSL_ports port 443 22
acl Safe_ports port 443 22

squid -k reconfigure

Configure home wireless router to port forwarding port 22 to your home Linux/Windows cygwin ssh server at port 22.

At home, configure ssh server /etc/ssh/sshd_config or /etc/sshd_config in cygwin (GatewayPorts yes)

In office, use the following ssh_config configuration (/etc/ssh/ssh_config): download proxytunnel v1.9 rpm and installed on admin linux server first.


Host jephe
    ServerAliveInterval 60
    ServerAliveCountMax 600

    Proxycommand /usr/bin/proxytunnel -p 10.0.0.2:8080 -d jephe.domain.com:22

Run the following command to ssh into home Linux/Windows pc:
ssh -v -R 3389:adminpc.jephe.com:3389 -R 2222:localhost:22  [-L 8080:192.168.10.1:80] [-g] 

note:
(-g means allows remote hosts to connect to local forwarded ports)
(-L part means you can connect to linux admin server at port 8080 which will be tunnelled to home wireless router admin access page)

How to connect to office from home:
use rdp to connect to localhost for office admin pc remote desktop. 
use putty to connect to localhost:2222 for ssh into office linux admin pc

note: Windows 7 Professional 64bit users:
a. You need to manually allow tcp port 22 incoming in Windows firewall incoming rules configuration
b. You need to allow remote desktop connection (right click computer, properties, advanced system settings,remote, choose users if necessary)
c. You cannot use port 3389 and 3390 for port forwarding at localhost, use 3391 instead.
so, after ssh, you should use localhost:3391 for connecting to office desktop pc.

note:
I have tried cygwin 1.7.7 on Windows xp for remote port portforwaring which is also working, without proxytunnel and http proxy. 

b. openssh+nc
you can use the following in the /etc/ssh/ssh_config

Host test
    hostname dest_server_ip
    ProxyCommand ssh jephe@firewall_ip nc -w 1 %h %p


c. putty + proxytunnel
According to my test, putty can use http proxy option or external program proxytunnel. I tried both options, the remote port forwarding part doesn't work(putty 0.60 and latest snapshot,also Xshell, CentOS 5.5 openssh server), the normal port forwarding through putty is working, which means you can access home pc and wireless router when you are in office, but not office pc and linux admin server when you are at home.

Putty's http proxy:
Just specify the squid proxy ip and port number, it will be able to tunnel your ssh connection through squid proxy:


Proxytunnel:
go into the Connection > Proxy menu. Select the Local proxy type. And then provide as Telnet command, or local proxy command the following line:
proxytunnel -q -p squidproxy.jephe.com:8080 [-r jephe.apacheserver.com:443] -d %host:%port


-r part is used for Apache mod_proxy, refer to Reference 1 link.

Note: another options for http proxy is http://www.agroman.net/corkscrew/


note: 
zmodem file transfer within Leputty(http://leputty.sourceforge.net/) and xshell



References:
2. proxytunnel homepage: http://proxytunnel.sourceforge.net/intro.php

3. tunnel ssh over http(s): http://dag.wieers.com/howto/ssh-http-tunneling/
4. use corkscrew tool
http://www.agroman.net/corkscrew/
http://daniel.haxx.se/docs/sshproxy.html
5. HowTo SSH outside using Authenticated Proxy
http://www.sohailriaz.com/howto-ssh-outside-using-authenticated-proxy/


SSH and keepalive

Jephe Wu -  http://linuxtechres.blogspot.com

Objective: make your SSH connection more stable. Do not disconnect due to inactivity
Environment: CentOS 5, Windows XP, putty 0.60, ssh client on CentOS 5

Concepts:
1.  Why SSH connection somehow discontinues during idle time
Router or firewall in between make the connection state invalid
According to http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html .This behavior is caused by the connection tracking procedures implemented in proxies and firewalls, which keep track of all connections that pass through them. Because of the physical limits of these machines, they can only keep a finite number of connections in their memory. The most common and logical policy is to keep newest connections and to discard old and inactive connections first.

Thus the trick is to send packets as infrequently as possible over idle connections.

2. TCP keepalive and application level keepalive
According to http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter4.html#config-keepalive .
TCP keepalives is similar to application-level keepalives, and the same caveats apply. The main differences are:

    * TCP keepalives are available on all connection types, including Raw and Rlogin.(in Putty)
    * The interval between TCP keepalives is usually much longer, typically two hours; this is set by the operating system, and cannot be configured within PuTTY.
    * If the operating system does not receive a response to a keepalive, it may send out more in quick succession and terminate the connection if no response is received.

TCP keepalives may be more useful for ensuring that half-open connections are terminated than for keeping a connection alive. Although it also can Prevent disconnection due to network inactivity

3. TCP keepalive
3.1 how it works
After authentication, ssh sends a 32 byte empty packet to the sshd every n seconds. sshd does not care about this, but the server's TCP stack must send back an ACK for that packet. If the client's TCP stack does not receive an ACK for this or a later packet, it will retransmit for some time and then signal a connection-timeout to ssh, causing ssh to exit.

3.2 configuration of tcp keepalive


/proc/sys/net/ipv4/tcp_keepalive_intvl
/proc/sys/net/ipv4/tcp_keepalive_probes
/proc/sys/net/ipv4/tcp_keepalive_time


or permanently set them in /etc/sysctl.conf as follows:
net.ipv4.tcp_keepalive_time = 7200
net.ipv4.tcp_keepalive_intvl = 75
net.ipv4.tcp_keepalive_probes = 9


note:
a. above settings are default ones, you can change it.
b. If the network hardware or software drops connections that have been idle for less than the two hour default, the Client session will fail. KEEPALIVE timeouts are configured at the OS level for all tcp connections that have KEEPALIVE function enabled in their application, and there's option to choose it. such as the one in Putty.


If the network hardware or software (including firewalls) have a idle limit of one hour, then the KEEPALIVE timeout must be less than one hour. To rectify this situation TCP/IP KEEPALIVE settings can be lowered to fit inside the firewall limits. The implementation of TCP KEEPALIVE may vary from vendor to vendor. The original definition is quite old and described in RFC 1122.

4. Application level keepalive



4.1 How to configure it for openssh client command 'ssh' to prevent disconnection
man ssh_config on Linux, you get

ServerAliveInterval:
Sets a timeout interval in seconds after which if no data has been received from the server, ssh will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server.

This option applies to protocol version 2 only.

ServerAliveCountMax:
Sets the number of server alive messages (see above) which may be sent without ssh receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session. It is important to note that the use of server alive messages is very different from TCPKeepAlive (below). The server alive messages are sent through the encrypted channel and therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The server alive mechanism is valuable when the client or server depend on knowing when a connection has become inactive.

The default value is 3. If, for example, ServerAliveInterval (above) is set to 30, and ServerAliveCountMax is left at the default, if the server becomes unresponsive ssh will disconnect after approximately 90 seconds.

we can use the following command

% ssh -o TCPKeepAlive=no -o ServerAliveInterval=30
or

put above options in /etc/ssh/ssh_config
You can use like this:

    ServerAliveInterval 60
    ServerAliveCountMax 600 
(default is 3 according to man ssh_config)


   
or
put above options in $HOME/.ssh/config  (see man ssh_config)

Make sure you set like this: ServerAliveInterval*ServerAliveCountMax <= 0.8*N, N being the timeout. The default value of ServerAliveCountMax is 3 (man ssh_config) and therefore a a 3x30 = 90 seconds if you guessed a disconnect is about less then 1.5min).

If you set it too low, there will be unnecessary traffic between client and server to keep alive, so it decrease performance.



4.2 How to configure it to prevent ssh disconnection for Putty

Enable tcp keepalive and 'seconds between keepalive' are totally different things, one is for TCP level keepalive, another is application level implementation. 

option 1: use tcp keepalive.
'Connection' menu:
Disable Nagle's algorithm
Enable TCP keepalives

option 2: use application level keepalive
'connection' - seconds between keepalives (0 to turn off)

You might consider to eanble 'Connection' -> 'SSH' -> 'X11'
Enable X11 forwarding
Enable MIT-Magic-Cookie-1


 Save the session


The following is from Putty documentation http://the.earth.li/~sgtatham/putty/0.58/htmldoc/Chapter4.html#config-keepalive

If you find your sessions are closing unexpectedly (most often with ‘Connection reset by peer’) after they have been idle for a while, you might want to try using this option.

Some network routers and firewalls need to keep track of all connections through them. Usually, these firewalls will assume a connection is dead if no data is transferred in either direction after a certain time interval. This can cause PuTTY sessions to be unexpectedly closed by the firewall if no traffic is seen in the session for some time.

The keepalive option (‘Seconds between keepalives’) allows you to configure PuTTY to send data through the session at regular intervals, in a way that does not disrupt the actual terminal session. If you find your firewall is cutting idle connections off, you can try entering a non-zero value in this field. The value is measured in seconds; so, for example, if your firewall cuts connections off after ten minutes then you might want to enter 300 seconds (5 minutes) in the box.

Note that keepalives are not always helpful. They help if you have a firewall which drops your connection after an idle period; but if the network between you and the server suffers from breaks in connectivity then keepalives can actually make things worse. If a session is idle, and connectivity is temporarily lost between the endpoints, but the connectivity is restored before either side tries to send anything, then there will be no problem - neither endpoint will notice that anything was wrong. However, if one side does send something during the break, it will repeatedly try to re-send, and eventually give up and abandon the connection. Then when connectivity is restored, the other side will find that the first side doesn't believe there is an open connection any more. Keepalives can make this sort of problem worse, because they increase the probability that PuTTY will attempt to send data during a break in connectivity. Therefore, you might find they help connection loss, or you might find they make it worse, depending on what kind of network problems you have between you and the server.

Keepalives are only supported in Telnet and SSH; the Rlogin and Raw protocols offer no way of implementing them. (For an alternative, see section 4.13.3.)

Note that if you are using SSH-1 and the server has a bug that makes it unable to deal with SSH-1 ignore messages (see section 4.23.1), enabling keepalives will have no effect.




connect to server or router serial console port under Linux or Windows

Jephe Wu - http://linuxtechres.blogspot.com




Please check also this comprehensive article regarding how to login from serial console - How to login from Linux serial console - http://linuxtechres.blogspot.com/2009/09/how-to-login-from-serial-port-under.html

Objective: connect to server or route serial console port under Linux or Windows
Environment: CentOS 5, Windows




Methods:

1. minicom under Linux
run minicom command under CentOS 5, it will firstly try to look  for /etc/minirc.df1, if it doesn't exist, it might prompt you the following warning
message:
minicom: WARNING: configuration file not found, using defaults
Device /dev/modem access failed: No such file or directory.


Then, you can configure the serial port by using command 'minicom -s', save setting as df1, a typical minicom configuration file is below:

----
$ more /etc/minirc.df1
# Machine-generated file - use "minicom -s" to change parameters.
pr port        /dev/ttyS0
pu baudrate    9600
pu bits        8
pu parity     N
pu stopbits    1
-----

If you are not using modem, you can blank out A B and K for 'modem and dialing' part. So it will become like this:

# Machine-generated file - use "minicom -s" to change parameters.
pr port             /dev/ttyS0
pu baudrate         9600
pu minit          
pu mreset         
pu mhangup       
  


Note:
a. please make sure the serial port setting is correct, you can vi file /etc/minirc.df1 to check.
because sometimes, the screen display will be corrupt.
b. If error comes then you can set hardware flow control to no.


2. Hyperterminal or Putty under Windows

The easist way is to use Putty, choose serial port and baud rate, then connect

3. hardware such as Raritan KVM over IP Switch

http://www.raritan.com/products/kvm-over-ip/

4. HP ILO VSP(virtual serial port)

http://linuxtechres.blogspot.com/search?q=vsp


5. References
http://www.howtoforge.com/setting_up_a_serial_console

6. FAQ
a. For Sun machines, if both end is the DB25/ethernet port connector or DB9/ethernet port connector, you might need to use standard network cable to connect both connectors. Otherwise, the ok prompt will not come up.

b.Sometimes, if you are expecting a menu coming up in the minicom console, you might need to enter 'ctrl +L' to get the menu appear before you see anything.