该情况可以通过vipw更改/etc/shadow 将用户的UID和GID更动进而模拟故障,并且该情况大多数发生都是由于系统管理员对用户以及文件的不熟悉进行误操作导致。
一.接下来讲述一下如何导致该报错会发生的情况:
正常情况下,UID和GID如下所示:
[weihu999@local ~]$ cat /etc/passwd | grep weihu999
weihu999:x:1002:1002::/home/weihu999:/bin/bash
[weihu999@local ~]$
在root用户下通过vipw更改UID和GID如下所示:
[root@local ~]# cat /etc/passwd | grep weihu999
weihu999:x:1005:1007::/home/weihu999:/bin/bash
[root@local ~]#
之后通过su切换用户显示如下:
[root@local ~]# su weihu999
bash: /home/weihu999/.bashrc: Permission denied
bash-5.0$
bash-5.0$ ll
bash: ll: command not found
bash-5.0$ ls -lh
ls: cannot open directory '.': Permission denied
bash-5.0$ pwd
/root
bash-5.0$ whoami
weihu999
bash-5.0$
bash-5.0$ cd /home/weihu999/
bash: cd: /home/weihu999/: Permission denied
bash-5.0$
此时通过ssh连接显示标题中的报错:
ssh weihu999@192.168.43.5
Warning: Permanently added '192.168.43.5' (RSA) to the list of known hosts.
weihu999@192.168.43.5's password:
X11 forwarding request failed on channel 0
Last login: Wed Nov 1 10:50:13 2023
Could not chdir to home directory /home/weihu999: Permission denied
-bash: /home/weihu999/.bash_profile: Permission denied
[weihu999@local /]$
但是若提前加入wheel组则使用sudo -i 可使用root权限进行修复,并且变量保持不变(无论是su切换还是ssh远程登录的方式):
bash-5.0$ sudo -i
[sudo] password for weihu999:
____ _ ____ _ _
| __ )(_) __ _ / ___| | ___ _ _ __| |
| _ \| |/ _` | | | |/ _ \| | | |/ _` |
| |_) | | (_| | |___| | (_) | |_| | (_| |
|____/|_|\__, |\____|_|\___/ \__,_|\__,_|
|___/
_____ _ _ _ _
| ____|_ __ | |_ ___ _ __ _ __ _ __(_)___ ___ | | (_)_ __ _ ___ __
| _| | '_ \| __/ _ \ '__| '_ \| '__| / __|/ _ \ | | | | '_ \| | | \ \/ /
| |___| | | | || __/ | | |_) | | | \__ \ __/ | |___| | | | | |_| |> <
|_____|_| |_|\__\___|_| | .__/|_| |_|___/\___| |_____|_|_| |_|\__,_/_/\_\
|_|
_____ _____ _ ____ _ _ ___
| ___|__ _ __ | ____|_ _| | ___ _ __ |___ \ / | / |/ _ \
| |_ / _ \| '__| | _| | | | | |/ _ \ '__| __) || | | | | | |
| _| (_) | | | |__ | |_| | | __/ | / __/|| | _ | | |_| |
|_| \___/|_| |_____|\__,_|_|\___|_| |_____||_|(_)|_|\___/
Welcome to BigCloud Enterprise Linux For Euler 21.10 (GNU/Linux 4.19.90-2107.6.0.0100.oe1.bclinux.x86_64 x86_64)
System information as of Wed Nov 1 10:57:41 CST 2023
* System CPU load: 0.00 0.00 0.00 * System uptime: 10:57:41 up 18 min
* Active sessions: 2 * Memory usage: 1012 / 1489 MB
* Processes count: 195
* Get Support :
* Ask Questions:
http://forum.bclinux.org
* Contact US :
MAIL : support@bclinux.org / TEL : 4001-10086-5
[root@local ~]#
通过上述sudo -i使用root权限或者直接使用root进行修复,修复方式如下:
1.rm -rf /home/user 用以删除宿主目录
[root@local home]# rm -rf weihu999
[root@local home]# ll
total 0
drwx------ 2 weihu weihu 83 Jun 20 22:05 weihu
drwx------ 2 weihu000 weihu000 62 Oct 18 16:04 weihu000
drwx------ 2 weihu000 weihu000 83 Jun 20 22:00 weihu01
2.执行groupdel usergroup删除组,并使用vipw将对应用户的记录删除
[root@local home]# groupdel weihu999
[root@local home]# vipw
You have modified /etc/passwd.
You may need to modify /etc/shadow for consistency.
Please use the command 'vipw -s' to do so.
3.再次创建用户时出现creating mailbox file: file exists报错则使用userdel -fr user
[root@local home]# useradd weihu999
Creating mailbox file: File exists
[root@local home]# userdel -fr weihu999 //-r: 删除用户的同时,删除与用户相关的所有文件。 -f:强制删除用户,即使用户当前已登录;
[root@local home]#
[root@local home]#
4.之后创建用户即可正常
[root@local home]# useradd weihu999
[root@local ~]# cat /etc/passwd | grep weihu999
weihu999:x:1002:1002::/home/weihu999:/bin/bash
[root@local ~]#