0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

macOSでPEMキーを移動する際の「Operation not permitted」エラーの解決方法

Posted at

はじめに

macOSでPEMキーファイルをDownloadsフォルダから~/.sshディレクトリに移動しようとした際に、「Operation not permitted」エラーが発生することがあります。この記事では、この問題の原因と解決方法について解説します。

問題の状況

以下のような状況でエラーが発生しました:

user@Mac ~ % cd Downloads 
user@Mac Downloads % ls
ls: .: Operation not permitted

user@Mac Downloads % mv ~/Downloads/example-key.pem ~/.ssh/
mv: rename /Users/user/Downloads/example-key.pem to /Users/user/.ssh/example-key.pem: Operation not permitted

user@Mac Downloads % cp ~/Downloads/example-key.pem ~/.ssh/
cp: /Users/user/Downloads/example-key.pem: Operation not permitted

原因

このエラーの主な原因は、macOSのセキュリティ機能です:

  • System Integrity Protection (SIP)
  • Privacy & Security設定
  • フルディスクアクセス権限

特にDownloadsフォルダへのアクセスが制限されている場合が多いです。

解決方法

方法1: Finderを使用した移動(推奨)

最も確実で簡単な方法です:

  1. Finderを開く

    • DockのFinderアイコンをクリック
  2. Downloadsフォルダを開く

    • サイドバーの「ダウンロード」をクリック
  3. PEMファイルを探してコピー

    • 対象のPEMファイルを右クリック → 「コピー」
  4. ~/.sshフォルダに移動

    • Finder上で Cmd + Shift + G を押す
    • ~/.ssh と入力して「移動」をクリック
  5. ファイルを貼り付け

    • Cmd + V で貼り付け
  6. ターミナルで権限設定

    chmod 600 ~/.ssh/example-key.pem
    ls -la ~/.ssh/example-key.pem
    

方法2: Terminalに権限を付与

根本的な解決のため、Terminalにフルディスクアクセス権限を与える方法:

  1. システム設定を開く

    • Apple メニュー > システム設定
  2. プライバシーとセキュリティ

    • 左サイドバーから「プライバシーとセキュリティ」をクリック
  3. フルディスクアクセス

    • 「フルディスクアクセス」をクリック
    • 鍵アイコンをクリックしてロックを解除(パスワード入力)
  4. Terminalを追加

    • 「+」ボタンをクリック
    • アプリケーション > ユーティリティ > Terminal を選択
    • 「開く」をクリック
  5. Terminalを再起動

    • Terminalを完全に終了して再起動

権限付与後、以下のコマンドが実行できるようになります:

cp ~/Downloads/example-key.pem ~/.ssh/
chmod 600 ~/.ssh/example-key.pem

方法3: sudoを使用(非推奨)

sudo mv ~/Downloads/example-key.pem ~/.ssh/
sudo chown $(whoami):$(whoami) ~/.ssh/example-key.pem
chmod 600 ~/.ssh/example-key.pem

注意: この方法は権限の問題を一時的に回避するだけで、根本的な解決にはなりません。

成功確認

移動が成功すると、以下のような出力が表示されます:

user@Mac .ssh % ls -la ~/.ssh/example-key.pem
-rw-------@ 1 user  staff  1678  8  5 21:02 /Users/user/.ssh/example-key.pem

重要なポイント:

  • 権限が 600 (-rw-------) になっていること
  • 所有者のみ読み書き可能であること

PEMキーの使用方法

移動完了後、SSH接続時に以下のように使用できます:

# 直接指定
ssh -i ~/.ssh/example-key.pem username@hostname

# ~/.ssh/configに設定を追加
Host myserver
    HostName xxx.xxx.xxx.xxx
    User ec2-user
    IdentityFile ~/.ssh/example-key.pem

まとめ

macOSでPEMキーファイルを移動する際の「Operation not permitted」エラーは、セキュリティ機能による制限が原因です。Finderを使用した移動が最も確実で簡単な解決方法です。

根本的な解決を望む場合は、Terminalにフルディスクアクセス権限を付与することをお勧めします。

参考


この記事が同様の問題に遭遇した方の参考になれば幸いです!

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?