Ubuntu系统忘记密码

如果root密码忘记,可以进入recovery模式进行重置。或者当sudo命令出现错误时,进入recovery模式可以修复/etc/sudoers文件。

recovery模式

1.启动Ubuntu系统,过程中长按Shift键,进入grub菜单;

 

2.选择recovery mode,按"e"键进入编辑页面;然后,选择recovery模式(恢复模式),回车;

 

3.到/etc目录下修改sudoers权限:chmod 0440 sudoers,搞定;也可以同时新增root密码,下次就不这么麻烦了;

 

4.ctrl+x

 

5.按ctrl+alt+del重启;或者reboot;

 

-----------------------------------

解决办法:

1、重启系统

2、按住ESC键进入显示grub菜单选项,选择recovery mode 即恢复模式;

3、在恢复模式菜单选项下,选择root,使用根用户进入shell

4、使用visudo命令编辑sudoers文件,命令为:

visudo (后面不需要跟任何文件名,直接进入/etc/sudoers.tmp文件编辑模式)

5、可以在执行visudo命令前执行  mount -o remount rw / 重新挂载一次文件系统

6、对照原文件恢复sudoers文件内容,以下图片为原文件内容。

7、使用CTRL+X 保存并离开,注意:保存时文件会以/etc/sudoers.tmp格式保存,不用管.tmp 实际是对sudoer文件进行操作。

8、更改完成之后,执行reboot重启系统即可恢复使用sudo权限。

 

状况:

  因为修改了/etc/sudoers以及相关权限,导致sudo无法使用,恰好Ubuntu的root密码没有设置。

  错误如下:

  ~$ sudo

  sudo: >>> /etc/sudoers:syntax error 在行 21 附近<<<

  sudo: /etc/sudoers 中第 21 行附近有解析错误

  sudo: 没有找到有效的 sudoers 资源,退出

  sudo: 无法初始化策略插件

  于是,只能进去单用户模式(拥有root权力)去修改,在Ubuntu下,开机时长按shift出现各种模式,选择第二个recovery mode,按e进去编辑。


解决方法:

1、重启ubuntu,启动时按Esc或Shift键,可以看到引导选项;

2、在引导选项中选择Recovery模式的那一项来引导;

3、进入Recovery Menu页面,选择root,也就是进入试用root用户进行系统恢复,在这里可以执行超级用户的权限的操作,回车后可以看到熟悉的 root@user ~# 命令提示符;

4、设置或者撤销/etc/sudoers文件的权限,也可以将该文件改回到发生错误之前的状态。

    chmod 666 /dev/null

    mount -o remount rw /

    vi /etc/sudoers

    恢复本文件内容并存盘

5、退出Recovery模式,重新启动ubuntu。

# /etc/sudoers

#

# This file MUST be edited with the 'visudo' command as root.

#

# See the man page for details on how to write a sudoers file.

#

 

Defaults env_reset

 

# Host alias specification

 

# User alias specification

 

# Cmnd alias specification

 

# User privilege specification

root ALL=(ALL) ALL

 

# Allow members of group sudo to execute any command after they have

# provided their password

# (Note that later entries override this, so you might need to move

# it further down)

%sudo ALL=(ALL) ALL

#

#includedir /etc/sudoers.d

 

# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL

posted @ 2016-10-12 15:27  Chell  阅读(903)  评论(0编辑  收藏  举报