Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

99
Visualizações
How to change a users password in Linux using C#

Following scenario:

  • I do have sudo permissions
  • I know the old and new password of a user

Now I want to write a C# .NET 6 application which can change the password of the user. In Linux I would just call

sudo passwd username

My first idea was to create a new process, redirect the stdin and write the password there.
Another option would be to create a bash script and call it from C# (also using a new process).

However, both seem a little bit hacky to me. Is there any option to change the password in-process in C#?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Something along these lines should work properly if you already have the linux shell script.

   if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)){    
        var ShellScriptPath =  $"-c '{ShellScriptPath } &>> {OutputTextFilePath}'";
        var fileInfo = new FileInfo(ShellScriptPath);
        var startInfo = new ProcessStartInfo
        {
           CreateNoWindow = true,
           RedirectStandardOutput = true,
           RedirectStandardError = true,
           UseShellExecute = false,
           FileName = "/bin/bash",
           WindowStyle = ProcessWindowStyle.Hidden,
           Arguments = fileInfo.FullName,
           WorkingDirectory = WorkingDirectoryPath,
        };
        
        using Process process = Process.Start(startInfo);
        process.WaitForExit();
        process.Dispose();
    }
over 4 years ago · Santiago Trujillo Relatório

0

Program.cs

class PasswordChanger
{
  static void Main(string[] args)
  {
    string password = args[1];
    string user = args[0];
    Console.WriteLine(ShellHelper.Bash($"echo -e \"{password}\n{password}\n\" | sudo passwd {user}"));
  }
}

LinuxHelper.cs

using System;
using System.Diagnostics;

namespace LinuxHelper
{
  public static class ShellHelper
  {
    public static string Bash(string cmd)
    {
      string escapedArgs = cmd.Replace("\"", "\\\"");

      Process process = new Process()
      {
        StartInfo = new ProcessStartInfo
        {
          FileName = "/bin/bash",
          Arguments = $"-c \"{escapedArgs}\"",
          RedirectStandardOutput = true,
          UseShellExecute = false,
          CreateNoWindow = true,
        }
      };

      process.Start();
      string result = process.StandardOutput.ReadToEnd();
      process.WaitForExit();

      return result;
    }
  }
}

The above code works, takes two args first is the username , second is the password. I have separated the solution into two files because the LinuxHelper/Shellhelper can be used for any shell command you need to call. The main function/method can also be easily modified to be a standard method in any project. If you do change the Main method to be a method inside a class, I suggest you change (string[] args) to (string user, string password) then you can remove the string declarations inside the method.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda