Linux script to create new user

Hi Guys,

As lately I’ve started my jorney to Linux world. I will share some nice script to add new users, which was shared to me by friend of mine.

To configure remote access, you need to change the configuration of ssh-server. In the file /etc/ssh/sshd_config, you must configure the following settings:

ListenAddress 10.10.x.y
PermitRootLogin no
PermitEmptyPasswords no
PasswordAuthentication no

Before adding users to the server, you need to create a group:

"admins":groupadd - g 500 admins

You must also configure sudo: create a file /etc/sudoers.d/remote_ssh with the content:

{e446ef79be674adb3c6945ce128768d37101f21a4bf2301a6de0b74a3fac67b5}admins ALL = (ALL) NOPASSWD: ALL

In the directory /root create subdirectory “scripts” and create a file “newuser.sh” with the content below:

For Debian:

#!/bin/sh
username=$1
comment=$2
if[ "$username"= ""]; then
error=1
fi
if[ "$comment"= ""]; then
error=1
fi
if[ $error ]; then
echo"usage: `basename $0` username 'fullname'"
exit
fi
useradd-d /home/$username -c "$comment"-m "$username"
su-m "$username"-c "mkdir -m 0700 /home/$username/.ssh/"
su-m "$username"-c "touch /home/$username/.ssh/authorized_keys"
adduser $username admins

For CentOS

#!/bin/bash
username=$1
comment=$2
if [ "$username" = "" ]; then
error=1
fi
if [ "$comment" = "" ]; then
error=1
fi
if [ $error ]; then
echo "usage: `basename $0` username 'fullname'"
exit
fi
useradd -d /home/$username -c "$comment" "$username"
su -m "$username" -c "mkdir -m 0700 /home/$username/.ssh/"
su -m "$username" -c "touch /home/$username/.ssh/authorized_keys"
usermod -G admins $username

Permissions to this file must be changed to 700:

chmod 700 /root/scripts/newuser.sh

Regards,

Denis.

2 thoughts on “Linux script to create new user

  1. Johnd649

    I really enjoy the blog article.Really looking forward to read more. Fantastic.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.