Shortcut for sudo & other shell improvements

David Obdržálek
3 min readJul 14, 2019

As everyone who works with Linux servers, I’ve typed sudo probably more then any other word. Ever. Here are some of my fav shell improvements I use.

Free photo by Altphotos, edit ΔO

Shortcut sudo into just s

Save yourslef 3/4 strokes every time. Make a bash alias, put this into ~/.bashrc:

alias s='sudo'
alias sus='sudo -s'

Reenter your bash and insted of sudo apt-get update, you can simply run things like s apt update 😊

But wait, it breaks bash completion abilities. Simple solution here: put complete -F _root_command s into /etc/bash_completion.d/s. Or simply:

s sh -c "echo complete -F _root_command s > /etc/bash_completion.d/s"

Reenter bash and completion works again 😇

Make your sudo paswrodless

Have a quick thought about this, if you don’t use this already. There is a good UX gain, but also some risks.

Pros:
+ You won’t need to type in your password every 10 minutes
+ You’ll be seen typing password less

Cons:
– Only do this if your access is secure enough (via certificates stored on encrypted-only drives)
– You’ll give away sudo if you leave your terminal open

If you want to proceed & your username is dave, put this into /etc/sudoers.d/dave :

dave   ALL=(ALL) NOPASSWD:ALL 

If you use different username 🧐, run this command:

sudo su -c "echo \"$USER  ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/$USER"

Now you’re all set to use sudo without password promt 😉

Make ssh better with screen

I always use screen for shells over ssh. It has several benefits:
+ Multiple shells over one connection
+ Running shells & processes keep running even if you disconnect
+ When your connection breaks (say you’re travelling in Italy or a 3rd world country), you’ll resume where you left

Simply put this at the end of your ~/.profile:

screen -R && exit

If you also use local access (local servers, kiosks, Raspberry Pi with an interface), and you only want to run screen for remote access, make it like this:

[[ ! -z $SSH_TTY ]] && screen -R && exit

Now every time you connect, your shell enters into the last opened screen, or makes a new one. Once you exit your last screen (Ctrl+D), your connection closes automatically.

Works like a charm ☺️

Make bash a bit more comfortable

With reasonable use of bash aliases you can make your life a little easier. My ~/.bashrc always looks like this:

PS1='[\[\e[0;32m\]\u@\h \[\e[0;34m\]\W\[\e[0;00m\]]\$ ';

alias ls='ls --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

alias ll='ls -lhF'
alias la='ls -AF'
alias l='ls -CF'
alias s='sudo'
alias sus='sudo -s'
alias a2reload='sudo service apache2 reload'

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
. /etc/bash_completion
fi

See my fav shell settings here: https://tools.deltazero.cz/server/setup.basic.sh

Make a backup command

For a one-cmd remote backup with rsync, see my previous article about rsync into chroot ☺️

More time savers for Linux servers

See my tools for 5-min Debian / Ubuntu server deployment:
https://github.com/deltazero-cz/linux-tools/tree/master/server

What do you think of scripts like these? Let me know in the comments 🙂

--

--