3

I'm using a program named ajenti on linux. it's a program to manage linux servers and it's written in python.

I need to get linux username of logged in user for different sections like file manager. it is required to restrict user's access.

the source of that software can be found here, and for example in line 73 of file manager the access can be defined. however I'm getting username by functions like "os.path.expanduser" or "pwd.getpwuid(os.getuid()).pw_name" now, it only returns "root" regardless of the logged in user.

I want each username, what should I do?

P.S: This is not a duplicate. I know there has been questions like mine but that's not exactly what I want. there is a slight difference between them.

9
  • @sumit I'm said I'm using this but that's does not return what I want. Commented Jun 10, 2016 at 9:46
  • what return value do you want? Commented Jun 10, 2016 at 9:54
  • @Snowleaf Did you try ALL the answers there? Commented Jun 10, 2016 at 10:04
  • @MSurrow the username, not "root". Commented Jun 10, 2016 at 10:04
  • @Snowleaf have you checked that you are not in fact logged in as root in the session where you are testing your script? (that could be a classical brainfart:) (or running python as root) Commented Jun 10, 2016 at 10:08

1 Answer 1

18

Could os.getlogin() be what you are looking for? (https://docs.python.org/3/library/os.html#os.getlogin)

>>> import os
>>> os.getlogin()
'msurrow'
>>> 

Or maybe getpass.getuser() (https://docs.python.org/3.1/library/getpass.html)

>>> import getpass
>>> getpass.getuser()
'msurrow'
>>> 

There is a difference between who the logged in user is, and 'who' the python process is running as. Jeff explains more in his answer here

Sign up to request clarification or add additional context in comments.

2 Comments

I think the problem is what you and Jeff said. but how can I solve it? I must somehow get user that is logged in.
if the python process is run as root, some of the functions will return 'root' (eg. getpass.getuser() will).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.