linux中su与sudo与su-的理解

来源:互联网 发布:js代码攻击 编辑:程序博客网 时间:2024/05/19 18:42

对于 su , su - , sudo的理解

su 与 su - 的相同点与区别

su全称是switch user,意思就是切换用户的功能.
当我们需要使用更高的权限去执行命令时,则需要先获取root权限。因此su 、su - 这样的命令也就诞生了。
1. 相同点: 默认情况下 su 与 su - 都是切换成root用户 (有一个关于root密码的知识点 下面将讲解)

nieyh@nieyh-QTJ5:~$ su密码:root@nieyh-QTJ5:/home/nieyh#
nieyh@nieyh-QTJ5:~$ su -密码:root@nieyh-QTJ5:/home/nieyh#
  1. 不同点:su 切换用户却不切换工作环境 , su - 同是切换用户与工作环境, 如果Shell环境不一样将会出现下面的无法找到对应命令的问题。
nieyh@nieyh-QTJ5:~$ su密码:root@nieyh-QTJ5:/home/nieyh# pwd/home/nieyhroot@nieyh-QTJ5:/home/nieyh# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/gamesroot@nieyh-QTJ5:/home/nieyh# adb程序 'adb' 已包含在下列软件包中: * adb * android-tools-adb请尝试:apt install <选定的软件包>
nieyh@nieyh-QTJ5:~$ su - root密码:root@nieyh-QTJ5:~# pwd/rootroot@nieyh-QTJ5:~# echo $PATH/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/home/nieyh/Android/Sdk/platform-tools/:/home/nieyh/Android/Sdk/tools/root@nieyh-QTJ5:~# adbAndroid Debug Bridge version 1.0.39Revision 3db08f2c6889-androidInstalled as /home/nieyh/Android/Sdk/platform-tools/adbglobal options: -a         listen on all network interfaces, not just localhost -d         use USB device (error if multiple devices connected) -e         use TCP/IP device (error if multiple TCP/IP devices available) -s SERIAL     use device with given serial number (overrides $ANDROID_SERIAL) -p PRODUCT     name or path ('angler'/'out/target/product/angler');     default $ANDROID_PRODUCT_OUT -H         name of adb server host [default=localhost] -P         port of adb server [default=5037] -L SOCKET  listen on given socket for adb server [default=tcp:localhost:5037]

su的缺点造就了sudo的诞生

由于用户通过 su root 命令直接获取root权限,从而造成用户的权限太大,也就可能给系统造成危险。
为了既保证系统的安全又可以执行相应命令,sudo 也就以此诞生。

sudo 通过配置文件来限制用户的权限 (以下就是 /etc/sudoers 文件)

## This file MUST be edited with the 'visudo' command as root.## Please consider adding local content in /etc/sudoers.d/ instead of# directly modifying this file.## See the man page for details on how to write a sudoers file.#Defaults    env_resetDefaults    mail_badpassDefaults    secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"# Host alias specification# User alias specification# Cmnd alias specification# User privilege specificationroot    ALL=(ALL:ALL) ALL# Members of the admin group may gain root privileges%admin ALL=(ALL) ALL# Allow members of group sudo to execute any command%sudo   ALL=(ALL:ALL) ALL# See sudoers(5) for more information on "#include" directives:#includedir /etc/sudoers.d

可以看到上面的配置 %sudo ALL=(ALL:ALL) ALL, sudo 可以执行任何命令,所以下面的命令也没有报错。

nieyh@nieyh-QTJ5:~$ sudo apt-get clean[sudo] nieyh 的密码:nieyh@nieyh-QTJ5:~$

当我将配置改成这样子,

# Allow members of group sudo to execute any command%sudo   ALL=/sbin/mount#ALL=(ALL:ALL) ALL

就会出现下面的错误提示

nieyh@nieyh-QTJ5:~$ sudo apt-get[sudo] nieyh 的密码:对不起,用户 nieyh 无权以 root 的身份在 nieyh-QTJ5 上执行 /usr/bin/apt-get。

所以通过 /etc/sudoers 配置文件来限制用户的权限使用,可以达到安全可控的目的。(更多的文件配置方法请自行百度或者等待作者更新)

总结

一般的使用情况下,最好使用sudo来执行命令,避免自己误操作破坏了系统。
如果是存在多用户使用同一个系统的话,主用户可以使用root权限来配置 /etc/sudoers 文件来指派不同用户不同的权限,从而保证系统的安全。

额外的补充下root密码的问题

如果用户忘记了root密码, 并且sudo能获取到最高权限,则可以通过一下命令来修改密码 (如果没有root权限的话, 只能通过操作系统来清空密码)

root@nieyh-QTJ5:~# sudo passwd输入新的 UNIX 密码:重新输入新的 UNIX 密码:passwd:已成功更新密码

此为作者的理解,如有不对的地方请指正。
如果觉得喜欢本片篇文章的话,请点击的关注,或者来参加评论(这样的话我会更加努力去写更好的文章的)