GNU/Linux >> Linux の 問題 >  >> Linux

Linux でユーザーのホーム ディレクトリにあるファイルをデフォルトに復元する方法

問題

ユーザーのホーム ディレクトリにあるファイルのコンテキストまたはファイル許可が誤って変更された場合、このユーザー ログイン システムの後に許可エラーまたは予期しないアプリケーションの動作が発生する可能性があります。

たとえば、/home/user1/.bash_profile のファイル許可が間違っている場合、user1 にログインすると、「/home/user1/.bash_profile:許可が拒否されました」というプロンプトが表示されます:

login as: user1
user1@geeklab's password:
Last login: Mon Dec 15 15:08:20 2014 from geeklab2.example.com
-bash: /home/user1/.bash_profile: Permission denied
-bash-3.2$

この投稿では、ユーザーのホーム ディレクトリにあるファイル/サブディレクトリをデフォルトに復元する方法を説明します。

解決策

ユーザーのホーム ディレクトリをデフォルトに復元するために必要な 2 つの重要なファイル/ディレクトリがあります。主に次のとおりです。
1. /etc/skel ディレクトリ
2. /etc/default/useradd

skel ディレクトリ

ディレクトリ /etc/skel/ (skel は「skeleton」から派生したものです) は、ユーザーが最初に作成されたときにホーム ディレクトリを開始するために使用されます。 「スケルトン」ユーザー ファイルのサンプル レイアウト:

# ls -lart /etc/skel
total 32
drwxr-xr-x    4 root root  4096 Feb  4  2016 .mozilla
-rw-r--r--    1 root root   124 Feb 15  2017 .bashrc
-rw-r--r--    1 root root   176 Feb 15  2017 .bash_profile
-rw-r--r--    1 root root    18 Feb 15  2017 .bash_logout
drwxr-xr-x.   3 root root  4096 Aug 22  2017 .
drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..
注意 :「skeleton」ディレクトリは /etc/default/useradd ファイルに定義されています。
# cat /etc/default/useradd
# useradd defaults file
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=yes

ホーム ディレクトリにあるファイルを復元する

1. たとえば、以下に示すように、.bash_profile ファイルがユーザーのホーム ディレクトリから削除された場合。

$ rm ~/.bash_profile
# ls -lart /etc/skel
total 32
drwxr-xr-x    4 root root  4096 Feb  4  2016 .mozilla
-rw-r--r--    1 root root   124 Feb 15  2017 .bashrc
-rw-r--r--    1 root root    18 Feb 15  2017 .bash_logout
drwxr-xr-x.   3 root root  4096 Aug 22  2017 .
drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..

2. 元の .bash_profile ファイルを復元するには、「skeleton」ディレクトリからデフォルト ファイルをコピーします。

$ cp /etc/skel/.bash_profile ~/
# ls -lart ~/
total 32
drwxr-xr-x    4 root root  4096 Feb  4  2016 .mozilla
-rw-r--r--    1 root root   124 Feb 15  2017 .bashrc
-rw-r--r--    1 root root   176 Feb 15  2017 .bash_profile
-rw-r--r--    1 root root    18 Feb 15  2017 .bash_logout
drwxr-xr-x.   3 root root  4096 Aug 22  2017 .
drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..
注意 処置:対応するユーザー権限でファイルをコピーしてください。 root ユーザー経由でファイルをコピーした場合は、後で所有権とファイル許可を手動で変更する必要があります。

ホーム ディレクトリの下にサブ ディレクトリを復元する

たとえば、サブディレクトリ .mozilla を復元するには、–recursive (-r) オプションを付けてコピーします:

$ cp -r /etc/skel/.mozilla/ ~/

ホーム ディレクトリ全体を最初から復元する

ユーザーのホーム ディレクトリ全体を復元する方法を見てみましょう。この例では、user1 のホーム ディレクトリを削除します。
1.ユーザーの UID と GID を確認してください:

$ id user1
uid=54324(user1) gid=54325(user1) groups=54325(user1)

2. ユーザーのホーム ディレクトリとユーザーを root 権限で削除します。

# rm -rf /home/user1

3. ユーザーのホーム ディレクトリにある /etc/skel ディレクトリからすべてのファイルをコピーします。

# cp -r /etc/skel/* ~/
# ls -lart /home/user1/
total 32
drwxr-xr-x    4 root root  4096 Feb  4  2016 .mozilla
-rw-r--r--    1 root root   124 Feb 15  2017 .bashrc
-rw-r--r--    1 root root   176 Feb 15  2017 .bash_profile
-rw-r--r--    1 root root    18 Feb 15  2017 .bash_logout
drwxr-xr-x.   3 root root  4096 Aug 22  2017 .
drwxr-xr-x. 112 root root 12288 Feb 26 03:09 ..


Linux
  1. Linuxでカスタムホームディレクトリを使用してユーザーを作成する方法

  2. Linux –ディレクトリ内のすべてのフォルダ/ファイルにデフォルトのファイル権限を設定するにはどうすればよいですか?

  3. / varの下にあるすべてのファイルのデフォルトのグループ/ユーザー所有権を復元するにはどうすればよいですか?

  1. Linux で FTP を使用してファイルを転送する方法

  2. Linuxのユーザーホームディレクトリの下にある.bash_profileファイルの目的は何ですか

  3. Linux で特定のディレクトリからファイルを削除しているユーザーを特定する方法

  1. Linuxで削除された/tmpディレクトリを復元する方法

  2. LinuxまたはUNIXでユーザーのホームディレクトリを見つける方法は?

  3. Linux で別のユーザーのホーム ディレクトリからファイル/フォルダをコピーするにはどうすればよいですか?