Load nfs-mounted ssh keys at login automatically

I use multiple ssh keys across multiple systems. Some systems need to have the same ssh key loaded.

My solution is to store the generic ones on my nfs mount, accessible only to my user, and to use a function in .bashrc:

load_ssh_key() {
   test -n "${1}" && SSHKEY="${1}" ;
   test -z "${SSHKEY}" && SSHKEY=/mnt/bgstack15/.ssh/bgstack15_devuan.key
   if test -e "${SSHKEY}" ;
      test -z "${SSH_AGENT_PID}" && eval $( ssh-agent ) | grep -viE 'Agent pid'
      ssh-add "${SSHKEY}" 2>&1 | grep -viE "Identity added:" 1>&2
      echo "Unable to get to private key!" 1>&2
   unset SSHKEY


Also, now, the function is generally available for invoking with a filename to load that ssh key. I realize ssh-add is pretty trivial, but I want the function to fail silently for when I’m off-network (when I won’t be doing any ssh work anyway).

Save common commands on this server to .useful file

Command line productivity takes many forms. Some people live by bash autocompletion. I disdained it when I first started using bash (coming from ksh93) but over time realized that it’s a fantastic method for discovery (it’s much more complete on Debian-based distros than Fedora-based ones) and ease of use when typing long pathnames.

When you manage a disparate set of systems, and there are some tasks you repeat often, you usually try to list history and find the useful commands. One of mine:

sudo /var/storage1/shares/public/www/example.com/repo/devuan-deb/update-devuan-deb.sh

But over time, I have many commands I log in for, just to run the one command, and log out. So I have started using a text file: ~/.useful. Inside this file, place your oneliners.

So whenever you log onto a box, and you need to see what commands you run here commonly, check what you’ve saved:

cat ~/.useful

Of course, it’s only useful if you bother to populate it.

echo 'vi $( newest /var/log/debmirror/ log )' >> ~/.useful

Load your bash profile even when switching user

If you use a shared application account, for example, ansible, and want your profile to be used even after switching to that user, here is a solution for you. Add this to the destination user ~/.bashrc.

echo "${SSH_CLIENT}" | grep -q '' && __bp=1
test "$( stat -c '%U' "$( tty 2>/dev/null )" 2>/dev/null )" = "bgstack15" && __bp=1
test "${__bp}" = "1" && test -f /usr/bin/bp && . /usr/bin/bp --noclear

I am using a world-readable copy of my bgstack15 profile, aka bash profile.

Edit terminal title from the command line


export PROMPT_COMMAND='echo -ne "\033]0;NEW TEXT HERE\007"'

Edit terminal title from command line

To modify the window title directly, you just need to use this:

echo -ne "\033]0;NEW TEXT HERE\007"

But in a normal bash environment, your PROMPT_COMMAND will be executed before each display of the prompt, so to affect your interactive shell, you will need that export PROMPT_COMMAND.



  1. https://askubuntu.com/questions/22413/how-to-change-gnome-terminal-title

New user in freeipa has plain bash shell instead of reading .bashrc

So you have a new user in freeipa, and he can successfully log in to a freeipa client. And you know for certain you executed ipa-client-install with the –mkhomedir option. But when you open a terminal as the new user, it shows you the boring bash prompt ‘bash-4.1$’ or whatever version.

You checked the /etc/skel, and it has a valid .bashrc file, and when you dot source your own ~/.bashrc, it then loads the prompt you expect.

Here’s your issue: do a getent passwd username. Look at the login shell of the user. It’s going to be the default /bin/sh. Just change it in ipa to be /bin/bash! An sss_cache -E command was not enough; you have to log out and then back in to have it take effect. It’s probably because the terminal emulator is being called from a process that was started before the account was changed.

Hide comments and blank lines in file

Hide comments and blank lines when viewing file

Original title: Show all non-blank non-comment lines in file

If you want to see just the lines with content, such as in a config file, use this one-liner:

grep -viE '^\s*((#|;).*)?$' smb.conf

How it works

grep -v means invert the selection, i.e., everything that does not match this search.
-iE case Insensitive, and treat this as a regular Expression. Technically there are no letters being searched, so the i is irrelevant, but I always use it in my searches anyway.
^ start of line
\s* white space, any amount from zero onward. This is a greedy search, so it will match all the white space (spaces, tabs, etc.)
(#|;) either a pound or a semicolon, which usually denote comments in config files (in my case, smb.conf)
((#|;).*) the above sentence, followed by any character (the period), and any amount of those “any characters.”
((#|;).*)? the whole thing in parentheses shown here, optionally.
$ end of line
So any line that starts with any amount of white space, followed by (a comment symbol, followed by anything else) optionally, and the end of the line.
So show everything but the above sentence, and tada, just the important stuff.

Replace config file entry or append if not found

Suppose you need to replace whatever current setting for a particular application to a specific setting. It doesn’t matter what it was before, or if it was absent. You want to define it.

Suppose you want to replace the session pam_mkhomedir setting in /etc/pam.d/common-session to be required. If it was optional, replace it. If it wasn’t listed at all, add it. This line updates an answer on stackoverflow.

sed -i -e '\|session.*pam_mkhomedir.so|h; ${x;s/mkhomedir//;{g;tF};a\' -e 'session\trequired\tpam_mkhomedir.so umask=0022' -e '};:F;s/.*mkhomedir.*/session\trequired\tpam_mkhomedir.so umask=0022/g;' /etc/pam.d/common-session

The -i makes the change inline directly in the file. I write deployment scripts so I don’t care about displaying the output; I want to change the original.

Basically the command looks like this:

sed -e '\|variable.*(possible|values|go|hereornot)|h; ${x;s/variable//;{g;tF};a\' -e 'variable updatedvalue1 updatedvalue2' -e '};:F;s/.*variable.*/variable thisvalueadded1/g;' /etc/inputfile

Questions? I would be happy to explain this if you want a guided tour.