Automatically Create a User on Linux Boot

Introduction

The project development requirement is to automatically create a user with a password after booting.

The provided SDK only has the adduser command.

This command requires terminal interaction when adding a user.

It requires manually entering the password twice,

so it cannot be created simply through a script.

To achieve automatic password filling, we can use the expect tool, but this tool requires the tcl library for portability.

Additionally, since the product’s storage space is quite limited, following the principle of saving resources,

Professor Peng chose another method,

which is to directly modify the shadow and passwd files of the root filesystem.

Environment

sdk: Fudan Micro
Buildroot 2018.02.3

Steps

1. Manually Add User with adduser Command

Manually add user ftp on the development board, password: 123456

# adduser ftp
adduser: /home/ftp: No such file or directory
Changing password for ftp
New password: 
Bad password: too weak
Retype password: 
passwd: password for ftp changed by root
Automatically Create a User on Linux Boot

After adding the user, the command modifies the files

/etc/shadow
/etc/passwd

At the bottom of these two files, user ftp related information will be added.

  • /etc/shadow
# cat /etc/shadow
…………
ftp:$1$AQb2UA9p$mX8fQskQJcHm0t4QlVk4A1:0:0:99999:7:::
  • /etc/passwd
# cat /etc/passwd
…………
ftp:x:1000:1000:Linux User,,,:/home/ftp:/bin/sh

2. Configure User Information to rootfs

Add this information to the buildroot directory of the SDK provided by the manufacturer:

/home/peng/xxx/work/xxx/sdk/buildroot-2018.02.3/output/target/etc
peng@ubuntu:~/work/xxx/sdk/buildroot-2018.02.3/output/target/etc$ ls passwd shadow -l
-rwxr--r-- 1 peng peng 339 Nov 15 17:24 passwd
-rwx------ 1 peng peng 242 Nov 15 17:24 shadow

Modify these two files with administrator privileges:

  • shadow
Automatically Create a User on Linux Boot
  • passwd
Automatically Create a User on Linux Boot

Compile, burn, and restart to log in with the ftp user.

Note

If you cannot log in after modifying the user password, it may be related to the default system time; if the system time is too old (1970), it may cause the password to be invalid.

Automatically Create a User on Linux BootTherefore, you must manually set the system time using the date command:

date -s '2024-11-23 15:12'

You can place this command in the system’s auto-start script for it to run automatically at boot:

sdk\buildroot-2018.02.3\output\target\etc\init.d\rcS

Autumn recruitment has ended, and if everyone does not prepare adequately, it will be difficult to find a good job in the spring recruitment.

Here’s a job package for everyone, you can take a crash course for the spring recruitment and find a good job!

Automatically Create a User on Linux Boot

Leave a Comment