Permission with cargo run and sudo

hey gays, i got a problem with my cargo run command.

if i just run cargo run --example go_well --release
the program will panic

warning: `x25` (example "go_well") generated 2 warnings
    Finished release [optimized] target(s) in 3.92s
     Running `target/release/examples/go_well`
error: device open failed: Permission denied
Segmentation fault (core dumped)

then i try to use sudo like this

sudo /home/somewheve/.cargo/bin/cargo  run --example go_well --release

but got

[sudo] password for somewheve: 
error: could not execute process `rustc -vV` (never executed)

Caused by:
  No such file or directory (os error 2)

and i tried cargo build --release first, then cd /target/release/examples/ and run
sudo ./go_well
it works.

but the solution is too complex, could you provide me a better way to process it?

cargo build --release && sudo ./target/release/examples/go_well

0.0

Not rust-centric, but we are here anyways so ..
I would suggest staying away from using sudo to get aroung permissions unless you are really sure you know you are safe and not going to break your system. It seems the go_well binary is requiring permission to access a device and your user does not have that permission. You could perhaps give your normal user permission to access that device and then run as that user. Running stuff as sudo, specially if you are compiling and not quite sure what might happen in a failure can be dangerous. Did you write all the go_well code? If there is a bug in paths or it does something wrong / unexpected, the permissions are there to prevent you from breaking your whole system. You can only break what you give your user permission to break.

2 Likes