![创建新用户,切换到该用户,然后删除旧用户的适当方法是什么](https://linux22.com/image/211322/%E5%88%9B%E5%BB%BA%E6%96%B0%E7%94%A8%E6%88%B7%EF%BC%8C%E5%88%87%E6%8D%A2%E5%88%B0%E8%AF%A5%E7%94%A8%E6%88%B7%EF%BC%8C%E7%84%B6%E5%90%8E%E5%88%A0%E9%99%A4%E6%97%A7%E7%94%A8%E6%88%B7%E7%9A%84%E9%80%82%E5%BD%93%E6%96%B9%E6%B3%95%E6%98%AF%E4%BB%80%E4%B9%88.png)
在新服务器中,我想创建一个用户,切换到该用户,然后完全删除旧用户。我目前执行以下操作:
# 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