S
S
Sergey Karbivnichy2021-09-16 22:45:06
linux
Sergey Karbivnichy, 2021-09-16 22:45:06

How to log in with a different username in a bash script?

I make a clean server configuration script for my needs:

USER='user'
PASS='555'

sudo useradd -G sudo -m -d /home/$USER --password $(echo "$PASS" | openssl passwd -1 -stdin ) $USER

apt update
apt install mc sqlite ncdu htop git nload curl zip tmux python3-pip -y

Here I create a new user user with a password of 555. Next, I install the necessary packages.

Next, you need to install the Python libraries for the user user:
login -p user # Как передать сюда пароль 555?
pip3 install bs4 lxml requests

And I ran into a problem, I don’t know how to automatically enter the password for the login command:login -p user

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Saboteur, 2021-09-16
@hottabxp

sudo su -l user -c 'pip3 install --user bs4 lxml requests'

under sudo, the su command will not ask for a password.

S
Sergey Karbivnichy, 2021-09-16
@hottabxp

One of the options:
su -l user -c 'pip3 install bs4 lxml requests'

A
Adamos, 2021-09-16
@Adamos

sudo login -f -p user

P
pfg21, 2021-09-16
@pfg21

sudo can accept password from stdin, option
-S, --stdin
Write the prompt to the standard error and read the password from the standard input instead of using the terminal device.
but this is not secure, it is more correct to write the necessary line for launching the program without asking for a password in /etc/sudoers
articles in bulk, for example https://losst.ru/otklyuchaem-parol-sudo-v-linux

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question