Mandar K
Mandar K

Reputation: 405

Execute a unix program from a Java class using java.lang.Process with a specific user

I am trying to run a unix command from Java using java.lang.Process class. How can we pass a specific user so that the command executes with the permissions of that specific user ? I don't want to execute the command with a superuser.

Upvotes: 0

Views: 224

Answers (3)

Angelo Fuchs
Angelo Fuchs

Reputation: 9941

You could start a login command first and then input credentials into it.

I would guess that starting su - [USER] should do the trick.

After reading through apropos user I came across the pkexec command which should do what you need.

Upvotes: 1

MrSimpleMind
MrSimpleMind

Reputation: 8597

You could use a ssh session, that logs with the user you know its password and run a specific command.

E.g. using java shell JSch

or

Expect4J

or

Ganymed-ssh-2

I found a good thread here by the way: Jsch or SSHJ or Ganymed SSH-2?

Upvotes: 2

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136022

You can run a new shell and execute commands which will switch to another user and then run the programm

Upvotes: 0

Related Questions