Discussion:
Running a Python Service under the LocalService or NetworkService Account
mistersexy
2009-07-20 13:57:57 UTC
Permalink
I am trying to create a Windows service in Python using pywin32. I do
not want this service to run under a user account. I want this service
to be able to run as a LocalService, NetworkService and the like. How
do I specify this using the win32 library? Thanks, everyone.
Tim Golden
2009-07-20 14:03:14 UTC
Permalink
Post by mistersexy
I am trying to create a Windows service in Python using pywin32. I do
not want this service to run under a user account. I want this service
to be able to run as a LocalService, NetworkService and the like. How
do I specify this using the win32 library? Thanks, everyone.
When you "install" the service, using the HandleCommandLine
option, specify --username= and --password options.

TJG
mistersexy
2009-07-20 15:55:35 UTC
Permalink
Post by Tim Golden
Post by mistersexy
I am trying to create a Windows service in Python using pywin32. I do
not want this service to run under a user account. I want this service
to be able to run as a LocalService, NetworkService and the like. How
do I specify this using the win32 library? Thanks, everyone.
When you "install" the service, using the HandleCommandLine
option, specify --username= and --password options.
TJG
That's exactly my point. I do not want to have to specify username and
password options. For instance, when creating a Windows Service
in .NET, it is possible to specify that the service should run using
the LocalService or NetworkService account. Doing this, you would not
need to specify username and password options. Is there a way to
achieve this in Python?
Tim Golden
2009-07-20 16:14:31 UTC
Permalink
Post by mistersexy
Post by Tim Golden
Post by mistersexy
I am trying to create a Windows service in Python using pywin32. I do
not want this service to run under a user account. I want this service
to be able to run as a LocalService, NetworkService and the like. How
do I specify this using the win32 library? Thanks, everyone.
When you "install" the service, using the HandleCommandLine
option, specify --username= and --password options.
TJG
That's exactly my point. I do not want to have to specify username and
password options. For instance, when creating a Windows Service
in .NET, it is possible to specify that the service should run using
the LocalService or NetworkService account. Doing this, you would not
need to specify username and password options. Is there a way to
achieve this in Python?
Sorry, I misread: I mentally removed the "not" in your 'I do not want
this service to run under a user account' and reinserted it
further on!

By default, the service will run as LocalSystem: you
only specify a username to override that default. The value
in Username is passed straight through to the CreateService
Win32 API, and the docs for that:

http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx


say:

"""
lpServiceStartName [in, optional]

The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName.

If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account.

If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account.

A shared process can run as any user.

If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system.

A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide.

Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2.
"""



So, although I haven't tried it, it looks as though you can pass
"LocalService" or "NetworkService" and so on if you want to
override the default LocalSystem but don't want to specify a
username/password.

TJG
David Adamo Jr.
2009-07-20 16:25:44 UTC
Permalink
Post by Tim Golden
Post by mistersexy
Post by Tim Golden
Post by mistersexy
I am trying to create a Windows service in Python using pywin32. I do
not want this service to run under a user account. I want this service
to be able to run as a LocalService, NetworkService and the like. How
do I specify this using the win32 library? Thanks, everyone.
When you "install" the service, using the HandleCommandLine
option, specify --username= and --password options.
TJG
That's exactly my point. I do not want to have to specify username and
password options. For instance, when creating a Windows Service
in .NET, it is possible to specify that the service should run using
the LocalService or NetworkService account. Doing this, you would not
need to specify username and password options. Is there a way to
achieve this in Python?
Sorry, I misread: I mentally removed the "not" in your 'I do not want
this service to run under a user account' and reinserted it
further on!
By default, the service will run as LocalSystem: you
only specify a username to override that default. The value
in Username is passed straight through to the CreateService
?http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx
"""
lpServiceStartName [in, optional]
? ? The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName.
? ? If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account.
? ? If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account.
? ? A shared process can run as any user.
? ? If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system.
? ? A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide.
? ? ? ? Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: ?Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2.
"""
So, although I haven't tried it, it looks as though you can pass
"LocalService" or "NetworkService" and so on if you want to
override the default LocalSystem but don't want to specify a
username/password.
TJG
I'll try this stuff. Thanks a million...I'll let everyone know how it
goes.
sightseer
2009-07-21 07:27:45 UTC
Permalink
Post by David Adamo Jr.
Post by Tim Golden
Post by mistersexy
Post by Tim Golden
Post by mistersexy
I am trying to create a Windows service in Python using pywin32. I do
not want this service to run under a user account. I want this service
to be able to run as a LocalService, NetworkService and the like. How
do I specify this using the win32 library? Thanks, everyone.
When you "install" the service, using the HandleCommandLine
option, specify --username= and --password options.
TJG
That's exactly my point. I do not want to have to specify username and
password options. For instance, when creating a Windows Service
in .NET, it is possible to specify that the service should run using
the LocalService or NetworkService account. Doing this, you would not
need to specify username and password options. Is there a way to
achieve this in Python?
Sorry, I misread: I mentally removed the "not" in your 'I do not want
this service to run under a user account' and reinserted it
further on!
By default, the service will run as LocalSystem: you
only specify a username to override that default. The value
in Username is passed straight through to the CreateService
?http://msdn.microsoft.com/en-us/library/ms682450%28VS.85%29.aspx
"""
lpServiceStartName [in, optional]
? ? The name of the account under which the service should run. If the service type is SERVICE_WIN32_OWN_PROCESS, use an account name in the form DomainName\UserName. The service process will be logged on as this user. If the account belongs to the built-in domain, you can specify .\UserName.
? ? If this parameter is NULL, CreateService uses the LocalSystem account. If the service type specifies SERVICE_INTERACTIVE_PROCESS, the service must run in the LocalSystem account.
? ? If this parameter is NT AUTHORITY\LocalService, CreateService uses the LocalService account. If the parameter is NT AUTHORITY\NetworkService, CreateService uses the NetworkService account.
? ? A shared process can run as any user.
? ? If the service type is SERVICE_KERNEL_DRIVER or SERVICE_FILE_SYSTEM_DRIVER, the name is the driver object name that the system uses to load the device driver. Specify NULL if the driver is to use a default object name created by the I/O system.
? ? A service can be configured to use a managed account or a virtual account. If the service is configured to use a managed service account, the name is the managed service account name. If the service is configured to use a virtual account, specify the name as NT SERVICE\ServiceName. For more information about managed service accounts and virtual accounts, see the Service Accounts Step-by-Step Guide.
? ? ? ? Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000: ?Managed service accounts and virtual accounts are not supported until Windows 7 and Windows Server 2008 R2.
"""
So, although I haven't tried it, it looks as though you can pass
"LocalService" or "NetworkService" and so on if you want to
override the default LocalSystem but don't want to specify a
username/password.
TJG
I'll try this stuff. Thanks a million...I'll let everyone know how it
goes.
I'm finding it hard to install my Python service. When I try to
install the service, this is what I get:

