Execute a command as a non super user in a sudo script [duplicate] - linux

This question already has answers here:
Stop being root in the middle of a script that was run with sudo
(2 answers)
Closed 7 years ago.
I developed a script.
This script should called with sudo:
$ sudo ./script
In my script I have a command in the middle which I want to execute it as a non super user (like if it's executed without sudo)
Is it posssible to do that?

Technical it's possible with sudo -u plain_user command in your script. This way of scripting is somehow strange to me though.

Related

ssh: dealing with nested strings in bash [duplicate]

This question already has answers here:
How to escape the single quote character in an ssh / remote bash command?
(5 answers)
Closed 9 months ago.
Im using ssh with a system that has a lot of security settings. The bash command I want to run is:
ssh –t user#address su –c ‘echo “user ALL=(ALL) NOPASSWD:ALL” >> /etc/sudoers.d/permissions’
This command does not work since su does not recognize the stuff after it as a command. EOF is disabled by default for ssh and EOSU is disabled by default on all computers in the network. Sudo is also disabled for the user on the host computer. Is there a clean way to do this while calling su as little as possible?
Note: I already did keygen stuff so the ssh login is passwordless, root login through ssh is disabled by default.
When you pass a command to ssh, it requires quoting both from the local shell and the remote shell.
A fix for your case would be to move the first single quote slightly, to prevent the local shell from messing with the command at all. In the internal quoting, we use nested double quotes where the inner ones are backslashed.
ssh –t user#address 'su –c "echo \"user ALL=(ALL) NOPASSWD:ALL\"" >> /etc/sudoers.d/permissions’

Shell Script - Run shell commands in parallel in different bash sessions [duplicate]

This question already has answers here:
How do you run multiple programs in parallel from a bash script?
(19 answers)
Closed 2 years ago.
I have a requirement to run database restore commands in parallel from shell scripts. Both the commands should run in different bash sessions in parallel.
The following are the commands I need to run.
sudo su - $user -c "db2 RESTORE DATABASE ${SDBP} FROM '/dbnfs/db2main/backups/${DB2DBP}' TAKEN AT $TIMESTAMPP ON '/data1/DB2/tablespaces/${DB2DBP}' , '/data2/DB2/tablespaces/${DB2DBP}' DBPATH ON '/home/db2inst1' INTO ${DB2DBP} NEWLOGPATH '/data1/activelogs/${DB2DBP}' without rolling forward without prompting 2>&1"
sudo su - $user -c "db2 RESTORE DATABASE ${SDBS} FROM '/dbnfs/db2main/backups/${DB2DBS}' TAKEN AT $TIMESTAMPS ON '/data1/DB2/tablespaces/${DB2DBS}' , '/data2/DB2/tablespaces/${DB2DBS}' DBPATH ON '/home/db2inst1' INTO ${DB2DBS} NEWLOGPATH '/data2/activelogs/${DB2DBS}' without rolling forward without prompting 2>&1"
Let me know how to achieve it.
Since you want different bash sessions (perhaps due to long running commands), screen command might be of interest to you.
You can create new named screens (sessions), let's call it restore1 for the first command:
screen -S restore1
This will create a new screen. In this screen you can run your first command. Once command starts running, you can "detach" (ctrl+a d) from it.
Create another named screen called restore2 for SDBS:
screen -S restore2
Run the second command here, then detach from it. You can check the screens (sessions) by:
screen -list
You can reattach to any of the screens with screen -r <screen_name>, to check on the status of that command. Example:
screen -r restore1

node daemon won't start with process.stdin.setRawMode(true) [duplicate]

This question already has answers here:
Input of Information into Javascript using terminal
(2 answers)
Closed 4 years ago.
I am running a node server daemon but I keep running into this error.
When I run my bash strip to test the application I get TypeError: process.stdin.setRawMode is not a function.
Can you help me find a way I can use keyboard input with this node application running in the background?
I have tried giving my bash script permissions chmod 777 x.sh & chmod 755 x.sh
setRawMode() is only available when the input is provided by a TTY and not like yours as direct stream from stdin.
Use this to check what stream you have:
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
}

How to use output redirection with sudo? [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 5 years ago.
I wish to write text from stdin to a file that is owned by root.
I'm doing this programmatically across an SSH connection - essentially from a script - so using a text editor is out of the question. There is no terminal. The process is entirely automated.
Given that [a] root elevation can be obtained via sudo, and [b] files can be written to using cat with redirection, I assumed the following would work:
ssh user#host sudo cat >the-file
Unfortunately, the redirection is applied to sudo, not to cat. How can I apply redirection to cat in this example?
The normal pattern to do this is:
ssh user#host 'cat | sudo tee the-file'
tee redirects output to a file and can be run with sudo.
If you want to mimic >> where the file is appended-to, use sudo tee -a.
You'll also need to be sure to quote your command as above, so that the pipe isn't interpreted by the local shell.
Edit
The tee command is part of POSIX, so you can rely on it existing.
To redirect Standard Error as well:
ssh user#host 'some_command 2>&1 | sudo tee output-file'

Permission denied even with sudo [duplicate]

This question already has answers here:
How do I use sudo to redirect output to a location I don't have permission to write to? [closed]
(15 answers)
Closed 6 years ago.
Following this tutorial
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04
and trying to use the command
echo 'prefix=/usr/local' > node/etc/npmrc
however I get a permission denied error, even when using sudo.
Any ideas?
echo 'prefix=/usr/local' > node/etc/npmrc
however I get a permission denied error, even when using sudo.
You haven't shown us the failing command using sudo. Please update your question and show us the exact command that failed, along with the exact error message.
Meanwhile, I can guess that the failing command was:
sudo echo 'prefix=/usr/local' > node/etc/npmrc
That runs the echo command with root privileges (which is not particularly useful, since you can runecho as an ordinary user). The redirection is handled by your current shell process, and is subject to the permissions of the current user.
Since > is handled by the shell, you need a shell running as root to handle it:
sudo sh -c "echo 'prefix=/usr/local' > node/etc/npmrc"

Resources