2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

macOSでsudoをTouch IDで許可するためのスクリプトをGistからワンライナーで動かす

Last updated at Posted at 2021-11-22

概要

MacBook AirやMacBook Proで、Touch IDが搭載されたモデルにおいて、ターミナルでsudoコマンドを使うときにTouch IDで許可できるようにするためのスクリプトです。

Qiitaの他の記事でも、いくつかあることを見たのですが自分のケースに合わなかったことと、複数回実行するケースもありそう(複数のmac、アップデート後、など)なのでスクリプトにしました。

書いたものとできること

書いたもの

Gistに置いてあるものをこちらにもコピペしておきます。

# !/bin/sh
# A shell script to enable sudo by "Touch ID" on macOS
# tested on macOS 11.6 Big Sur
TARGET_FILE=/etc/pam.d/sudo 

# skip if it is not run on macOS
if [ 'Darwin' != `uname` ]; then
	echo "not on macOS platform. exit."
	exit 0
fi

# check target file
grep "auth       sufficient     pam_tid.so" $TARGET_FILE > /dev/null 

if [ $? -ne 1 ]; then
	echo "already has pam_tid.so line in ${TARGET_FILE}. do nothing."
	exit 0
fi

# edit file with sed command. 
# with -i option, sed creates backup file on the same directory.

sudo chmod +w ${TARGET_FILE}
sudo sed -i '.bak' -e '2 i\
auth       sufficient     pam_tid.so' ${TARGET_FILE}
if [ $? -ne 0 ]; then
	echo "an error occured. exit."
    exit 0
fi

sudo chmod -w ${TARGET_FILE}

echo "succeeds. see ${TARGET_FILE}."

cat ${TARGET_FILE}

できること

  • /etc/pam.d/sudo ファイルの2行目に、Touch IDで認証するため、「auth sufficient pam_tid.so」という行を追加します。

動作前提

/etc/pam.d/sudo ファイルが、以下のような構成になっていることを前提としています。

# sudo: auth account password session
auth       sufficient     pam_smartcard.so
auth       required       pam_opendirectory.so
account    required       pam_permit.so
password   required       pam_deny.so
session    required       pam_permit.so

試した環境

macOS Big Sur で書きました。
(2021/11/25追記: macOS Monmtereyでも動作することを確認しました。)

uname -a
Darwin MacBook-Air.local 20.6.0 Darwin Kernel Version 20.6.0: Mon Aug 30 06:12:20 PDT 2021; root:xnu-7195.141.6~3/RELEASE_ARM64_T8101 arm64

動かし方

方法1:上記スクリプトを手元に保存して実行

  1. 上記ファイルをご自身のmacOSで好みのファイル名をつけて保存します。
  2. ファイルに実行権限をつけるか、"sh ファイル名" で実行します。

方法2:Gistからスクリプトをダウンロードして実行

  1. まず、下記のワンライナーで指定されているGistファイルの内容を確かめ、実行してもよいか考える。
  2. 実行して良いと判断した場合、下記のワンライナーを実行する。(sudoコマンドを内部で使っているので、パスワードなりで許可してあげる)
curl https://gist.githubusercontent.com/hrkt/1bbac50c84d99fdb07375d713587f864/raw/658ffd698a0d7552d160c9c5651757a96e874bc1/enable_sudo_by_touch_id.sh | sh 

追記

2021/11/25

自分のMacをBig SurからMontereyにアップグレードで/etc/pam.d/sudoのからpam_tid.soの行がなくなっていたケースで、上記方法2.の実行でまたsudoにてTouch IDが使えるようになることを確認しました。

他の記事

検索して出てきた順にいくつか。~~~(を自分で書いちゃったあとに調べました)~~~

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?