Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 手動新增使用者帳號完整步驟 (不使用 useradd)

編輯:Linux 教學

雖然 useradd 可自動完成帳號建立,但了解底層檔案操作有助於排錯與特殊環境部署。本文示範手動編輯 /etc/passwd、/etc/shadow、/etc/group、建立家目錄、複製骨架檔案的完整流程。

環境說明

以新增使用者 suse (UID/GID 527) 為例。

步驟 1:編輯三大核心檔案

/etc/passwd


echo "suse:x:527:527::/home/suse:/bin/bash" >> /etc/passwd

欄位:用戶名:密碼佔位符:UID:GID:註解:家目錄:Shell

/etc/group


echo "suse:x:527:" >> /etc/group

/etc/shadow (密碼欄位需加密)


# 產生 MD5 加密密碼 (salt 8 碼)
openssl passwd -1 -salt XXXXXXXX "實際密碼"
# 輸出範例: $1$XXXXXXXX$encryptedhash

# 計算自 1970-01-01 至今天數 (last change)
date -d "2011-04-29" +%s / 86400  # 或直接用 shell 運算: $[`date +%s`/86400]

# 寫入 shadow (欄位: 加密密碼:last_change:min_age:max_age:warn:inactive:expire:flag)
echo "suse:\$1\$XXXXXXXX\$encryptedhash:15089:0:99999:7:::" >> /etc/shadow

注意:$ 在雙引號中需轉義 \$,或改用單引號並拼接變數。

步驟 2:建立家目錄並設定權限


mkdir /home/suse
chown -R suse:suse /home/suse
chmod 700 /home/suse

步驟 3:複製骨架檔案 (.bash_profile, .bashrc 等)


cp /etc/skel/.bash* /home/suse/
chown suse:suse /home/suse/.bash*
chmod 644 /home/suse/.bash*

驗證


su - suse
# 或 ssh suse@localhost

若能正常登入並看到 Shell 提示符,表示手動建帳號成功。

補充說明

  • UID/GID 請確保不衝突,可用 cat /etc/passwd | cut -d: -f3 | sort -n | tail -1 查看最大 UID。
  • /etc/skel 內檔案為新用戶預設環境,含 .bash_profile、.bashrc、.bash_logout。
  • 生產環境建議仍使用 useradd,手動方式僅供學習或極少數無 useradd 的精簡環境。
Copyright © Windows教程網 All Rights Reserved