Search code examples
linuxpython-3.xbashredhatsudo

sudo or not sudo?


The ultimate goal is to run this single command from inside a shell script on a redhat machine. I've used the script for years on an ubuntu machine, but i have fewer privileges on redhat. I'll describe my attempted solution below, but wanted to frame the question first.

read -r val1 val2 val3 <<<$(python3 script_name.py "$json_args")

In redhat, i had to install python/pip3.5 as sudo... like this...

sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install python35u
sudo yum -y install python35u-pip
sudo pip3.5 install --upgrade pip

sudo pip3.5 install boto3
sudo pip3.5 install awscli --upgrade --user

different machines may have different python versions, so I created an alias for python3 in .bash_profile so the same shell script works everywhere.

echo 'alias python3="python3.5"' >>~/.bash_profile

now... everything was locked down in python... I could import boto3, but it wasn't usable... from python3 command line to demonstrate...

>>> import boto3
>>> boto3.__version__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'boto3' has no attribute '__version__'

so I tried running python3 as 'sudo', but got...

sudo: python3: command not found

so I added an alias to .bashrc like this...

echo 'alias sudo="sudo "' >>~/.bashrc

Great! now things seem to work! i can finally run the command i originally wanted (see below).

sudo python3 script_name.py args

or more specifically (note the added sudo compared to my intro)...

read -r val1 val2 val3 <<<$(sudo python3 script_name.py "$json_args")

working great from the command line!

... until I try putting it in a shell script. now I'm back to my original error...

sudo: python3: command not found

I've tried all kinds of things... putting the 'sudo ' alias in the script... putting it in /root/.bashrc... a few other random things.

At this point I suspect i could run the bash script as sudo too... but that starts to cause all kinds of other problems and I suspect is a pretty terrible security practice. I feel like I've gone off the rails and there's some much smarter solution here.

any ideas for either

  • how to run python3.5 without sudo and get it to work
  • how to get the sudo python3 to work inside the shell script without running the shell script as sudo?

thanks in advance

EDIT

based on suggestions below from @JulianLoaiza and @TerryCarmen, the chown -R allows me to run python3 without the sudo... but boto3 can't authorize me anymore when I do. Checking sys.path there's only one difference from python's perspective... that's

  • sudo has: '/root/.local/lib/python3.5/site-packages'
  • w/out sudo has: '/home/ec2-user/.local/lib/python3.5/site-packages'

both have '/usr/lib/python3.5/site-packages', which appears last (and contains the libs I explicitly installed).

what might be happening... /root/.local/lib/python3.5/site-packages has nothing in it about awscli or boto... /home/ec2-user/.local/lib/python3.5/site-packages DOES have 'awscli' and 'botocore' stuff in it. So does /usr/lib/python3.5/site-packages... which also has boto3 and other libs I explicitly intalled.

Could python be getting confused by looking in /home/ec2-user/.local/lib/python3.5/site-packages before /usr/lib/python3.5/site-packages when I'm not logged in as 'sudo'?


Solution

  • You can try change the folder ownership,

    sudo chown -R ec2-user:ec2-user /usr/lib/python2.7
    sudo chown -R ec2-user:ec2-user /usr/lib64/python2.7