📜  使用 sudo 运行上一个命令

📅  最后修改于: 2022-05-13 01:57:05.345000             🧑  作者: Mango

使用 sudo 运行上一个命令

很多时候,在 linux 中,我们执行命令只是看到显示“权限被拒绝”的错误。然后我们意识到我们在命令之前忘记了sudo ,我们再次编写命令,这次是在前面加上sudo

例子:

rohan@Rohan-PC:~$ apt-get update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - 
SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open 
(13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - 
RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - 
RemoveCaches (13: Permission denied)

rohan@Rohan-PC:~$ sudo apt-get update
[sudo] password for rohan: 
Get:1 http://packages.microsoft.com/repos/vscode stable InRelease [2, 802 B]
Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease                
Hit:3 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease 
Ign:4 http://dl.google.com/linux/chrome/deb stable InRelease           
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Hit:6 https://deb.nodesource.com/node_8.x xenial InRelease             
Hit:7 http://ppa.launchpad.net/hanipouspilot/rtlwifi/ubuntu xenial InRelease
Hit:8 http://dl.google.com/linux/chrome/deb stable Release             
Get:9 http://packages.microsoft.com/repos/vscode stable/main amd64 Packages [77.9 kB]
Hit:10 http://ppa.launchpad.net/maarten-baert/simplescreenrecorder/ubuntu xenial InRelease
Get:12 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Hit:13 https://download.sublimetext.com apt/stable/ InRelease          
Hit:14 http://ppa.launchpad.net/noobslab/apps/ubuntu xenial InRelease  
Get:15 http://archive.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Hit:16 http://ppa.launchpad.net/noobslab/macbuntu/ubuntu xenial InRelease
Hit:17 http://packages.treasuredata.com/3/ubuntu/xenial xenial InRelease
Get:18 http://nginx.org/packages/ubuntu xenial InRelease [4, 339 B]     
Ign:18 http://nginx.org/packages/ubuntu xenial InRelease    
Hit:19 http://ppa.launchpad.net/otto-kesselgulasch/gimp/ubuntu xenial InRelease
Hit:20 http://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial InRelease
Hit:21 http://ppa.launchpad.net/videolan/master-daily/ubuntu xenial InRelease
Hit:22 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease
Hit:23 https://download.docker.com/linux/ubuntu xenial InRelease
Hit:24 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Fetched 408 kB in 3s (117 kB/s)                     
Reading package lists... Done

当您键入的命令很长时,情况会变得令人沮丧。您当然不想重新输入那个长命令。为了解决这个问题,提出了以下很酷的 linux hack:

Command : sudo !!

此命令使用sudo infront 重复上一个命令。它肯定会节省大量时间和精力,并防止极客感到沮丧。

Example:
rohan@Rohan-PC:~$ apt-get update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - 
SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open 
(13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - 
RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - 
RemoveCaches (13: Permission denied)

rohan@Rohan-PC:~$ sudo !!
sudo apt-get update
Hit:1 http://packages.treasuredata.com/3/ubuntu/xenial xenial InRelease
Ign:2 http://dl.google.com/linux/chrome/deb stable InRelease           
Hit:3 http://ppa.launchpad.net/ansible/ansible/ubuntu xenial InRelease 
Hit:4 http://archive.ubuntu.com/ubuntu xenial InRelease                
Hit:5 http://dl.google.com/linux/chrome/deb stable Release             
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:8 http://nginx.org/packages/ubuntu xenial InRelease [4, 339 B]      
Hit:9 https://dl.winehq.org/wine-builds/ubuntu xenial InRelease        
Ign:8 http://nginx.org/packages/ubuntu xenial InRelease                
Hit:10 http://ppa.launchpad.net/hanipouspilot/rtlwifi/ubuntu xenial InRelease
Hit:11 https://download.sublimetext.com apt/stable/ InRelease          
Hit:12 http://ppa.launchpad.net/maarten-baert/simplescreenrecorder/ubuntu xenial 
InRelease
Get:13 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Hit:14 http://ppa.launchpad.net/noobslab/apps/ubuntu xenial InRelease  
Get:15 http://archive.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Hit:16 https://deb.nodesource.com/node_8.x xenial InRelease            
Hit:17 https://download.docker.com/linux/ubuntu xenial InRelease       
Hit:18 http://ppa.launchpad.net/noobslab/macbuntu/ubuntu xenial InRelease
Hit:19 http://ppa.launchpad.net/otto-kesselgulasch/gimp/ubuntu xenial InRelease
Hit:20 http://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial InRelease
Hit:21 http://ppa.launchpad.net/videolan/master-daily/ubuntu xenial InRelease
Get:22 http://packages.microsoft.com/repos/vscode stable InRelease [2, 802 B]
Hit:23 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Fetched 330 kB in 3s (103 kB/s)                     
Reading package lists... Done