0

My server is debian 8 (jessi) , when i restart networking using following java code then i get warning "Faild to restart Networking.service: Access denied."

 try
    {            
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("systemctl restart networking");
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<ERROR>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
      } catch (Throwable t)
      {
        t.printStackTrace();
      }

Can any one solve it ? Thanks in Advance.

Sushil Kumar
  • 84
  • 1
  • 10
  • 1
    You didn't run the code as `root`. Try adding `sudo` to the command, or run the Java code as `root`. --- I bet you cannot run command `systemctl restart networking` directly either. Perhaps you should have tried that before asking here. – Andreas Sep 20 '18 at 20:37
  • 1
    Thanks to reply, when i am try here `sudo systemctl restart networking` then it gives again a warning `sudo: no tty present and no askpass program specified`. can you write any reference/demo code related to it ? – Sushil Kumar Sep 20 '18 at 20:45
  • 1
    @Sushil Check this: https://stackoverflow.com/questions/21659637/how-to-fix-sudo-no-tty-present-and-no-askpass-program-specified-error – TMS Sep 20 '18 at 22:23
  • 2
    @TMS thanks it helped me well. i completed it. – Sushil Kumar Sep 20 '18 at 23:03

1 Answers1

0

Ohh, finally i get the solution, here i changed in my nano /etc/sudoers and change after the line,

    ` %sudo ALL=(ALL:ALL) ALL`
`sushil ALL=(ALL) NOPASSWD: ALL`

here "sushil" is my system user name.

Sushil Kumar
  • 84
  • 1
  • 10