Check linux user and password (authenticate)


what is the best and securest way in go to check where the password for an linux user is valid.

Best regards Jens

Does not exist (i think) a go way to check if a password is valid because this is a mater of Linux not Go language. Anyway, you could login with a password and this can be executed from Go as Linux command. You can do this using a local command like sudo -u user -i (or other similar commands) or through ssh command, in both cases having the passing password problem :wink:
Better is to use login with sshbecause is more portable and work local and remote. For this you need a ssh server active on the Linux machine. See in this project how to ssh work and pass the password.

You probably want to talk to PAM. looks somewhat promising.


Here a solution with the PAM library:

package main

import (


func PAMAuth(serviceName, userName, passwd string) error {
	t, err := pam.StartFunc(serviceName, userName, func(s pam.Style, msg string) (string, error) {
		switch s {
		case pam.PromptEchoOff:
			return passwd, nil
		case pam.PromptEchoOn, pam.ErrorMsg, pam.TextInfo:
			return "", nil
		return "", errors.New("Unrecognized PAM message style")

	if err != nil {
		return err

	if err = t.Authenticate(0); err != nil {
		return err

	return nil

func main() {
	err := PAMAuth("passwd", "user", "pass")
	if err != nil {
	} else {

Thank you


I try to build this and it bombs thusly:

…/…/ fatal error: security/pam_appl.h: No such file or directory
//#include <security/pam_appl.h>

| Nemmind … libpam0g-dev … apt is your friend :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.