Error Installing Service: Access is Denied. (5)

Event with a correct username and password (I think).
Also, inheriting from the win32serviceutil.Framework class, I do not
see a way to explicitly specify the serviceType. I know some of the
parameters such as the startupType can be specified when installing
the service in the command line, but how are these things specified in
the service code when inheriting from win32serviceutil.Framework
(especially the serviceType).

Does this mean I would have to code my service from scratch without
using win32serviceutil.Framework? Basically, I still can't install my
service using a NetworkService or LocalSystem Account because it still
requires me to specify a username and password. Following Tim Golden's
advice, I'm trying to specify the service type in order to determine
what account to run the service under. Please help!
Martin P. Hellwig
2009-07-21 09:40:54 UTC
Permalink
sightseer wrote:
<knip>
Post by sightseer
Error Installing Service: Access is Denied. (5)
<knip>
Are you trying to do this on windows vista?
--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
David Adamo Jr.
2009-07-21 12:49:45 UTC
Permalink
On Jul 21, 10:40?am, "Martin P. Hellwig" <martin.hell... at dcuktec.org>
Post by Martin P. Hellwig
<knip>
Post by sightseer
Error Installing Service: Access is Denied. (5)
<knip>
Are you trying to do this on windows vista?
--
MPHhttp://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
Yeah, I was trying to do it on Vista. Someone has just helped me out.
I had to deactivate User Account Control on Windows Vista...and now
everything is rosy. Thanks guys.
Martin P. Hellwig
2009-07-21 19:05:55 UTC
Permalink
On Jul 21, 10:40 am, "Martin P. Hellwig" <martin.hell... at dcuktec.org>
Post by Martin P. Hellwig
<knip>
Post by sightseer
Error Installing Service: Access is Denied. (5)
<knip>
Are you trying to do this on windows vista?
--
MPHhttp://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
Yeah, I was trying to do it on Vista. Someone has just helped me out.
I had to deactivate User Account Control on Windows Vista...and now
everything is rosy. Thanks guys.
No need to deactivate it, just right click on the command shell program
and say run as administrator, than you can install the service via the
command line.
--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
David Adamo Jr.
2009-07-22 07:13:49 UTC
Permalink
On Jul 21, 8:05?pm, "Martin P. Hellwig" <martin.hell... at dcuktec.org>
Post by Martin P. Hellwig
On Jul 21, 10:40 am, "Martin P. Hellwig" <martin.hell... at dcuktec.org>
Post by Martin P. Hellwig
<knip>
Post by sightseer
Error Installing Service: Access is Denied. (5)
<knip>
Are you trying to do this on windows vista?
--
MPHhttp://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
Yeah, I was trying to do it on Vista. Someone has just helped me out.
I had to deactivate User Account Control on Windows Vista...and now
everything is rosy. Thanks guys.
No need to deactivate it, just right click on the command shell program
and say run as administrator, than you can install the service via the
command line.
--
MPHhttp://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
Thanks MPH

Loading...