2

After downloading netbeans 10.0 I taught of creating a desktop file to launch it, So, I don't have to run sudo /home/abc/Downloads/netbeans/bin/netbeans every time.

I created small script to launch netbeans in admin priviliges.

#!/bin/sh
cd /home/abc/Downloads/netbeans/bin/ 
echo "password" | sudo -S ./netbeans

I put this file to /usr/bin/ and

created netbeans.desktop file as follows

[Desktop Entry]
Version=1.0
Type=Application
Terminal=False
Exec=/usr/bin/netbeans
Name=Netbeans
Comment=Java IDE
Icon=/home/abc/Downloads/netbeans/nb/netbeans.icns

by typing

netbeans in terminal it opens netbeans in admin privileges.

But clicking the desktop file or shell file doesn't respond anything

Sorry if it is silly I am a Newbie.

EDIT

Tried everything stated here => How do I run a 'sudo' command inside a script? but nothing worked.

As @dessert suggested to use pkexec. It works but gives another problem.

/home/abc/Downloads/netbeans/platform/lib/nbexec: WARNING: environment variable DISPLAY is not set

I think netbeans needs terminal to run. and so I tried xdf-open and gksu but non of them worked. Please help!

  • 2
    Possible duplicate of [How do I run a 'sudo' command inside a script?](https://askubuntu.com/questions/425754/how-do-i-run-a-sudo-command-inside-a-script) – dessert Mar 24 '19 at 11:06
  • You can use `pkexec` to get a GUI password prompt (`Exec=pkexec /usr/bin/netbeans`), see [How can I run an application with a GUI as admin from a non-admin user session?](https://askubuntu.com/q/164819/507051). – dessert Mar 24 '19 at 11:11
  • 1
    1) Do you really need to launch a code editor as root? 2) The gui with its menuing system is not really designed to launch applications as another user or as root. – vanadium Mar 24 '19 at 11:36
  • @vanadium Yes! I think i need to run netbeans in root else it displays error https://imgur.com/a/6u0uz1r. This can be over-comed by changing netbeans directory. but I think i can learn something new from this question. changing netbeans directory is is just passable – Khushit shah Mar 24 '19 at 11:51
  • updated image link : https://imgur.com/a/kj648EH – Khushit shah Mar 24 '19 at 11:59
  • Check the folder mentioned in the error message: see that it exists and has the correct owner and permissions set. It is very bad practice to launch a program as root in order to mask configuration errors. – vanadium Mar 24 '19 at 12:01
  • @vanadium Well changing the userdir and chachedir of netbeans solves the problem. but just sake of curiousness It would be great to known solution of the question! – Khushit shah Mar 24 '19 at 12:16

1 Answers1

1

The large majority of user applications should never be run as root. If root permissions are required, it would be very bad practice to allow user to run applications with root permissions just by clicking a launcher without any form of authentication.

Fortunately, it is not easy to run scripts outside of the terminal with admin privileges. The GUI menu system does not allow to easily create launchers that launch programs with root permissions without any security measures.

In rare cases, there is a need to have graphical user interfaces perform administrative actions. Previously, the gksudo command allowed a user with root privileges to launch just any graphical application with root privileges. This poor security model has been depreciated in favor of more secure systems that either 1) do not require that a graphical user interface is run as root at all, but still allow for access to system files and folders, or 2) still run the entire graphical application as root, but have a stronger security in place.

1) is covered by the "admin://" URI. Applications such as gedit and nautilus can be started with such an URI, e.g. as in

gedit admin:///etc/fstab

With this approach, gedit itself just runs as the normal user. The system provides it with a temporary copy of the system file, in this example /etc/fstab. Once the user saves the temporary copy, the system updates the original /etc/fstab. Also "Disks" is designed this way. The application, i.e., the graphical user interface, runs as normal user anytime, but requests the administrator password at a point where an administrative task is about to be performed.

2) is handled through pkexec. Here, after authentication, the entire application is started with root permissions. This is also how gksudo used to work. The big difference, however, is that, additionally, a system policy file about the application must be in place. Thus, only previously authorized applications can be started as root by the privileged user.

vanadium
  • 82,909
  • 6
  • 116
  • 186