This can be done using the roslaunch launch-prefix
option in the node tag. For example, if you user has passwordless sudo enabled, you could try:
<node pkg="my_pkg" type="my_node" name="my_super_node" launch-prefix="sudo"/>
If you don't want to set up passwordless sudo, and your node doesn't change very often, you could try setting the setuid bit on it, so that it runs as root even if started by a non-root user. The only trouble with this is that you'll have to reset the permissions whenever your node changes, and it generally won't be deployable through debs, and it will only work for compiled nodes; not for scripts.
cd devel/lib/my_pkg # cd to the directory with your node
chown root:root my_node # change ownship to root
chmod a+rx my_node # set as executable by all
chmod u+s my_node # set the setuid bit
Note that both of these methods will unset LD_LIBRARY_PATH before executing your node (it's a security feature of glibc). This means that your node will either need to be statically compiled, or you'll need to turn on RPATH during the compilation process, so that the library search path is included in your executable rather than relying on the environment to be set up correctly.
Originally posted by ahendrix with karma: 47576 on 2014-05-15
This answer was ACCEPTED on the original site
Post score: 18
Original comments
Comment by Hendrik Wiese on 2014-05-16:
Sounds promising. Is it possible to use passwordless sudo even with a Python node? I'm okay with passwordless sudo.
Comment by ahendrix on 2014-05-16:
Yes, I think passwordless sudo with a python node should work. You may want to check that sudo is configured to pass the PYTHONPATH variable.
Comment by Wedigo on 2015-07-31:
I know this old question, but I got stuck on that issue for some days.
Could someone please be more specific how this is solved, especially concerning the staical compilation and turning on the RPATH?
Comment by ahendrix on 2015-08-08:
Have a look at https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
Comment by Wedigo on 2015-08-10:
Thank you for the link.
I found a solution before you posted this. I added the needed environment variables to the sudo prefix in the .launch file. It works for now....
Comment by suforeman on 2017-01-20:
if using sudo
as the launch-prefix, it may be necessary to use sudo -E
to preserve the ROS environment variables.
Comment by Blupon on 2017-08-02:
hi ! how can I do the reverse of the command line 4 instructions set ? This broke my node and I don't understand the following documentation well enough to do the compilation part of the fix. Between, I'm trying to solve this
Comment by ahendrix on 2017-08-02:
You can remove the setuid bit with sudo chmod u-s my_node
Comment by muttidrhummer on 2019-01-18:
i noticed that when i close the launchfile in which i ran some nodes with sudo , the sudo nodes won't close at at all, any idea?