Changing to another users account to access files in bash - linux

Basically I want to change directory but to another users account how do I do this is it possible?

Use su, then the command to run as the user:
$ su otheruser
$ cd /home/otheruser

You can switch user and get a shell with the command su < username >, you must know the password for that user though. If you want to execute a single command instead of having a full shell add that command with the -c switch. Man pages here

Related

Execute scripts from Django server by other unix user

I have django server installed under ownership of user say 'X'. Now I want to switch to user 'Y' and execute some scripts. Currently for changing user I am using sudo su "Y" -c "commands to execute" . I have added user "X" in sudoers file so that now it does not ask for a password.
Is there any way to do it without sudo.I have already tried it by editing /etc/pam.d/su file so that it does not ask for password when user X runs "su Y" without sudo.
Is there any other way in which this can be achieved?
You can use setuid bit for script, I think.
chmod a+u your_script_to_execute
will do the trick. Then any user will launch this script with priviligies of it's owner
You can read more at https://linuxconfig.org/how-to-use-special-permissions-the-setuid-setgid-and-sticky-bits

How can I run a command as another use + use user's environment/config variables from home folder in Linux?

I have a lot of configuration for Kwin (the window manager) in another user's home folder (/home/B/.config and other folders).
Is there a way to run Kwin from my session, but make Kwin consider the other user's configuration and environment as well?
I can't copy the files over to my account because they're scattered around B's home folder, and I don't want to risk overwriting stuff.
I tried using sudo, but it doesn't run the command as if it was the B's session, and uses my own config files to run the program instead of those in /home/B/.
If the -H sudo parameter is not enough, maybe you also need something from shell resource files that -i would give you, so something like sudo -i -uusername command.
From sudo's man page:
-i, --login Run the shell specified by the target user's password
database entry as a login shell. This means that
login-specific resource files such as .profile or
.login will be read by the shell. If a command is
specified, it is passed to the shell for execution
via the shell's -c option. If no command is
specified, an interactive shell is executed. sudo
attempts to change to that user's home directory
before running the shell. The command is run with an
environment similar to the one a user would receive
at log in. The Command environment section in the
sudoers(5) manual documents how the -i option affects
the environment in which a command is run when the
sudoers policy is in use.

Difference between environment variables get loaded when using sudo linux

Hello I would like to understand the differences w.r.t env that gets setup using sudo su - vs just a sudo su.
User already a part of sudoers
Not able use few kerberos commands when I use just sudo su.
A look in the manual page for the su command showed the following:
-, -l, --login
Start the shell as a login shell with an environment similar
to a real login:
o clears all the environment variables except TERM
o initializes the environment variables HOME, SHELL,
USER, LOGNAME, and PATH
o changes to the target user's home directory
o sets argv[0] of the shell to '-' in order to make
the shell a login shell
So, if you use sudo su - the above mentioned variables are erased and filled with information according to your new user (root or another).
Maybe some needed environmental variables are set for your root user?
Unfortunately, I haven't worked with kerberos yet.
Hope I helped you a bit.

Linux passwd command : formatting the ouput

I have this script in which a user can change its password using passwd transparently. The script itself is executed by root, launching it with
su - <user> -c "script"
I know it might not be very safe a way to launch the script but that is how it is and I have no lattitude to change that part.
My problem is that when called, passwd displays the following:
Changing password for user <user>.
Changing password for <user>
current (UNIX) password:
New UNIX password:
Retype new UNIX password:
Several things to note here:
Why does it even begin with two lines ? It seems the first is displayed when root calls passwd for and the second when calls passwd on himself. Can it be the start of an explanation ?
I need to filter some words out of those prompts. I thought of using a combination of greps and seds piped one after the other but here is the trick: the two first lines seem to be outputed to stdout, but the others to stderr. When I try to redirect stderr to stdout to treat it, nothing gets displayed anymore.
Has anyone got any answer or tips regarding this situation ? Thanks a lot.
(First question here so do not hesitate to ask for more info.)
Try keying:
su - vartaghan -c passwd
onto the command line and then contrast that with keying:
passwd
onto the command line.
The answer is right there. Because you are using su to implement the command it requires the password to be keyed in and then the passwd command becomes active, which requires the password all over again.
Your best option would be to change the way that the menu which runs for your users, starts this password changing shell, by simply issuing the passwd command.
Edit:
If you want to get rid of the I/O use something like:
(echo $1; echo $2; echo $2) | passwd &>/dev/null
Which requires that you run the script as myscript oldpassword newpassword

How can I run a sudo script from "external tools" in IntelliJ / WebStorm / PhpStorm?

I would like my root-requiring bash script to be run from IntelliJ/WebStorm, asking me for the root password when I run it. Having my root password hardcoded in the script is a bad idea of course.
IntelliJ/WebStorm actually has a $Prompt$ macro for reasons like this, which prompts you and uses your input as a value.
So I tried using $Prompt$ along with echo YOURPASSWORD | sudo -S yourcommand as described in use-sudo-with-password-as-parameter.
Then I pass passwd & script to run to a sudorun.sh script echo -e $1 | sudo -S $2 $3 $4 (since echo can't be be the 'program' line) which although works on the CLI, it fails to read echo-stdin on the IntelliJ console.
Ideally, I would like the solution to be configured solely from within IntelliJ and not require specific OS configuration changes outside of IntelliJ.
Perhaps there are other ways to deal with this, so lets improvise!
I, too, faced the same issue, but I work with sensitive data on my development machine and removing the password requirement for sudoers just isn't an option.
I was able to resolve this issue by launching the actual WebStorm application from the command line using the sudo command as follows:
sudo /Applications/WebStorm.app/Contents/MacOS/webide
Once WebStorm/PhpStorm are launched this way, you can run a script with root access without supplying root credentials.
Use the NOPASSWD feature of sudo. Add a rule like so to sudoers (via visudo or similar):
someuser ALL = NOPASSWD: /usr/bin/interesting_program
%somegroup ALL = NOPASSWD: /usr/bin/interesting_program
I find myself automating a lot of my workflow, and running into the same issue. I don't want to punch a hole in my sudoer permissions, and I don't want to run my IDE as root either. A good solution that I've found is gksudo, on Ubuntu and many other Linux variants you'll find it installed by default. What gksudo does is it allows you to prompt the user(yourself) to input your password with a graphic overlay, much like Ubuntu/KDE/etc. do when you need to be root to perform an operation such as an update.
This will then prompt you to provide your password to escalate privilege, then execute a given command/program as root.
In the Edit Tool Window simply:
Set the Program to /usr/bin/gksudo
gksudo may be located at a different path, try: whereis gksudo to find its path
Set Parameters to all commands you want to execute in quotes
Ex. "mongod --fork --config /etc/mongodb.conf; service elasticsearch start"
Make sure you have the quotes!
Set a working directory(if needed)

Resources