linux如何添加一新用户stu33,指定其组为stu,并设置其用户主目录为/data/stu33,密码为linux33,账号永不过期
时间: 2023-12-03 12:45:56 浏览: 83
可以使用以下命令来添加新用户:
```
sudo useradd -m -d /data/stu33 -s /bin/bash -g stu -p $(openssl passwd -1 linux33) -U -N stu33
```
解释一下各个参数的含义:
- `-m`:创建用户主目录。如果不加此参数,则不会自动创建主目录。
- `-d /data/stu33`:指定用户主目录为 `/data/stu33`。
- `-s /bin/bash`:指定用户的默认shell为 `/bin/bash`。
- `-g stu`:将用户加入到组 `stu` 中。
- `-p $(openssl passwd -1 linux33)`:指定用户的密码为 `linux33`。这里使用了 `openssl` 命令来生成加密后的密码。
- `-U`:创建一个与用户名同名的组。
- `-N stu33`:不创建与用户名同名的组。
最后,账号永不过期是默认设置的,不需要额外指定。
相关问题
编写Linux C程序,列出stu2主目录中的文件信息,并将其中saturday.txt文件的权限设置为文件所有者可读可写、同组用户只读、其他用户无权限。
您可以使用Linux系统的系统调用函数来实现这个功能,下面是一个简单的示例程序:
```c
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
int main() {
char *dir_path = "/home/stu2"; // stu2主目录的路径
DIR *dir = opendir(dir_path); // 打开目录
if (dir == NULL) {
perror("opendir failed");
return -1;
}
struct dirent *dp;
while ((dp = readdir(dir)) != NULL) { // 遍历目录
if (dp->d_name[0] == '.') continue; // 忽略隐藏文件
printf("%s\n", dp->d_name);
}
closedir(dir); // 关闭目录
char *file_path = "/home/stu2/saturday.txt"; // saturday.txt的路径
struct stat st;
if (stat(file_path, &st) != 0) { // 获取文件状态
perror("stat failed");
return -1;
}
mode_t new_mode = (st.st_mode & ~S_IWOTH) | S_IRUSR | S_IWUSR | S_IRGRP; // 新的文件权限
if (chmod(file_path, new_mode) != 0) { // 设置文件权限
perror("chmod failed");
return -1;
}
return 0;
}
```
需要注意的是,这个程序需要以root权限运行才能修改文件权限。
(1)创建一个名为“student”,登录名为“net-stu”的用户账户,并设置其密码为net-stu-XX(XX为学号)。
要在 Linux 系统中创建一个名为“student”,登录名为“net-stu”的用户账户,并设置其密码为“net-stu-XX”(XX 为学号),可以使用以下命令:
```
sudo useradd -m -s /bin/bash -c "Net Student" -p $(openssl passwd -1 net-stu-XX) net-stu
```
其中,“-m” 选项表示同时创建用户的主目录,“-s” 选项表示指定用户的默认 shell,“-c” 选项表示指定用户的注释。注释可以包含有关用户的任何信息,例如姓名、职位等。密码使用 `openssl passwd` 命令生成,`-1` 选项表示使用 MD5 加密算法。将“XX”替换为实际的学号。
需要注意的是,创建用户的命令需要使用 `sudo` 或者以 root 用户身份执行。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)