大网数据广告活动
服务器运维问答

Use pam_exec to run a script on an ssh login event

· 52CE 我爱测

I'm trying out linuxserver.io openssh-server. Its a lightweight openssh image and makes it easy to configure with public key and user name. If I run it, ssh with public key auth, works as expected: $ docker run --rm -d --name ac1…

问题原文

原帖链接 · doughgle · CC BY-SA 4.0

I'm trying out linuxserver.io openssh-server. Its a lightweight openssh image and makes it easy to configure with public key and user name.

If I run it, ssh with public key auth, works as expected:

$ docker run --rm -d --name ac1 -e PUBLIC_KEY -e USER_NAME=dough lscr.io/linuxserver/openssh-server:latest

Now I want to use pam_exec to run a script on an ssh login event.

Interactively, in the ac1 container, I enable PAM in /etc/ssh/sshd_config:

...
UsePAM yes
KbdInteractiveAuthentication no
...

and restart openssh server:

# s6-svc -r /run/service/openssh-server

But upon ssh from the host machine, the server appears to accept key authentication, but promptly closes the connection before starting a shell session:

$ ssh -vvv -i ~/.ssh/mykey -p 2222 [email omitted]

debug1: Server accepts key: /home/dough/.ssh/mykey RSA SHA256:tUHJFPJXyGdhe2poOnpbgDz1t6mhvpmSiRprMIJdFko explicit agent
debug3: sign_and_send_pubkey: RSA SHA256:tUHJFPJXyGdhe2poOnpbgDz1t6mhvpmSiRprMIJdFko
debug3: sign_and_send_pubkey: signing using rsa-sha2-512 SHA256:tUHJFPJXyGdhe2poOnpbgDz1t6mhvpmSiRprMIJdFko
debug3: send packet: type 50
Connection closed by 172.18.0.3 port 2222

On ac1, in /config/logs/openssh/current, it seems there's a problem with pam account management: "Authentication service cannot retrieve authentication info":

2022-09-22 02:16:41.821095711  debug1: /config/.ssh/authorized_keys:1: matching key found: RSA SHA256:tUHJFPJXyGdhe2poOnpbgDz1t6mhvpmSiRprMIJdFkoM
2022-09-22 02:16:41.821106309  debug1: /config/.ssh/authorized_keys:1: key options: agent-forwarding port-forwarding pty user-rc x11-forwardingM
2022-09-22 02:16:41.821110668  Accepted key RSA SHA256:tUHJFPJXyGdhe2poOnpbgDz1t6mhvpmSiRprMIJdFko found at /config/.ssh/authorized_keys:1M
2022-09-22 02:16:41.821117354  debug1: restore_uid: (unprivileged)M
2022-09-22 02:16:41.821121394  debug3: mm_answer_keyallowed: publickey authentication: RSA key is allowedM
2022-09-22 02:16:41.821133896  debug3: mm_request_send: entering, type 23M
2022-09-22 02:16:41.821202231  debug3: mm_sshkey_verify: entering [preauth]M
2022-09-22 02:16:41.821204241  debug3: mm_request_send: entering, type 24 [preauth]M
2022-09-22 02:16:41.821208932  debug3: mm_request_receive: enteringM
2022-09-22 02:16:41.821215539  debug3: monitor_read: checking request 24M
2022-09-22 02:16:41.821295607  debug3: mm_answer_keyverify: publickey RSA signature verifiedM
2022-09-22 02:16:41.821300496  debug1: auth_activate_options: setting new authentication optionsM
2022-09-22 02:16:41.821302447  debug3: mm_request_send: entering, type 25M
2022-09-22 02:16:41.821310562  debug3: mm_request_receive_expect: entering, type 102M
2022-09-22 02:16:41.821312128  debug3: mm_request_receive: enteringM
2022-09-22 02:16:41.826582283  debug1: do_pam_account: calledM
2022-09-22 02:16:41.826587001  debug2: do_pam_account: auth information in SSH_AUTH_INFO_0M
2022-09-22 02:16:41.831312509  debug3: PAM: do_pam_account pam_acct_mgmt = 9 (Authentication service cannot retrieve authentication info)M
2022-09-22 02:16:41.831316748  debug3: mm_request_send: entering, type 103M
2022-09-22 02:16:41.831321867  Failed publickey for dough from 172.18.0.1 port 40700 ssh2: RSA SHA256:tUHJFPJXyGdhe2poOnpbgDz1t6mhvpmSiRprMIJdFkoM

I've tried creating a minimal /etc/pam.d/sshd.pam with:

account include base-account

but no joy. Whether this file exists or not, the behaviour is the same.

Need some help here...

I'm stuck with the pam configuration to run pam_exec.

But the goal is to to run a hook script on an ssh login event. How can we do that?

已采纳解答

原帖链接 · doughgle · CC BY-SA 4.0

After some experiments, here's what I learned...

  1. Launch openssh server container with PASSWORD_ACCESS=true.
  2. Enable PAM in sshd_config.
  3. Configure PAM to run a hook script when an ssh session opens.
  4. If the hook script needs container environment variables, use #!/usr/bin/with-contenv bash in the shebang line.

You can run a hook script on ssh login with password authentication. For publickey authentication, its also possible, but you'll need to consider the security implications for your context.

Setting PASSWORD_ACCESS to true, enables read only permission on /etc/shadow for the chosen user's group. That ownership change happens on this line of the init script. This fixes the PAM account failure "Authentication service cannot retrieve authentication info" because PAM is able to read /etc/shadow.

This also has the side-effect of enabling password authn which we likely don't want when we use publickey authn.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2022-09-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。