Rinalds Gudriks Rinalds Gudriks - 9 months ago 79
Linux Question

change any linux user password using c++

I want to change any Linux user password using C++, any solution would be great.

I need to do it manually, that mean by opening files (Can't use system()):

  • /etc/passwd

  • /etc/shadow

  • /etc/group

Also seems I need to lock these files. Have c code sample for opening .txt file and reading content, but can't read large content, that is problem too.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <stdlib.h>
#include <string>

const char* path = "/tmp/log.txt";

void read_file()
int fd;
char buf[1000];
int i;

fd = open(path, O_RDONLY);
flock(fd, LOCK_SH);

for(i=0; i < 255 ; i++) {
read(fd, &buf[i], 1);
usleep(10 * 10); // 10 ms

flock(fd, LOCK_UN);

printf("reader: %#x: %s\n", getpid(), buf);
usleep(10 * 10); // 10 ms

void reader()

int main()
return 0;

In ideal solution it must work like that:

  1. Program ask to enter username.

  2. Program ask to enter new password.

  3. Open necessary file, change password and save file, but not corrupt it.

Can someone explain with which file I need to work to change password and how to open it properly to not hurt the system? How to edit exactly that line where is password?

At the moment i'm working on Ubuntu 16.04.


I saw in comments that normal user cant access these files. And one comment was to give permissions to every user change any user password.
So what about if one user have permissions to edit these files and change password to any user. So is it way to safe open and edit system file
, save and close it. I need to lock it somehow or how I can access it?

There is
file structure, as all can see there is special hash or crypt for password, so if change it properly and save file, password must change.

My code to get user data using username:

#include <stdio.h>
#include <pwd.h>
#include <iostream>

using namespace std;

int main()
struct passwd *p_entry1, *p_entry2;
char* username;

cout<<"Insert username: ";
/* Find user data by entered username */
p_entry1 = getpwnam(username);

printf("username = %s = %s"
" userid = %d group id = %d"
" real name = %s directory = %s"
" primary shell = %s",
p_entry1->pw_name, p_entry1->pw_passwd,
p_entry1->pw_uid, p_entry1->pw_gid,
p_entry1->pw_gecos, p_entry1->pw_dir,



All is possible in this way -> to make c++ code which change password I done this:

  • Password must be encrypted, so I used code to make it in $6$:SHA-512 hash.
  • Must open /etc/shadow file, read it, find user by username and change password. There you can read about shadow file structure.

To open file used fstream, to find and change line I used std::getline and std::vector.

To run .cpp file, need to be root in terminal, run g++ fileName.cpp -o excqFileName -lcrypt and ./excqFileName commands. Thats it's for me.

That all folks, I did my task and it's possible. Will not share code, but I explained enough to understand how it works.