DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Group Creation and User Assignment

The system admin team at xFusionCorp Industries has streamlined access management by implementing group-based access control. Here's what you need to do:

a. Create a group named nautilus_sftp_users across all App servers within the Stratos Datacenter.

b. Add the user mohammed into the nautilus_sftp_users group on all App servers. If the user doesn't exist, create it as well.


Solution: Interactive Login Method (Recommended)

Since you're on the jump host, connect to each server interactively:

App Server 1 (stapp01) - User: tony

ssh tony@stapp01
# Password: Ir0nM@n

# Now inside stapp01, switch to root (you'll need tony's password again)
sudo su -
# Password: Ir0nM@n

# Create group and add user
groupadd nautilus_sftp_users
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed

# Verify
groups mohammed
grep nautilus_sftp_users /etc/group

# Exit back
exit
exit
Enter fullscreen mode Exit fullscreen mode

App Server 2 (stapp02) - User: steve

ssh steve@stapp02
# Password: Am3ric@

sudo su -
# Password: Am3ric@

groupadd nautilus_sftp_users
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed

groups mohammed
grep nautilus_sftp_users /etc/group

exit
exit
Enter fullscreen mode Exit fullscreen mode

App Server 3 (stapp03) - User: banner

ssh banner@stapp03
# Password: BigGr33n

sudo su -
# Password: BigGr33n

groupadd nautilus_sftp_users
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed

groups mohammed
grep nautilus_sftp_users /etc/group

exit
exit
Enter fullscreen mode Exit fullscreen mode

Alternative: Using sudo -S with Password

If you want to run it as a one-liner from jump host:

For stapp01 (tony):

echo 'Ir0nM@n' | ssh tony@stapp01 "sudo -S groupadd nautilus_sftp_users 2>/dev/null; echo 'Ir0nM@n' | sudo -S useradd -G nautilus_sftp_users mohammed 2>/dev/null || echo 'Ir0nM@n' | sudo -S usermod -a -G nautilus_sftp_users mohammed; echo 'Ir0nM@n' | sudo -S groups mohammed; echo 'Ir0nM@n' | sudo -S grep nautilus_sftp_users /etc/group"
Enter fullscreen mode Exit fullscreen mode

For stapp02 (steve):

echo 'Am3ric@' | ssh steve@stapp02 "sudo -S groupadd nautilus_sftp_users 2>/dev/null; echo 'Am3ric@' | sudo -S useradd -G nautilus_sftp_users mohammed 2>/dev/null || echo 'Am3ric@' | sudo -S usermod -a -G nautilus_sftp_users mohammed; echo 'Am3ric@' | sudo -S groups mohammed; echo 'Am3ric@' | sudo -S grep nautilus_sftp_users /etc/group"
Enter fullscreen mode Exit fullscreen mode

For stapp03 (banner):

echo 'BigGr33n' | ssh banner@stapp03 "sudo -S groupadd nautilus_sftp_users 2>/dev/null; echo 'BigGr33n' | sudo -S useradd -G nautilus_sftp_users mohammed 2>/dev/null || echo 'BigGr33n' | sudo -S usermod -a -G nautilus_sftp_users mohammed; echo 'BigGr33n' | sudo -S groups mohammed; echo 'BigGr33n' | sudo -S grep nautilus_sftp_users /etc/group"
Enter fullscreen mode Exit fullscreen mode

Better Alternative: Using a Heredoc

This approach is cleaner and more readable:

For stapp01:

ssh tony@stapp01 << 'EOF'
echo 'Ir0nM@n' | sudo -S bash -c '
groupadd nautilus_sftp_users 2>/dev/null
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed
groups mohammed
grep nautilus_sftp_users /etc/group
'
EOF
Enter fullscreen mode Exit fullscreen mode

For stapp02:

ssh steve@stapp02 << 'EOF'
echo 'Am3ric@' | sudo -S bash -c '
groupadd nautilus_sftp_users 2>/dev/null
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed
groups mohammed
grep nautilus_sftp_users /etc/group
'
EOF
Enter fullscreen mode Exit fullscreen mode

For stapp03:

ssh banner@stapp03 << 'EOF'
echo 'BigGr33n' | sudo -S bash -c '
groupadd nautilus_sftp_users 2>/dev/null
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed
groups mohammed
grep nautilus_sftp_users /etc/group
'
EOF
Enter fullscreen mode Exit fullscreen mode

Complete Script (Run from Jump Host)

Create a script file on the jump host:

#!/bin/bash
# Save as setup_groups.sh

echo "Setting up group and user on all App servers..."

# stapp01
echo "=== Processing stapp01 ==="
ssh tony@stapp01 << 'EOF'
echo 'Ir0nM@n' | sudo -S bash -c '
echo "Creating group nautilus_sftp_users..."
groupadd nautilus_sftp_users 2>/dev/null && echo "✓ Group created" || echo "✓ Group already exists"

echo "Adding user mohammed..."
if id mohammed &>/dev/null; then
    usermod -a -G nautilus_sftp_users mohammed
    echo "✓ User mohammed added to group"
else
    useradd -G nautilus_sftp_users mohammed
    echo "✓ User mohammed created and added to group"
fi

echo "Verification:"
groups mohammed
grep nautilus_sftp_users /etc/group
'
EOF

echo ""

# stapp02
echo "=== Processing stapp02 ==="
ssh steve@stapp02 << 'EOF'
echo 'Am3ric@' | sudo -S bash -c '
echo "Creating group nautilus_sftp_users..."
groupadd nautilus_sftp_users 2>/dev/null && echo "✓ Group created" || echo "✓ Group already exists"

echo "Adding user mohammed..."
if id mohammed &>/dev/null; then
    usermod -a -G nautilus_sftp_users mohammed
    echo "✓ User mohammed added to group"
else
    useradd -G nautilus_sftp_users mohammed
    echo "✓ User mohammed created and added to group"
fi

echo "Verification:"
groups mohammed
grep nautilus_sftp_users /etc/group
'
EOF

echo ""

# stapp03
echo "=== Processing stapp03 ==="
ssh banner@stapp03 << 'EOF'
echo 'BigGr33n' | sudo -S bash -c '
echo "Creating group nautilus_sftp_users..."
groupadd nautilus_sftp_users 2>/dev/null && echo "✓ Group created" || echo "✓ Group already exists"

echo "Adding user mohammed..."
if id mohammed &>/dev/null; then
    usermod -a -G nautilus_sftp_users mohammed
    echo "✓ User mohammed added to group"
else
    useradd -G nautilus_sftp_users mohammed
    echo "✓ User mohammed created and added to group"
fi

echo "Verification:"
groups mohammed
grep nautilus_sftp_users /etc/group
'
EOF

echo ""
echo "✅ Setup completed on all App servers!"
Enter fullscreen mode Exit fullscreen mode

Make it executable and run:

chmod +x setup_groups.sh
./setup_groups.sh
Enter fullscreen mode Exit fullscreen mode

Quickest Method: Interactive Session

If you prefer the simplest approach, just run:

# Connect to each server and run commands manually
ssh tony@stapp01
sudo su -
groupadd nautilus_sftp_users
useradd -G nautilus_sftp_users mohammed 2>/dev/null || usermod -a -G nautilus_sftp_users mohammed
groups mohammed
exit
exit

# Repeat for stapp02 and stapp03 with their respective users and passwords
Enter fullscreen mode Exit fullscreen mode

The interactive method is recommended since it handles password prompts properly and you can see the output clearly.

Top comments (0)