Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm doing development with PyCharm, doing development on a script that uses the scapy module for Python to do port scans, sniff traffic, and build raw packets. This, of course, requires root permissions, and so I run PyCharm as root (Yes, I'm aware of the security issues this presents).

The problem is, when PyCharm contacts the company license server, it submits the current username, which of course, ends up being "root", and the license server rejects my request. If I don't launch PyCharm as root, then it submits my actual username and successfully gets a license, but then I can't run my script because it needs root permissions to do the things it does.

Is there a way to make PyCharm run and submit my actual username to the license server, but then have root privileges, without just editing my account to give myself root permissions?

share|improve this question

I imagine sudo would be perfect for this. As a quick test:

$ echo $USER
$ sudo echo $USER
[sudo] password for halosghost:

That is, I have hightened permissions, but maintain my user's identity. The -E flag may also be particularly useful. It makes sure the environment variables are preserved during execution.

share|improve this answer
I was using sudo before and it wasn't working. PyCharm started, but couldn't get a license from the server. I did it now and it worked. It might be worth mentioning that while doing "sudo echo $USER" does show my username and not root, "sudo whoami" tells me I'm root. EDIT: I'm wondering if there's someone else in the company also using PyCharm as root, and my license request was failing because someone else was already holding the license using "root" as the username. I'm not sure exactly how PyCharm's floating licenses work. – Sohcahtoa82 Jun 6 '14 at 20:35
It would be helpful to know how PyCharm detects your username. – HalosGhost Jun 6 '14 at 20:40
Indeed it would. From within PyCharm, I opened a Python console and used it to check environment variables, and LOGNAME, USERNAME, and USER are all "root". SUDO_USER is of course my actual username. If I run os.system('echo $USER') from that prompt, it tells me I'm root. So it looks like it is probably still telling the license server that I'm root, the server has just decided to give me a license this time, for some reason. – Sohcahtoa82 Jun 6 '14 at 20:54
The reason why you are seeing the results you are seeing is that the shell expands environment variables before passing them to the command. Since you are using sudo, the shell cannot use the echo builtin (as far as the shell is concerned, echo is just one more parameter to sudo). So the shell expands $USER to halosghost (your username), then executes sudo with parameters echo and halosghost. In turn, sudo sets the security context and then executes echo with the parameter halosghost. Congratulations, you just ran (most likely) /bin/echo as root. – Michael Kjörling Apr 12 at 12:50

How about running PyCharm on your local machine, as yourself, and doing your development inside a container, such as one provided by Vagrant (natively supported in PyCharm) or Docker (supported via plugin)?

share|improve this answer

This worked for me ( You essentially create a different interpreter for python which is a shell script that calls python with sudo. When it talks about creating a sudoers file (/etc/sudoers.d/python) I had to add the line Defaults!/usr/bin/python2.7 !requiretty to enable execution without a tty.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .