sudo

All posts tagged sudo

Lately been struggling with quotes and getting my variables working correctly inside the quotes.  With more precisely variables followed directly by text without spaces.  Like updating sudoers files the end result I want, to take whatever groups are in sudoers now and append _ldap to them using only bash echo.

%testgroup1 ALL=(ALL) ALL

%testgroup1_ldap ALL=(ALL) ALL

 

use strong quotes around everything except the variable

echo ‘%’$GROUP’_ldap ALL=(ALL) ALL’ >> /some/file

 

 

 

Letting user ONLY run certain commands as root.

We have recently been having issues at work with users, sudo su directly to root thus bypassing sudo logging and completely blocking any ability to trace back what was done to the systems.  Most of these issues can be traced back to my team (system administration) and I will take the blame for the most part.  We were handing out full sudo root to users by just saying don’t use sudo su or any of the other methods to drop directly to a root prompt, it’s against corporate security and was based on the trust factor.  The reason:  there’s primarily only 2 UNIX SE’s, we had roughly 700 UNIX boxes 6 months ago, we now have close to 1500 UNIX hosts.  Way to much work to worry about trying to setup and stricten the sudoers file.  However, now that things have settled down a bit.  Everyone and their brother are using pretty much only sudo su -.  It’s a complete mess at this point.  So much for the trust factor…

Now the users are spoiled and so used to just sudo su, it’s crazy some don’t even know when to use sudo properly and think sudo su is how your suppose to use sudo.  Anyhow got tired of slapping little Johnnies hand and said no no no…So just added this to the sudoers file…  The bottom line is that Johnny doesn’t listen…

Sure the smart thing to do is to stricten the sudoers down and only hand out what they need, or setup Puppet, but those things take time and I need to re-lock down the environment now.

Defaults mailto=systemadmin@example.com, mail_badpass, mail_no_user, mail_no_perms

Defaults logfile=/root/.sudolog, log_year, log_host

Defaults always_set_home
Defaults env_reset

Cmnd_Alias SHELLS=/bin/ash, /bin/bash, /bin/csh, /bin/sh, /usr/bin/es, /usr/bin/ksh,/usr/bin/rc, /usr/bin/tcsh, /usr/bin/zsh, /bin/sash, /bin/zsh, /usr/bin/esh, /bin/tcsh, /sbin/sh, /usr/local/bin/bash, /usr/local/bin/tcsh, /bin/login, /bin/su

johnny   ALL=ALL, !SHELLS

 

If anyone has any other suggestions I would love to hear them

Some of this is not new but just adding it, not my writing found this article somewhere.

To allow a user to gain full root privileges when he/she precedes a command with “sudo”, add the following line:
USER_NAME ALL=(ALL) ALL

and/or to allow a user sudo access from the local machine only:

USER_NAME HOSTNAME=(ALL) ALL

and/or to allow members of group wheel sudo access requiring no password:

%wheel ALL=(ALL) NOPASSWD: ALL

where USER_NAME is the user name of the individual.

Password cache timeout

Users may wish to change the default timeout before the cached password expires. This is accomplished by adding following to /etc/sudoers (visudo) for example:

Defaults:USER_NAME timestamp_timeout=20

where the password expires for user USER_NAME if unused for over 20 minutes. Values between 0 and 1 are also allowed.

 

Enabling tab-completion in bash

Tab-completion, by default, will not work when a user is initially added to the sudoers file. For example, normally john only needs to type:

and the shell will complete the command for him as:

If, however, john is added to the sudoers file and he types:

the shell will do nothing.

To enable tab-completion with sudo, add the following to your ~/.bashrc:

Alternatively, you could also install and enable bash-completion to get smarter tab-completion for commands like sudo, see bash#Auto-completion for more information.

 

Run X11 apps using sudo

To allow sudo to start graphical application in X11, you need to add

to visudo.

Disable per-terminal sudo

Warning: This will let any process use your sudo session

If you are annoyed by sudo’s defaults that require you to enter your password every time you open a new terminal, disable tty_tickets:

Environment variables (Outdated?)

If you have a lot of environment variables, or you export your proxy settings via export http_proxy="...", when using sudo these variables do not get passed to the root account unless you run sudo with the -E option.

Because of this you may wish to add an alias in ~/.bashrc:

Another way of fixing this would be to add in /etc/sudoers:

If you want to just pass *_proxy variables, add the following:

Add /sbin and /usr/sbin to root’s PATH

If you want to run administrative commands (those in /sbin or /usr/sbin) with sudo without using their full path, add:

in /etc/sudoers.

This allows you to do:

instead of:

or:

Passing aliases

If you use a lot of aliases, you might have noticed that they do not carry over to the root account when using sudo. However, there is an easy way to make them work. Simply add the following to your ~/.bashrc or /etc/bash.bashrc:

Insults

Users can configure sudo to display clever insults when an incorrect password is entered instead of printing the default “wrong password” message. Find the Defaults line in /etc/sudoers and append “insults” after a comma to existing options. The final result might look like this:

To test, type sudo -K to end the current session a let sudo ask for the password again.

Root password

Users can configure sudo to ask for the root password instead of the user password by adding “rootpw” to the Defaults line in /etc/sudoers:

Disable root login

Warning: Arch Linux is not fine-tuned to run with a disabled root account. Users may encounter problems with this method.

With sudo installed and configured, users may wish to disable the root login. Without root, attackers must first guess a user name configured as a sudoer as well as the user password.

Warning: Ensure a user is properly configured as a sudoer before disabling the root account!

The account can be locked via passwd:

A similar command unlocks root.

Alternatively, edit /etc/shadow and replace the root’s encrypted password with “!”:

To enable root login again:

kdesu

kdesu may be used under KDE to launch GUI applications with root privileges. It is possible that by default kdesu will try to use su even if the root account is disabled. Fortunately one can tell kdesu to use sudo instead of su. Create/edit the file /usr/share/config/kdesurc:

Policykit

When disabling the root account, it is nessecary to change the policykit configuration for local authorification to reflect that. The default is to ask for root password, so that must be changed. With polkit-1, this can be achieved by editing /etc/polkit-1/localauthority.conf.d/50-localauthority.conf so that

is replaced by something else, depending on the system configuration. It can be a list of users and groups, for example

or

Debugging Sudo

SSH TTY Issues

SSH does not allocate a tty by default when running a remote command. Without a tty, sudo cannot disable echo when prompting for a password. You can use ssh’s “-tt” option to force it to allocate a tty. (use -tt twice).

The Defaults option requiretty only allows the user to run sudo if they have a tty

Display User Privileges

You can find out what privileges a particular user has with the following command:

Or view your own with

Example Sudoers

This is especially helpful for those using terminal multiplexers like screen, tmux, or ratpoison, and those using sudo from scripts/cronjobs.