Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7726955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:20:23+00:00 2026-06-01T05:20:23+00:00

I am using ubuntu in my server. I have two users, let them be

  • 0

I am using ubuntu in my server.

I have two users, let them be user1 and user2

Each user have their own project folder with permissions set due to their needs. But user1 needs to run a python script which is in the other user’s project folder. I use subprocess.Popen for this. The python file have required access permissions, so i do not have problem in calling that script. But log files (which have permission for user2) causes permision denied error (since they belong to other user, not the one i need to use).

So i tried to change the user with

Popen("exit", shell=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE) #exit from current user, and be root again
Popen(["sudo", "user2"], shell=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
Popen("/usr/bin/python /some/file/directory/somefile.py param1 param2 param3", shell=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)

But second Popen fails with

su: must be run from a terminal

Is there any way to change user within python shell?

PS: Due to some reasons, i can not change the user permissions about log files. I need to find a way to switch to other user…

EDIT: I need to make some explenaiton to make things clear…

I have two different django projects running. each projects have a user, have their own user folders where each project codes and logs are kept.

Now, in some cases, i need to transfer some data from project1 to projet2. Easiest way looks to me is writing a python script that accept parameters and do relevant job [data insertion] on the second project.

So, when some certain functions are called within project1, i wish to call py script that is in the project2 project folder, so i can do my data update on the second prject.

But, since this i call Popen within project1, currnt user is user1. But my script (which in in project2) have some log files which denied me because of access permissions…

So, somehow, i need to switch from user1 to user2 so i will not have permission problem, and call my python file. Or, do find another way to do this..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:20:25+00:00Added an answer on June 1, 2026 at 5:20 am

    Generally, there is no way to masquerade as another user without having root permission or knowing the second user’s login details. This is by design, and no amount of python will circumvent it.

    Python-ey ways of solving the problem:

    • If you have the permission, you can use os.setuid(x), (where x is a numerical userid) to change your effective user id. However, this will require your script to be run as root. Have a look at the os module documentation.

    • If you have the permission, you can also try su -c <command> instead of sudo. su -c will require you to provide the password on stdin (for which you can use the pexpect library)

    Unix-ey ways of solving the problem:

    • Set the group executable permission on the script and have both users in the same group.
    • Add the setuid bit on the script so that user1 can run it effectively as user2:

      $ chmod g+s script
      

      This will allow group members to run the script as user2. (same could be done for ‘all’, but that probably wouldn’t be a good idea…)

    [EDIT]: Revised question

    The answer is that you’re being far too promiscuous. Project A and project B probably shouldn’t interact by messing aground with each other’s files via Popening each other’s scripts. Instead, the clean solution is to have a web interface on project A that gives you the functionality that you need and call it from project B. That way, if in the future you want to move A on a different host, it’s not a problem.

    If you insist, you might be able to trick sudo (if you have permission) by running it inside a shell instead. for example, have a script:

    #!/bin/sh
    sudo my/problematic/script.sh
    

    And then chmod it +x and execute it from python. This will get around the problem with not being able to run sudo outside a terminal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with my connection tunneling. So I'm using two ubuntu server
I have a bluetooth server running on my Ubuntu desktop written using PyBlueZ. That
I have two issues. I have Ubuntu 10.10 and I have installed git server
i have a virtual ubuntu server running in windows 7 pro using VMware Player.
I using an Ubuntu server and I have mod_rewrite loaded but for some reason,
I have been using an Ubuntu 8.04 server with fail2ban for a while now
I have two GeoServers running in the same machine Ubuntu Server 9.04. GeoServer V2.0.1
I am trying to host two websites using Apache from the same Ubuntu server.
I just installed Glassfish on my Ubuntu server (No GUI) using THIS tutorial. Everything
I wrote a server-client rmi application and I'm using MacOsX and Ubuntu laptops to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.