• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Change UNIX user in JAVA

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have wriiten java class to excute on Unix machine.The UNIX user ID is passed through command line arguments to this java class and have to perform all the work as that user which passed thru command line arguments.How to change the UNIX user using user ID in JAVA ?

Please give me any idea to implement this.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balaji Soundarajan wrote:How to change the UNIX user using user ID in JAVA ?

Please give me any idea to implement this.



What do you mean by 'change the unix user id'? Do you mean to say 'Run As <thisUser>' ??
 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,I have to perform all the operations on UNIX in java as the user which is passed thru command line argument.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good aspect. Thanks. I have never come across.

Have been googling around by the time I posted my first reply here .

I have NOT got any satisfied and straight forward results so far.

But to give you a work around, you can first issue a native command of switching user ("su <userEnteredInCommandLine>") via the external process you launched. This would effectively change the user for running the program. Subsequently you can run the java program you wish to run.


Pseudo code :



Does that help?

But I just doubt would there be a continuity between these two attempts!.

Meanwhile, If I get another solution/work around shall post it here. Good luck
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like this is rescue http://expectj.sourceforge.net/. Try it.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not the same thing.

Once you su you're that user until you exit the su shell.

sudo will run a given command on the command line and you'll be the original user when the command has completed.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:That's not the same thing.

Once you su you're that user until you exit the su shell.

sudo will run a given command on the command line and you'll be the original user when the command has completed.



Thats great David. Thanks for the clarification.

However, we are NOT really using the shell here. Right? It is through an external process he is using via/through Java. Hence i doubted on the effect of 'su' for the subsequent commands. Hope I made it clear.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balaji,

Try using this



This -c switch facilitates you to specify a process to execute as a specified user in the 'su' command. It would probably work and give you a relief
 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I execute this statement from Java code?
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to launch it through java.lang.Process instance.

Google it for launching an external/native process via Java. You can get it. If you face any difficulty, please post back. We can help you out.
 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried like p=Runtime.getRuntime().exec("su root") and its not executed.its working for all other commands like ls -a
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're going to use Processes, search for and read the article "When Runtime.exec() won't" on JavaWorld.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balaji Soundarajan wrote:I tried like p=Runtime.getRuntime().exec("su root") and its not executed.its working for all other commands like ls -a



Did you check with "su root -c java YourJavaPgm"? Shall try with the same in my machine and get back!
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:If you're going to use Processes, search for and read the article "When Runtime.exec() won't" on JavaWorld.



Yes, thats correct Rob . There are some pitfalls to it.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just posted an example of how to change the Unix user in the Unix forum. Take a look at the "setuid" topic.
 
Balaji Soundarajan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats good Andrew.I got some idea on that.

I will get user id in the java code form command line argument like

java StorageServer Start -U 100

here 100 is user id and i will get this user id in the java code and have to make him as a owner for all the operations thru JAVA.

Plaese tell me how to achieve this?
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic