Menu
Newbedev LogoNEWBEDEV Python Javascript Linux Cheat sheet
Newbedev LogoNEWBEDEV
  • Python 1
  • Javascript
  • Linux
  • Cheat sheet
  • Contact

How to run part of a script with reduced privileges?

You may want to use this trick:

{ anycommand } | su -c 'tee file' user

tee(1) is POSIX utility, so you may rely on its availability.


Or, with sudo:

{ anycommand } | sudo -u user 'tee file'

Use a subshell: (su -c 'psql -U postgres -c "<command>"' postgres) > file

Inside the subshell you can drop permissions to do your work, but output is redirected to your original shell which still has your original permissions.

Tags:

Linux

Permissions

Bash

Shell Script

Related

SPEC %files attribute and Shell variables Task manager keyboard shortcut in Linux? piped program in sendmail's /etc/aliases Once sudo su'd to root, is there a command to see my username? Is there an easy way to update information in known_hosts when you know that a hosts key has changed? Filtering invalid utf8 Need to upgrade svn on centos Why put some config info in conf/httpd.conf and some in files in the conf.d folder? find searching in parent directories instead of subdirectories bulk rename (or correctly display) files with special characters How to check if $PWD is a subdirectory of a given path How to "jail" a process without being root?

Recent Posts

Pandas how to find column contains a certain value Recommended way to install multiple Python versions on Ubuntu 20.04 Build super fast web scraper with Python x100 than BeautifulSoup How to convert a SQL query result to a Pandas DataFrame in Python How to write a Pandas DataFrame to a .csv file in Python
© 2021 newbedevPrivacy Policy