Unix / Linux - 使用者管理

Hello there, aspiring Linux enthusiasts! As your friendly neighborhood computer science teacher, I'm thrilled to guide you through the exciting world of Unix/Linux user administration. Don't worry if you're new to programming – we'll start from the basics and work our way up. By the end of this tutorial, you'll be managing users and groups like a pro!

Unix / Linux - User Administration

管理使用者與群組

Before we dive into the nitty-gritty, let's understand why user and group management is crucial. Imagine a shared computer in a busy office – you wouldn't want everyone to access everything, right? That's where user administration comes in handy. It helps maintain security, organize resources, and keep things running smoothly.

了解使用者與群組

In Unix/Linux systems, a user is an entity that can log in and perform operations. A group is a collection of users with similar permissions. Think of it like a school – students are users, and classes are groups. Now, let's roll up our sleeves and get started!

創建群組

Creating a group is like forming a new club. It's simple and fun! Here's how you do it:

sudo groupadd developers

This command creates a new group called "developers". The sudo at the beginning gives you superuser privileges – it's like having a master key!

Let's break it down:

  • sudo: Superuser do (gives admin privileges)
  • groupadd: The command to add a new group
  • developers: The name of our new group

修改群組

Sometimes, you might need to change things about a group. Maybe you want to rename the "developers" group to "coders". Here's how:

sudo groupmod -n coders developers

This command renames the "developers" group to "coders".

Explanation:

  • groupmod: The command to modify a group
  • -n: Option for new name
  • coders: The new name
  • developers: The old name

删除群組

Oops! Did you create a group by mistake? No worries, we can delete it:

sudo groupdel coders

This command deletes the "coders" group.

Breakdown:

  • groupdel: The command to delete a group
  • coders: The name of the group to delete

創建帳號

Now, let's create a user account. It's like registering a new student in school:

sudo useradd -m -g developers -s /bin/bash john

This command creates a new user named "john" and adds him to the "developers" group.

Let's dissect this command:

  • useradd: The command to add a new user
  • -m: Creates a home directory for the user
  • -g developers: Adds the user to the "developers" group
  • -s /bin/bash: Sets the user's default shell to bash
  • john: The username for the new account

修改帳號

People change, and so do user accounts. Let's modify John's account:

sudo usermod -l johnny -d /home/johnny -m john

This command changes John's username to "johnny" and updates his home directory.

Here's what each part does:

  • usermod: The command to modify a user account
  • -l johnny: Changes the login name to "johnny"
  • -d /home/johnny: Sets the new home directory
  • -m: Moves the contents of the old home directory to the new one
  • john: The current username

删除帳號

Sometimes, we need to say goodbye. Here's how to delete a user account:

sudo userdel -r johnny

This command deletes the user "johnny" and his home directory.

Explanation:

  • userdel: The command to delete a user
  • -r: Removes the user's home directory and mail spool
  • johnny: The username to delete

便捷的使用者管理命令

Here's a table of the commands we've learned, for quick reference:

命令 描述 示例
groupadd 創建新群組 sudo groupadd developers
groupmod 修改群組 sudo groupmod -n coders developers
groupdel 删除群組 sudo groupdel coders
useradd 創建新使用者帳號 sudo useradd -m -g developers -s /bin/bash john
usermod 修改使用者帳號 sudo usermod -l johnny -d /home/johnny -m john
userdel 删除使用者帳號 sudo userdel -r johnny

Remember, with great power comes great responsibility. Always double-check before executing these commands, especially when deleting accounts or groups!

結論

Congratulations! You've just taken your first steps into the world of Unix/Linux user administration. It might seem overwhelming at first, but with practice, you'll be managing users and groups like a seasoned system administrator.

As we wrap up, here's a little story from my early days of teaching: I once had a student who accidentally deleted his own user account while practicing. Panic ensued until we realized it was a valuable lesson in always having backups and being careful with admin commands. So, don't be afraid to experiment, but always be cautious and keep backups!

Keep exploring, stay curious, and happy administrating!

Credits: Image by storyset