I want to create C application to add user in Linux with useradd command .
I write this code when i use it terminal print error message:
useradd :permission denied
useradd :cannot lock/etc/passwd; try again later
this is my C code to add user such as i want to add user1 just write user1
char a[50];
char command[150];
fgets(a,sizeof(a),stdin);
strcpy(command,"useradd ");
strcat(command,a);
how i can solve this problem with correct syntax or command?
if sudo package is not installed, You can install it. Or you can create it as a root using su . It will ask root password after that you can use any superuser caommnads.
This is because you need to run the command with root privileges.
Try something like this, sudo -- useradd
Related
I have a program that I need to run with sudo. I create a directory using mkdir, but this directory has owner and group set to root. That makes sense since I am using sudo. I would like to change the owner and group to the normal user, but I'm not sure how to do that. I thought running system("chown $USER:$USER /directory/") would work, but I suppose since I am in sudo it will just set to root. I was looking into using chown, but I wasn't sure how I was supposed to get the owner and group id. Also it would be good for it to be portable, so I don't want to just hardcode a user/group id.
You're mostly on the right path already, chown is the command you're looking for here.
You can string the two commands to make and then own the directory together using a semicolon.
sudo mkdir test ; sudo chown $USER:$USER test
I've tested this on ubuntu 18.04 and ubuntu 20.04 as that's your tag. The $USER variable resolves to the user that you originally logged in as, not root, as long as you're using it at the beginning of your command like the above. Note that you need to call sudo again when doing the chown portion, the ; ends the sudo elevation.
The coreutils package includes an useful little command, install, you can use instead of mkdir in a sudo context. For example,
sudo install -o USER -g GROUP -m MODE -d DIRECTORY
where USER is the user to own the directory DIRECTORY, GROUP is the group to own the directory, and MODE is the access mode (like chmod) to the directory.
Because system(COMMAND) and popen(COMMAND,...) actually run /bin/sh with -c and COMMAND as parameters, you can use the form
sudo install -o $(id -u) -g $(id -g) -m u=rwx,g=r-x,o=x DIRECTORY
where the shell replaces the user and group names (or rather, numbers, since I'm not using the -n option) before executing sudo. (The id command is also included in coreutils, so you can definitely expect both install and id to be available on all full-blown Linux machines; and even on most embedded systems. It is what all package managers et cetera use to install files, you see.)
Above, I used the mode u=rwx,g=r-x,o=x (equivalently, 0751) as an example; it sets the mode to rwxr-x--x, i.e. grants access to everybody, with owner user and group being able to list the directory contents, and only the owner user being able to create new files or directories in it.
I added a user to run a specific service using the command:
sudo useradd -r -s /bin/false userName
I then try to run the service manually (after changing ownership of the service) by using:
su userName -c "./path/to/service"
Then I get a password prompt for which I do not know the password. I thought that system users weren't supposed to have passwords. What am I doing wrong here?
Trying to run yo angular:app myWebApp shows me following Error:
What should I do to overcome the permission error?
Use sudo with your command. Like
sudo yo angular:app myWebApp
. If it asks for your password enter it and it will work.
I write a pam module that is called when user execute the sudo command. I have already got the username, password, tty, but now, I have to get the user command to encrypt.
If user exec
sudo ls -l
I have to get ls -l
I have no pam app for my module. (So I use only pam_modules.h) and my module is called first in /etc/pam.d/sudo
I thought about using the netlink socket like pam_tty_audit, but I don't thinks this is the best method (or the most simple)
Have you any idea for getting the user command ?
Thank you and sorry for my bad english
According to the API documentation of sudo's authentication mechanisms, the command is not passed to the authentication backend, so PAM is the wrong place to do whatever you are trying to do. Consider writing a sudo plugin instead.
I created a new non-sudo user(user1) in vagrant(Ubuntu 12.04 OS), and added the insecure public key to the user1 authorised key file. In vagrant file, added the default user as "user1" :
config.ssh.default.username = "user1"
Now vagrant up is failing with following error message:
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!
mkdir -p /vagrant
Stdout from the command:
Stderr from the command:
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: no tty present and no askpass program specified
Sorry, try again.
sudo: 3 incorrect password attempts
But if am setting the sudo user for default user, then vagrant up is successful.
Can anyone help me with the changes I need to do to enable vagrant up for non-sudo users.
Vagrant requires root/sudo permissions on the VM for almost all of it's operations; like configuring the networking, mounting shared folders, running provisioners, etc. So you wouldn't get very useful VM without sudo even if you managed to avoid it.
Note that you only need sudo access on the guest. Vagrant commands itself can (and should) be run as a non-root user on the host.