0
0
Raspberry Piprogramming~5 mins

User authentication basics in Raspberry Pi

Choose your learning style9 modes available
Introduction

User authentication helps check who you are before letting you use a device or service. It keeps things safe by making sure only the right people can get in.

When you want to protect your Raspberry Pi from strangers.
When you need to log in to your Raspberry Pi remotely.
When you want to control who can run certain programs on your Raspberry Pi.
When you want to keep your files private on your Raspberry Pi.
Syntax
Raspberry Pi
username = input('Enter username: ')
password = input('Enter password: ')

if username == 'admin' and password == '1234':
    print('Access granted')
else:
    print('Access denied')

This is a simple way to check username and password in Python on Raspberry Pi.

In real use, passwords should be stored safely, not in plain text.

Examples
Checks if the username and password match fixed values.
Raspberry Pi
username = input('Username: ')
password = input('Password: ')

if username == 'user1' and password == 'pass1':
    print('Welcome user1!')
else:
    print('Try again.')
Uses a dictionary to store multiple users and their passwords.
Raspberry Pi
users = {'alice': 'apple', 'bob': 'banana'}

username = input('Username: ')
password = input('Password: ')

if username in users and users[username] == password:
    print(f'Hello, {username}!')
else:
    print('Wrong login.')
Sample Program

This program asks for username and password, then checks if they match stored users. It prints a welcome message if correct, or denies access if not.

Raspberry Pi
def authenticate():
    users = {'pi': 'raspberry', 'guest': '1234'}
    username = input('Enter username: ')
    password = input('Enter password: ')

    if username in users and users[username] == password:
        print(f'Access granted. Welcome, {username}!')
    else:
        print('Access denied. Please try again.')

if __name__ == '__main__':
    authenticate()
OutputSuccess
Important Notes

Never store passwords as plain text in real projects; use hashing for safety.

Always test your authentication code to avoid mistakes that lock you out.

Summary

User authentication checks who you are before giving access.

Simple programs compare typed username and password to stored ones.

Keep passwords safe and never share them openly.