-1

I wanted to start the ssh agent the moment I login to my Ubuntu machine. So I followed http://mah.everybody.org/docs/ssh and appended the following piece of code in my ~/.profile.

#
# setup ssh-agent
#
# set environment variables if user's agent already exists
[ -z "$SSH_AUTH_SOCK" ] && SSH_AUTH_SOCK=$(ls -l /tmp/ssh-*/agent.* 2> /dev/null | grep $(whoami) | awk '{print $9}')
[ -z "$SSH_AGENT_PID" -a -z `echo $SSH_AUTH_SOCK | cut -d. -f2` ] && SSH_AGENT_PID=$((`echo $SSH_AUTH_SOCK | cut -d. -f2` + 1))
[ -n "$SSH_AUTH_SOCK" ] && export SSH_AUTH_SOCK
[ -n "$SSH_AGENT_PID" ] && export SSH_AGENT_PID

# start agent if necessary
if [ -z $SSH_AGENT_PID ] && [ -z $SSH_TTY ]; then  # if no agent & not in ssh
  eval `ssh-agent -s` > /dev/null
fi

# setup addition of keys when needed
if [ -z "$SSH_TTY" ] ; then                     # if not using ssh
  ssh-add -l > /dev/null                        # check for keys
  if [ $? -ne 0 ] ; then
    alias ssh='ssh-add -l > /dev/null || ssh-add && unalias ssh ; ssh'
    if [ -f "/usr/lib/ssh/x11-ssh-askpass" ] ; then
      SSH_ASKPASS="/usr/lib/ssh/x11-ssh-askpass" ; export SSH_ASKPASS
    fi
  fi
fi

Now when I login to unity session it gives me a nasty error pop-up indicating line 30 in ~/.profile which is:

[ -z "$SSH_AGENT_PID" -a -z `echo $SSH_AUTH_SOCK | cut -d. -f2` ] && SSH_AGENT_PID=$((`echo $SSH_AUTH_SOCK | cut -d. -f2` + 1))

according to my text editor. I tried to search for a solution and amusingly this Start ssh-agent on login SO answer points to the same website but another solution which requires .bash_profile. Problem is that my ~/.profile will be ignored if a ~/.bash_profile or ~/.bash_login exists which I don't want as the .profile contains some more important initialization. I don't understand what's wrong with this expression?

Black_Zero
  • 345
  • 1
  • 4
  • 11

1 Answers1

0

The problem is the -a parameter in the first [].

Rewrite it like this and it will work (still doing the same thing):

[ -z "$SSH_AGENT_PID" ] && [ -z `echo $SSH_AUTH_SOCK | cut -d. -f2` ] && SSH_AGENT_PID=$((`echo $SSH_AUTH_SOCK | cut -d. -f2` + 1))
  • Thanks for the input but now I am getting another error. Here is the snap of the error msg https://imagebin.ca/v/3U7X7ex2JKLb and here (https://pastebin.com/WNLbJP0y) is my .profile – Black_Zero Jul 21 '17 at 09:43
  • Have you tried letting gdm handle the startup? This way is also written in your tutorial: http://prntscr.com/fyh2py – Lukas Isselbächer Jul 21 '17 at 11:23
  • I think unity uses _lightdm_ and there is no `.xsession` file in my `$HOME`. – Black_Zero Jul 24 '17 at 09:30