创建新用户,切换到该用户,然后删除旧用户的适当方法是什么

创建新用户,切换到该用户,然后删除旧用户的适当方法是什么

在新服务器中,我想创建一个用户,切换到该用户,然后完全删除旧用户。我目前执行以下操作:

# create new user
sudo useradd -m -s /bin/bash newuser
sudo passwd newuser
sudo usermod -aG sudo newuser

# set up user
su - newuser

# delete old user
sudo deluser --remove-home olduser

当我运行最终命令时,我会收到警告user olduser is currently used by process xxx. returned error code 8. Exiting。我相信我可以pkill执行用户的流程,但是执行这些操作的行业标准方式是什么?

看起来可行sudo userdel -r -f olduser,但它给出的日志似乎并不理想:

userdel: user olduser is currently used by process xxx
userdel: olduser mail spool (/var/mail/olduser) not found

答案1

你几乎做得对了,但是你错过了中间的两个重要步骤,这两个步骤本身就需要完整的文章。我将这些步骤作为简短的描述,相信您有能力并且能够通过网络搜索找到它们。

不,正如您已经发现的那样,su - newuser无论您多么希望它都不起作用(至少在大多数现代发行版上)。要解释所有的原因太复杂了。

# create new user
sudo useradd -m -s /bin/bash newuser
sudo passwd newuser
sudo usermod -aG sudo newuser

# 1 !!! IMPORTANT STEP !!!
# login on different VT or ssh connection into new user

# 2 !!! IMPORTANT STEP !!!
# logout all sessions for old user, and terminate all the 
# processes it may have running after the loguts 
# (for example tmux/screen sessions, service processes and whatnot)

# delete old user from the pristine newuser session
sudo deluser --remove-home olduser

相关内容