Search code examples
sshcentoslinode

Unable to ssh into a newly created user on Centos on first attempt


this is what I am doing:

  1. Creating a new server on Linode. OS is centos 6.5
  2. Logging in as root
  3. running the following script to add a user called shortfellow which does not have a password.

The script is:

#!/bin/bash

yum -y update
adduser shortfellow

mkdir -p /home/shortfellow/.ssh
echo "ssh-rsa REALLYLONGSSHPUBLICKEY [email protected]" >> /home/shortfellow/.ssh/authorized_keys

chmod -R 700 /home/shortfellow/.ssh
chown -R shortfellow:shortfellow /home/shortfellow/.ssh
su - shortfellow
exit

The problem is that first time when I try to ssh into the system. It does not work at all. It simply asks for the password. I hit ctrl + c and try to ssh again as the same user, it works.

this behaviour is really annoying because I am writing code to create the server programmatically and it does not work because of this silly issue.

Does anyone have any idea why this might not be working as expected?


Solution

  • I did /sbin/mkhomedir_helper shortfellow in the script before the exit and it works correctly after that.

    I guess the issue was really that the home directory for a user is created only at login and when I tried to programmatically create this user this would not happen for some reason.