How Can My C/C++ Application Determine If the Root User Is Executing the Command

How can my C/C++ application determine if the root user is executing the command?

getuid or geteuid would be the obvious choices.

getuid checks the credentials of the actual user.

The added e in geteuid stands for effective. It checks the effective credentials.

Just for example, if you use sudo to run a program as root (superuser), your actual credentials are still your own account, but your effective credentials are those of the root account (or a member of the wheel group, etc.)

For example, consider code like this:

#include <unistd.h>
#include <iostream>

int main() {
auto me = getuid();
auto myprivs = geteuid();


if (me == myprivs)
std::cout << "Running as self\n";
else
std::cout << "Running as somebody else\n";
}

If you run this normally, getuid() and geteuid() will return the same value, so it'll say "running as self". If you do sudo ./a.out instead, getuid() will still return your user ID, but geteuid() will return the credentials for root or wheel, so it'll say "Running as somebody else".

Check if Qt c++ application is running as sudo

I would use getuid or geteuid.

Here is a previously answered question similar to yours.

Rails 4: Rakefile and Gemfile not found as Root User

Since you installed rvm as another user, all your gems and such will be placed in that user's home directory, as will the actual version of ruby you're using. In order to get everything working as root, you'd either need to reinstall rvm system-wide and set things up that way, or you could try getting RVM running as root.

When you install rvm, it adds the following lines to the user's ~/.bash_profile, so you could try adding this to root's:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting

Here, you'd need to substitute $HOME for /home/username, where username is the user that has rvm installed. Otherwise, it would default to the home directory of the currently logged in user, which would be /root.

Another idea, you say you lost access to the computer you could log in as that user from. But, if you can log in as root, you can change anything in the system. Assuming it's an ssh key issue, why not just add your current computer's ~/.ssh/id_rsa.pub to the user's ~/.ssh/authorized_keys as root, and then log in and execute commands as that user as you originally intended?

How to run 'sudo' command in windows

There is no sudo command in Windows. The nearest equivalent is "run as administrator."

You can do this using the runas command with an administrator trust-level, or by right-clicking the program in the UI and choosing "run as administrator."

Java' is not recognized as an internal or external command

You need to configure your environment variables, JAVA_HOME and PATH.

JAVA_HOME must contain the path to java, and you should add %JAVA_HOME%\bin to PATH

Alternatively, you can simply add to your PATH the whole path to the bin folder, without the JAVA_HOME variable, however, this makes a little more annoying when you need to have more than one java version on your machine (that way you only need to change JAVA_HOME and don't even bother with PATH)

Running a command as Administrator using PowerShell?

If the current console is not elevated and the operation you're trying to do requires elevated privileges then you can start powershell with the Run as Administrator option :

PS> Start-Process powershell -Verb runAs

Microsoft Docs: Start-Process



Related Topics



Leave a reply



Submit