ターミナルで既存ユーザーのデフォルトホームディレクトリを作成

どのようにすればよいのでしょうか? ホームディレクトリのないユーザーを作成しましたが、今度はホームディレクトリを作成したいのです。new_userというフォルダだけではなく、デフォルトのホームディレクトリとその中のフォルダや隠しファイルなどをすべて作成したいのです。

サブディレクトリ(ドキュメント、ダウンロードなど...)は、ホームディレクトリが正しいパーミッションで作成されていれば、ユーザがGNOMEから初めてログインしたときに自動的に作成されます。ここではそのデモをご紹介します。

alaa@aa-lu:~$ sudo useradd testinguser
alaaa@aa-lu:~$ sudo passwd testinguser
新しいUNIXパスワードを入力してください。
Retype new UNIX password:
passwd: password updated successfully
alaa@aa-lu:~$ sudo ls -l /home
合計 20
drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa
drwx------ 2 root root 16384 Jun 5 09:46 lost+found
sudo mkdir /home/testinguser
alaaa@aa-lu:~$ sudo chown testinguser:testinguser /home/testinguser
ls -l /home
total 24
drwxr-xr-x 55 alaa alaa 4096 Aug 22 22:00 alaa
drwx------ 2 root root 16384 Jun 5 09:46 lost+found
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:03 testinguser
alaaa@aa-lu:~$ ls -l /home/testinguser/
合計 0
alaaa@aa-lu:~$ ls -l /home/testinguser/ total 0;

ユーザー'のホームディレクトリが正しく設定されているかどうかは、/etc/passwdのエントリをチェックすることで確認できます。デフォルトでは、ホームディレクトリが /home/testinguser に設定されているはずです。

と表示されます。 alt;b>alaa@aa-lu:~$ grep testinguser /etc/passwd testinguser:x:1001:1001::/home/testinguser:/bin/sh

ホームディレクトリ /home/testinguser が表示されていない場合は、sudo usermod -d /home/testinguser testinguser というコマンドを実行して更新する必要があります。

その後、自分のアカウントからログアウトし、testinguserでログインし直すと、以下のようなサブディレクトリが自動的に作成されていました。

alaaa@aa-lu:~$ ls -l /home/testinguser/
合計 36
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Desktop
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Documents
drwxr-xr-x 2 testinguser testinguser 4096 8月23日 10:05 ダウンロード
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 音楽
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:07 Pictures
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Public
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Templates
drwxr-xr-x 2 testinguser testinguser 4096 Aug 23 10:05 Videos

etc/skel`の内容をコピーする必要はありませんでした。

可能であれば、別の新規ユーザーを作成して、この手順を踏んでみていただけますか?終わったら、この新しいユーザーを sudo deluser testinguser && sudo rm -r /home/testinguser で削除してください。

これでうまくいかなかった場合は、バグだと思われます。

解説 (5)

**UPDATE: このソリューションは壊れていて、私にも使えません。

ユーザーのホームディレクトリが存在しない場合に作成したい場合は、-m フラグを付けて useradd コマンドを実行してください。これにより、/etc/skelディレクトリからすべてのファイルがコピーされます。

useradd -m username

お使いのシステムに合わせて設定を行う必要があるかもしれません。マニュアルページによると :

  -m, --create-home
    Create the user's home directory if it does not exist. The files and directories
    contained in the skeleton directory (which can be defined with the -k option)
    will be copied to the home directory.

    By default, if this option is not specified and CREATE_HOME is not enabled, no
    home directories are created.

と、さらに示しています。

CONFIGURATION

   The following configuration variables in /etc/login.defs change the behavior
   of this tool:

   CREATE_HOME (boolean)
       Indicate if a home directory should be created by default for new users.
解説 (5)

adduserコマンドで新規ユーザを作成した場合は,新規ユーザ用のホームディレクトリを作成する必要はありません。ホームディレクトリは,/home`ディレクトリにユーザ名を冠したもので,以下のサブディレクトリがあります。デスクトップ」「ダウンロード」「ドキュメント」「ミュージック」「ピクチャー」「ビデオ(その他)」のサブディレクトリを含む、ユーザー名を冠したホームディレクトリが自動的に作成されます。

useradd`コマンドで新規ユーザーを作成した場合は、ターミナルで以下のコマンドを実行してください。

sudo -i     #enter your user password when you are asked
gedit /etc/passwd

をクリックして、root として /etc/passwd ファイルを編集します (これは最も重要なステップです)。新しいユーザーの名前が書かれている行を探し、次のように彼のためにデフォルトのホームディレクトリを設定します。

new_username:x:1001:1001::/home/new_username:/bin/sh

この行では、新しいユーザの本名を追加したり、デフォルトのシェルを設定したりすることもできます。以下のようになります。

new_username:x:1001:1001:Real New Username,,,:/home/new_username:/bin/bash

注意して、他の変更をしないでください。

ファイルを保存した後、rootアカウントから外に出る前に、以下のコマンドを実行します。

mkdir /home/new_username                                #to create the directory /home/new_username
cp -r /etc/skel/. /home/new_username                    #to copy skeleton files to /home/new_username
chown -R new_username:new_username /home/new_username   #to change the owner of /home/new_username to the new user

これらの設定を行うと、最初のログイン時に新規ユーザーのホームフォルダが自動的に作成されます。

関連項目: https://askubuntu.com/questions/152707/how-to-make-user-home-folder-after-account-creation

解説 (6)