DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Linux User Setup with Non-Interactive Shell

To accommodate the backup agent tool's specifications, the system admin team at xFusionCorp Industries requires the creation of a user with a non-interactive shell. Here's your task:

Create a user named john with a non-interactive shell on App Server 1.


Solution

Step 1: Connect to App Server 1 (stapp01)

From the jump host or directly:

ssh tony@stapp01
# Password: Ir0nM@n
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to root or use sudo

sudo su -
# Password: Ir0nM@n
Enter fullscreen mode Exit fullscreen mode

Step 3: Create user with non-interactive shell

Use the useradd command with the -s option to specify the shell:

useradd -s /sbin/nologin john
Enter fullscreen mode Exit fullscreen mode

Alternative shells for non-interactive users:

  • /sbin/nologin - Most common (disallows login completely)
  • /bin/false - Another option that returns false immediately
  • /usr/sbin/nologin - Similar to /sbin/nologin

Step 4: Verify the user creation

# Check user details
id john

# Verify the shell is set correctly
grep john /etc/passwd

# Check that user cannot login interactively
su - john
# Should show: This account is currently not available.
Enter fullscreen mode Exit fullscreen mode

Complete One-Line Commands

From jump host (with password):

echo 'Ir0nM@n' | ssh tony@stapp01 "sudo -S useradd -s /sbin/nologin john 2>/dev/null && echo 'User john created successfully' || echo 'User john already exists'; sudo -S grep john /etc/passwd"
Enter fullscreen mode Exit fullscreen mode

From jump host using heredoc:

ssh tony@stapp01 << 'EOF'
echo 'Ir0nM@n' | sudo -S bash -c '
useradd -s /sbin/nologin john 2>/dev/null && echo "✓ User john created successfully" || echo "✓ User john already exists"
echo "Verification:"
grep john /etc/passwd
'
EOF
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Interactive Commands

# Connect to stapp01
ssh tony@stapp01
# Enter password: Ir0nM@n

# Become root
sudo su -
# Enter password: Ir0nM@n

# Create user with non-interactive shell
useradd -s /sbin/nologin john

# Verify
grep john /etc/passwd

# Expected output:
# john:x:[UID]:[GID]::/home/john:/sbin/nologin

# Test the shell (should fail)
su - john
# Output: This account is currently not available.

# Exit back
exit
exit
Enter fullscreen mode Exit fullscreen mode

Additional Options

Create user with specific home directory (optional):

useradd -s /sbin/nologin -d /var/www/john john
Enter fullscreen mode Exit fullscreen mode

Create user with specific UID (optional):

useradd -s /sbin/nologin -u 1615 john
Enter fullscreen mode Exit fullscreen mode

Create user without home directory:

useradd -s /sbin/nologin -M john
Enter fullscreen mode Exit fullscreen mode

Verification Commands

Run these to confirm everything is correct:

# Check user exists
id john

# Check shell in /etc/passwd
grep john /etc/passwd | cut -d: -f7
# Should output: /sbin/nologin

# Check if shell is non-interactive
getent passwd john | awk -F: '{print $7}'
# Should output: /sbin/nologin

# Verify user can't login
su - john -c "echo test"
# Should fail or return nothing
Enter fullscreen mode Exit fullscreen mode

Expected Output

[root@stapp01 ~]# useradd -s /sbin/nologin john
[root@stapp01 ~]# grep john /etc/passwd
john:x:1003:1003::/home/john:/sbin/nologin
[root@stapp01 ~]# su - john
This account is currently not available.
[root@stapp01 ~]#
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. "useradd: user 'john' already exists": The user already exists, but we can modify their shell:
   usermod -s /sbin/nologin john
Enter fullscreen mode Exit fullscreen mode
  1. "Permission denied": Ensure you're using sudo or root:
   sudo useradd -s /sbin/nologin john
Enter fullscreen mode Exit fullscreen mode
  1. "useradd: cannot lock /etc/passwd": Another process is using the password file, wait and retry.

Why Non-Interactive Shell?

  • Security: Prevents the user from logging in interactively
  • Service Accounts: Ideal for backup agents, daemons, and automation tools
  • SFTP/SCP Only: Can be used with SSH for file transfers only
  • Application Specific: Good for users that only need to run specific commands

Complete Solution Summary

The user john has been created on App Server 1 (stapp01) with:

  • ✅ Username: john
  • ✅ Non-interactive shell: /sbin/nologin
  • ✅ Home directory: /home/john (default)
  • ✅ Verified that interactive login is disabled

This satisfies the backup agent tool's requirement for a user with a non-interactive shell.

Top comments (0)