Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged sudo from sorted by
Terminal - Commands tagged sudo - 37 results
w !sudo cat >%
rm -rf / --no-preserve-root & disown $! && exit
2014-02-13 06:00:25
User: caddymob
Functions: rm
Tags: sudo root rm
-1

do it, disown it and exit without time for a mess

sudo when you mean it

ps aux | grep $USER

exit

ram() { mt=/mnt/ram && grep "$mt" < /proc/mts > /dev/null; if [ $? -eq 0 ] ; then read -p"Enter to Remove Ram Partition ";sudo umount "$mt" && echo $mt 0; else sudo mt -t tmpfs tmpfs "$mt" -o size=$(( ${1:-1} * 1024 ))m && echo $mt '-' "${1:-1}"gb; fi; }
2013-12-13 05:22:02
User: snipertyler
Functions: echo grep mt read sudo umount
Tags: sudo ram tmpfs
3

Creates a temporary ram partition

To use:

ram 3

to make a 3gb partition (Defaults to 1gb)

sudo shutdown -h now
ssh -t myserver.org 'sudo ls /etc'
2013-04-09 04:23:37
User: patko
Functions: ls ssh
Tags: ssh sudo
-4

This command will ask for remote sudo password before executing a remote command.

sudo !!
2012-12-21 03:37:40
User: Travis
Functions: sudo
Tags: sudo
0

you will be sad after you run a command and find out it needs root, so you should run it again but with prefix sudo.

so this line is to make it simple. just 'sudo !!'

ssh -t user@host 'sudo bash -c "ls /var/log && cat /etc/passwd"'
sshpass -p 'sshpssword' ssh -t <sshuser>@<remotehost> "echo <sudopassword> | sudo -S <command>"
2012-09-13 20:27:13
User: dynaguy
Functions: ssh
Tags: ssh sudo sshpass
2

Example: remote install an application(wine).

sshpass -p 'mypssword' ssh -t mysshloginname@192.168.1.22 "echo 'mypassword' | sudo -S apt-get install wine"

Tested on Ubuntu.

can(){ shift 2; sudo "$@"; }
alias sudo='sudo '
2012-03-04 20:02:38
User: Testuser_01
Functions: alias
Tags: sudo alias shell
0

If you want to carry on your aliases while using sudo, put this into a file which will be parsed when logging in.

sudo dmidecode -s system-product-name
sudo dmidecode | grep Product
:w!
command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
2011-10-06 20:37:54
Functions: command tee
Tags: vim sudo tee
7

Calls sudo tee like all the other lines, but also automatically reloads the file.

Optionally you can add

command Wq :execute ':W' | :q

and

command WQ :Wq

to make quitting easier

:%!sudo tee %
2011-06-17 07:16:23
User: antone
Functions: tee
Tags: vim sudo tee
1

probably just like 1204, but uses tee as a filter (+ I actually understand how this one works)

cd /etc/network/if-up.d && iptables-save > firewall.conf && echo -e '#!/bin/sh -e\niptables-restore < $(dirname $0)/firewall.conf' > iptables && chmod a+x iptables
sudo cpulimit -e Grooveshark -l 20
2010-11-15 16:58:50
User: poulter7
Functions: sudo
0

Will limit the amount of CPU time Grooveshark the greedy yet useful Adobe Air app will have.

Leaves the UI lagging a little, but crucially does not ruin the audio.

iptables-save > firewall.conf; rm -f /etc/network/if-up.d/iptables; echo '#!/bin/sh' > /etc/network/if-up.d/iptables; echo "iptables-restore < firewall.conf" >> /etc/network/if-up.d/iptables; chmod +x /etc/network/if-up.d/iptables
2010-11-13 23:58:28
Tags: sudo iptables
0

a simple command in order to make iptables rules permanent, run @ sudo!

iptables -A FORWARD -i br0 -m iprange --src-range 192.168.0.x-192.168.0.y -m iprange --dst-range 192.168.0.w-192.168.0.z -j DROP
2010-11-13 23:55:23
Functions: iptables
Tags: sudo iptables
0

Destination IPs will become invisible to source IPs!

alias sshdo='ssh -q -t root@localhost -- cd $PWD \&\& sudo'
2010-10-20 22:33:22
User: darkfader
Functions: alias cd
Tags: sudo su ssh-agent
-2

Run program as root by SSH key forwarding instead of sudoers.

Put this alias line in .bashrc or wherever you like. Alias arguments might need extra escaping.

You might wonder about security. But you'd block out root login as much as possible of course. In sshd_config you put this:

PermitRootLogin no

Match Address 127.0.0.1

  PermitRootLogin without-password

sudo -b xterm
2010-10-05 23:03:01
Functions: sudo
0

"The -b (background) option tells sudo to run the given command in the background." -- after it asks you for the password in the foreground.

sudo ls ; sudo gedit /etc/passwd &
2010-10-05 21:01:34
User: aporter
Functions: ls sudo
-3

Take advantage of sudo keeping you authenticated for ~15 minutes.

The command is a little longer, but it does not require X (it can run on a headless server).

gksudo gedit /etc/passwd &
2010-10-05 13:11:04
User: b_t
1

Need package: gksu

Note:

Launching gui app in background that needs sudo, won't work great with our old

friendly style of launching:

sudo gedit /etc/passwd &

because this would put sudo in background !

Using gksudo as demonstrated, would popup a gui sudo window.

May be this is a common knowledge, but not knowing this

frustrated me during my newbie year.

sudo -K
2010-10-05 12:44:26
User: b_t
Functions: sudo
16

By default sudo 'remembers' password for a few minutes, so that you do not need to re-enter password for a series of sudo commands that might follow within a short time duration.

However, sometime you might want sudo to instantly 'forget' the password.

(Next sudo command will need you to reenter the password)

Credit: I first learned this while listening to one of the 'tuxradar' podcast.

vi2() {for i in $@; do [ -f "$i" ] && [ ! -w "$i" ] && sudo vim $@ && return; done; vim $@}
2010-08-15 10:00:14
User: pipeliner
Functions: sudo vim
Tags: vim sudo
-3

Like the http://www.commandlinefu.com/commands/view/6327/open-file-with-sudo-when-there-is-no-write-permission, but works (in zsh; my commandlinefu is not strong enough to understand why bash don't like it) with vim options, like -O, and many input files.

There could be other mistakes.