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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Snowleaf
  • 67
  • 1
  • 3
  • 8
  • @sumit I'm said I'm using this but that's does not return what I want. – Snowleaf Jun 10 '16 at 09:46
  • what return value do you want? – MSurrow Jun 10 '16 at 09:54
  • @Snowleaf Did you try ALL the answers there? – Bhargav Rao Jun 10 '16 at 10:04
  • @MSurrow the username, not "root". – Snowleaf Jun 10 '16 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) – MSurrow Jun 10 '16 at 10:08
  • @MSurrow I don't understand your point exactly! this program needs root privilege to run. but is there another way to get user in the software? – Snowleaf Jun 10 '16 at 10:15
  • @Snowleaf not that i know.linux allows for multiple users to be logged in at the same time, so if you had a "getLoggedInUser" function, what should that return if multiple users are logged in? One option is to return the user running the script, which is what the methods you tested do and here is your problem: that user is root. You could maybe try and get the list of logged in users, and if there is only one user besides root, then maybe you can assume that is the answer you want. But that may be risky since two users could actually be logged in and you may assign wrong privileges to the user – MSurrow Jun 10 '16 at 10:38
  • @Snowleaf this may be a place to start: http://stackoverflow.com/questions/14319023/find-out-who-is-logged-in-on-linux-using-python – MSurrow Jun 10 '16 at 10:38
  • the accepted answer in the duplicate question does not necessarily work on Linux: https://stackoverflow.com/a/842096/44330 – Jason S Nov 11 '19 at 23:02

1 Answers1

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

Community
  • 1
  • 1
MSurrow
  • 1,183
  • 2
  • 9
  • 23
  • 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. – Snowleaf Jun 10 '16 at 10:07
  • if the python process is run as root, some of the functions will return 'root' (eg. getpass.getuser() will). – MSurrow Jun 10 '16 at 10:15