sudo認証 pam_ssh_agent_auth ssh-agent

Many systems are configured with NOPASSWD in sudo. This is poor security, all an attacker needs is the context of the user. You'd do well to remove all NOPASSWD: strings from your sudo configuration (use sudo visudo and don't forget sudoers.d/*!)

Make sure you set sufficiently random passwords for all users, or use the ssh-agent method described below. You're already using keys to authenticate in SSH, right?!?
Secure sudo without password | brnrd - Bernard Spil 2021

sudoをパスワードレス認証で使いたい | IIJ Engineers Blog 2025

/etc/sudoers
Defaults        env_keep += "SSH_AUTH_SOCK"

の追加が必要だった。

ssh接続してるんだから、sudo認証にパスワードではなくssh公開鍵認証を使う #Linux - Qiita 2021

サーバ

sudo dnf install pam_ssh_agent_auth

sudo vi /etc/pam.d/sudo

#%PAM-1.0
auth sufficient pam_ssh_agent_auth.so file=~/.ssh/authorized_keys # 追加
auth       include      system-auth

system-authの前に追加

sudo visudo

# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults    env_keep += "SSH_AUTH_SOCK" # 追加

クライアント

$ eval `ssh-agent`
$ ssh-add ~/.ssh/id_ed25519
(確認)
$ ssh-add -l

$ ssh -A example.com

-A: エージェント転送を許可する

$ sudo ls
$ exit
(ssh-agent終了)
$ ssh-agent -k

【SSH】ssh-agentの使い方を整理する #SSH - Qiita 2016

ansible

ansible.cfg
[ssh_connection]
ssh_args =-o ForwardAgent=yes

become_flags=--preserve-env=SSH_AUTH_SOCK

In your case, your SSH_AUTH_SOCK environment variable is preserved in your interactive session, but not in Ansible’s non-interactive session. Passing become_flags=--preserve-env=SSH_AUTH_SOCK to become_method=sudo then instructs sudo that it needs to preserve this ENV for the session, when it normally wouldn’t by default.

https://forum.ansible.com/t/missing-sudo-password-when-using-agent-based-authentication-for-sudo/8092/6

AnsiblePlaybookでssh-agentを使ってPrivateRepositoryをCloneする #Ansible - Qiita 2022