"To be a warrior is not a simple matter of wishing to be one. It is rather an endless struggle that will go on to the very last moment of our lives. Nobody is born a warrior, in exactly the same way that nobody is born an average man. We make ourselves into one or the other." --Kokoro
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
Tolimit user frank toONLY one ortwo hosts torun those commands,you need toreplace ALL with the hostnames of your machines.
1
2
3
frank linuxhost1=/bin/kill,/sbin/linuxconf,/etc/init.d/
Tolimit user frank toONLY one linux host but also grant him access touser other files owned by another user.Toenable that,frank would need to`sudo-utimmy command`.
1
2
3
4
frank linuxhost1=(timmy,george)/bin/kill,/sbin/linuxconf,/etc/init.d/
Let's say frank is getting tired of having to type sudo -u timmy everytime, as he spends most of his time editing timmys files.
This says that frank will type his password in one time, then have a 20 minute window which he won'thave tore-type his password andsudo-uwill defaulttouser timmy.
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.
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:
1
fire<TAB>
and the shell will complete the command for him as:
1
firefox
If, however, john is added to the sudoers file and he types:
1
sudo fire<TAB>
the shell will do nothing.
To enable tab-completion with sudo, add the following to your ~/.bashrc:
1
complete-cf sudo
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
1
Defaults env_keep+="HOME"
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:
1
Defaults !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.
1
$sudo-Epacman-Syu
Because of this you may wish to add an alias in ~/.bashrc:
1
alias sudo="sudo -E"
Another way of fixing this would be to add in /etc/sudoers:
1
Defaults !env_reset
If you want to just pass *_proxy variables, add the following:
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:
1
alias sudo='sudo '
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:
1
2
#Defaults specification
Defaults insults
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:
1
Defaults timestamp_timeout=0,rootpw
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:
1
# passwd -l root
A similar command unlocks root.
1
$sudo passwd-uroot
Alternatively, edit /etc/shadow and replace the root’s encrypted password with “!”:
1
root:!:12345::::::
To enable root login again:
1
$sudo passwd root
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:
1
2
[super-user-command]
super-user-command=sudo
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
1
AdminIdentities=unix-user:0
is replaced by something else, depending on the system configuration. It can be a list of users and groups, for example
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
1
2
3
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear. You have to run "ssh -t hostname sudo <cmd>".
#
#Defaults requiretty
Display User Privileges
You can find out what privileges a particular user has with the following command:
1
sudo-lU askapache
Or view your own with
1
sudo-l
1
2
3
4
5
6
7
8
9
10
11
Matching Defaults entries foraskapache on thishost: