sudo
sudo
(Super User Do) is a command used in Linux and Unix systems to execute commands with super user (root) privileges. It allows ordinary users to run specific commands as a privileged user, usually requiring a password to confirm their identity.
sudo
is a safe way to limit which users can perform privileged operations to reduce potential system damage. Typically only available to system administrators or users who need it sudo
.
Here are some common uses of the sudo
command:
- Run a single command as superuser (root):
sudo command_to_run
For example, to install software with superuser privileges, you would run:
sudo apt-get install package_name
- Switch to superuser (root) mode:
sudo -i
This will open a new shell session that will run all commands as superuser. To exit superuser mode, just enter exit
.
- edit system connection:
sudo nano /etc/file_to_edit
This will open the file for editing with superuser rights using a text editor. You can use vim
, nano
or another text editor.
- Management System Services:
sudo systemctl start|stop|restart service_name
This will start, stop or restart specific system services.
- Manage users and groups:
sudo useradd new_username
sudo userdel existing_username
sudo usermod -aG group_name username
These commands are used to manage users and groups, and to add users to groups.
Be careful when using sudo
because users with superuser privileges can troubleshoot and make changes to the system, and incorrect actions can cause system instability. When using sudo
, make sure you know what you are doing and read the commands you are running carefully to avoid unnecessary risks.
useradd
useradd
command to create a new user, but it does not automatically assign a home directory or set a password for the user. The following is the basic syntax for creating a user using the useradd
command:
sudo useradd [options] username
options
: Can include some options for specifying user details, such as home directory, default Shell, etc.username
: The username of the new user to be created.
Here are some commonly used useradd
options:
-m
: Automatically create a home directory for the user.-s
: Specifies the user's default Shell. For example,-s /bin/bash
will set the user's default shell to Bash.-G
: Specify additional groups that the user wants to join. Multiple groups can be separated by commas.-d
: Specify the user's home directory path.-p
: Specify the user’s encrypted password. The password is usually set using thepasswd
command rather than specifying the password directly in theuseradd
command.
Here is an example that creates a new user named "newuser" and assigns it a home directory:
sudo useradd -m newuser
Next, use the passwd
command to set a password for the new user:
sudo passwd newuser
Enter your new password and confirm your password again.
If you need to add the new user to another group, you can use the -G
option:
sudo useradd -m -G group1,group2 newuser
Once a new user is created, the new user will be able to log in to the system using their username and password, as well as access their home directory. Please note that you should be careful when managing users and passwords to ensure password strength and security.
passwd
passwd
is a command used to change a user's password, or a user with administrator rights can change the password of another user. Here are some common uses of the passwd
command:
- Change the current user's password:
Users can use the passwd
command to change their password. In the terminal, just type:
passwd
Then follow the prompts to enter your current password and then enter your new password twice.
- Change another user's password:
If you have administrator rights, you can use the passwd
command to change other users' passwords. In the terminal, use the following command:
sudo passwd username
where username
is the username of the user whose password you want to change. Then follow the prompts and enter your new password twice.
- Forcing users to change their password:
If you need to force the user to change their password the next time they log in, use the following command:
sudo passwd -e username
This will cause the user's password to expire on the next login and the user will be asked to change their password immediately.
- View user password policy:
Use the following command to view password policy details:
sudo chage -l username
This will display information such as password expiration date, minimum and maximum password age, and more.
- Lock user account:
Use the following command to lock a user account so that the user cannot log in:
sudo passwd -l username
To unlock a user account, use the following command:
sudo passwd -u username
chmod
chmod
Command used to change file or directory permissions. Used to assign or change access permissions on a file to determine which users or groups can read, write, or execute the file. chmod
The command can operate in symbolic mode or octal mode. Here are some common chmod
command uses:
Symbol mode:
-
Addition limit:
chmod +[权限] [文件名]
For example, to add execute permission to file
file.txt
, you can use:chmod +x file.txt
-
删外权limit:
chmod -[权限] [文件名]
For example, to remove write permission from file
file.txt
, you would use:chmod -w file.txt
-
Set permissions:
chmod [权限] [文件名]
For example, to set permissions on file
file.txt
to both read and write, use:chmod rw file.txt
-
Set multiple permissions at the same time:
chmod [权限1][权限2] [文件名]
For example, to set read and execute permissions for file
file.txt
, use:chmod rx file.txt
Octal mode:
-
Distribution limit:
chmod [八进制权限] [文件名]
In octal mode, each permission is represented by a number, as follows:
- 4: Read permission
- 2: Write permission
- 1: Execute permissions
For example, to set the permissions on file
file.txt
to read and write, use:chmod 600 file.txt
-
Please change the limit:
To recursively change the permissions of a directory and its subdirectories, use the
-R
or--recursive
option:chmod -R [权限] [目录名]
For example, to recursively set permissions on files in directory
mydir
and all its subdirectories to read-only, use:chmod -R 444 mydir
Be careful when changing permissions on files or directories to ensure that you do not compromise the security of your system or files. Normally, only the system administrator or the owner of the file should have the right to change permissions.
chown
chown
Command for changing the owner and group of a file or directory. Assign owner (user) and group (group) permissions to the file. Here are some common chown
command uses:
Change owner:
sudo chown [新用户] [文件名/目录名]
新用户
Is the user name to assign ownership of the file or directory to.
For example, to change the ownership of file file.txt
to user newuser
, use:
sudo chown newuser file.txt
Change group:
sudo chown :[新组] [文件名/目录名]
新组
Is the name of the group to which to assign group permissions for the file or directory.
For example, to change the group permissions of file file.txt
to group newgroup
, you would use:
sudo chown :newgroup file.txt
Change owner and group:
sudo chown [新用户]:[新组] [文件名/目录名]
新用户
Is the username of the new file or directory owner.新组
Is the group name of the new file or directory group.
For example, to change the owner of filefile.txt
to newuser
and the group to newgroup
, you would use :
sudo chown newuser:newgroup file.txt
Change permissions recursively:
To recursively change the owner and group of files in a directory and its subdirectories, use the -R
or --recursive
option:
sudo chown -R [新用户]:[新组] [目录名]
For example, to recursively set the owner and group of files in directory mydir
and all its subdirectories to newuser
and newgroup
, you can use:
sudo chown -R newuser:newgroup mydir
Please use the chown
command with caution and ensure you have sufficient permissions to change the owner and group of the file or directory. Normally, only the system administrator or the owner of the file should have the rights to change the owner and group.
groups
groups
The command is used to check which user groups (group names) the current user belongs to. In Unix, Linux and Unix-like systems, each user can belong to one or more user groups at the same time.
To see which user groups the current user belongs to, just enter the following command in the terminal:
groups
The system will list all user groups to which the current user belongs.
For example, if the output contains the following:
user adm cdrom sudo dip plugdev lpadmin sambashare
This means that useruser
also belongs to adm
, cdrom
, sudo
, < /span> user groups. Each group usually has specific permissions and access controls in the system. , and , dip
, plugdev
lpadmin
sambashare
Use the id
command to view the user's details, including the user's UID (User Identifier), GID (Group Identifier), primary group, and other additional groups:
id
This will display detailed information including users and groups.
su
su
is used to switch user identities. Usually, it is used to switch from the current user to another user, usually the super user (root) or other ordinary users. Here is the basic usage of the su
command:
Switch to superuser (root):
su
When you run the su
command without specifying a user to switch to, by default it will attempt to switch to superuser (root) identity. You need to enter the superuser password to obtain superuser privileges. On some systems, explicit use of su -
is required to obtain a full superuser environment.
su -
Switch to another user:
Use the su
command to switch to the identity of another ordinary user. For example, to switch to user newuser
, run:
su newuser
Then, enter the newuser
user's password to gain their permissions.
Switch users and start the shell:
Normally, thesu
command can also be used to switch users and start a new shell session to execute a series of commands. For example:
su - newuser -c "command_to_run"
This will switch to user newuser
and run the specified command.
su
The command usually requires superuser privileges, so a superuser password is required. On some systems, the user account must be configured in the superuser's (root) /etc/sudoers
file to allow use of the su
command. Additionally, use of the su
command may be restricted by system and security policies. Typically, it is recommended to use sudo
to perform privileged operations, as it allows for more granular permission control.