HOWTO - kill terminal services sessions remotely

If you use Windows Terminal Services for remote desktop access, it's common to run into this nasty error message from time to time:

The terminal server has exceeded the maximum number of allowed connections.

The error refers to the maximum 2 remote connections that are allowed to a Windows 2000 server (or up... XP only allows 1). Now, only a crazy person would ever consider connecting more than one or two users remotely to a server, and even though that would never, ever, even in a million years, need to happen, that error still crops up from time to time.

The problem is that if a user closes their terminal services client without logging out, their remote session switches to "disconnected" mode and will continue using up one of those two available connections.

While I'm sure we all agree that this is an awesome feature, sometimes in an emergency you need to log into a server and all of its connections are already in use. There are a couple of really useful and mostly unknown command line utilities that will allow you to remotely find and kill other remote desktop sessions, whether they are in a connected or disconnected state.

On most systems, these tools are qwinsta.exe and rwinsta.exe, though on some 2003 systems these appear to have been renamed and might be available as query.exe and reset.exe, respectively. Use whichever is available on your machine.

Finding disconnected sessions with qwinsta or query
Run "qwinsta /server:theserver_or_ip" or "query session /server:theserver_or_ip", using the server name or ip address of the machine you can't log in to. You'll get something like the following:

SESSIONNAME  USERNAME  ID  STATE  TYPE  DEVICE
console              0   Conn    wdcon
rdp-tcp              65536  Listen  rdpwd
rdp-tcp#5     auser   1  Active  rdpwd
rdp-tcp#7     another  2  Disc  rdpwd

From this, you can see that users "auser" and "another" are using the two remote Terminal Services connections. Furthermore, the remote desktop client that "auser" is using is still connected to the server, whereas "another" is in a disconnected state, probably the result of closing the remote desktop window instead of logging off.


Killing disconnected sessions with rwinsta or reset
Run "rwinsta sessionid /server:theserver_or_ip" or "reset session sessionid /server:theserver_or_ip", using the session id obtained above (1 for "auser" and 2 for "another") and the name or ip address of the server. In this case, you'd probably want to kill session 2, since that user is disconnected anyway.

The command will run without a response, but if you run qwinsta (or query) again, you'll notice that the session is no longer there. More importantly, if you try and connect to the machine using remote desktop, you'll get in.

Posted by Jason Striegel | Aug 15, 2007 08:40 PM
Windows Server | Permalink | Comments (5) Bookmark and Share

Recent Entries

Comments

Newest comments listed first.

Posted by: thousandrobots on August 15, 2007 at 11:44 PM

great post. i used to run into this all the time at my old job (where i was an accidental admin). the users would unknowingly leave themselves logged in, and i was always sending them an explanation of the difference between "disconnecting the session" and actually logging off. wish i had known this stuff at the time ... might've saved some aggravation!


Posted by: mr.sticky on August 18, 2007 at 7:08 PM

Alternatively, you can log in to the console session via RDP by adding /console after the IP address in the connect field.


Posted by: Motoma on August 20, 2007 at 12:51 PM

Most certainly a handy trick, and the sarcasm is thoroughly appreciated!


Posted by: menglis3 on August 21, 2007 at 3:06 AM

See MS support artciles on MSTSC switches and use of Mstsc.exe with the CONSOLE switch.


Posted by: VOODOOS!L on June 3, 2008 at 2:50 AM

net use

your post helped me to get my remote deskotp working again.
Before I was able to execute the qwinsta or rwinsta on a remote server, I had to authenticate to the server using net use, for example: 'net use \\ /USER:(\) ""', example: net use \\192.168.1.1 /USER:administrator "password"
maybe this can help some other users too ;)


Leave a comment



Bloggers

Welcome to the Hacks Blog!

Brian Jepson.Brian Jepson


Jason Striegel.Jason Striegel


Philip Torrone.Phillip Torrone



See all of the books in the Hacks Series!
Advertise here.

Recent Posts

www.flickr.com
photos in Hacks More photos in Hacks