Renaming user in linux

How can I rename a Unix user?

I have a user named hedgehog and I want him to be named squirrel , but I don’t want to change his numeric user ID. How can I accomplish this?

Probably because renaming a user isn’t exactly a task a professional SA would struggle with. (For the question downvotes.)

@Jeznet I downvoted because not only is this an incredibly simple task that could be solved by typing your title into google, you also answered your own question as soon as you asked it. Seemed a waste of time.

@boburob «you also answered your own question as soon as you asked it. Seemed a waste of time» please see: blog.stackoverflow.com/2011/07/…

@boburob Did you see that tick box that says Answer your own question – share your knowledge, Q&A-style when you ask a question? It’s there for a reason, and wouldn’t be there if they didn’t want it used. (Ideally on better questions, but regardless, downvoting someone for answering their own question is bad form.)

This is such a «simple» task that all three answers are incomplete; the Desktop Environment (Gnome/KDE/etc) may have its own idea about user names. That will typically come with a User Mgmt tool which synchronizes down; but building blocks like usermod do not synchronize up. Using the accepted answers will leave you with a login screen showing «hedgehog», but the log you in as «squirrel».

3 Answers 3

Under Linux, the usermod command changes user names. It modifies the system account files to reflect the changes that are specified on the command line.

To change just the username:

usermod --login new_username old_username 

To change the username and home directory name:

usermod --login new_username --move-home --home path_to_the_new_home_dir old_username 

You may also want to change the name of the group associated with the user:

groupmod --new-name new_username old_username 

Also note, that the group is not changed either. If you need to rename the old-user-name group as well, use # groupmod -n new-user-name old-user-name .

You’ll also need to rename the crontab file located in /var/spool/cron/crontabs/ (if you have any jobs installed)

NOTE: don’t try this if your directory is encrypted! If this is your case you might want to check first: https://askubuntu.com/questions/107410/can-you-unencrypt-remove-encryption-from-a-user-home-folder

The straight out way of doing this is:

    Create a new temp account with sudo rights:

sudo adduser temp sudo adduser temp sudo 
sudo usermod -l new-username -m -d /home/new-username old-username 
sudo groupmod -n new-username old-username 

Otherwise, you just (1) create a new user and (2) rsync the old user home folder to the new and then (3) chown it.

Generally you can rename a user by changing their username in the /etc/passwd (and /etc/shadow , if applicable) files. On most unix systems the vipw command is used to edit these files (and on many systems includes some safeguards to ensure that you don’t mess things up too badly).
See the man pages for passwd(5) , shadow(5) , and vipw(8) for more information.

Note that the method above does not rename other things which may bear the original username (home directories being the prime example, per-user personal groups (on systems which use them) being another). You may wish to clean these up as well for consistency, by changing the appropriate fields in the passwd file and renaming the directories.

Читайте также:  Linux mint на слабый пк

Several operating systems provide a system-specific way of renaming users. For example many Linux systems include the usermod(8) command, and on AIX you can change account names using SMIT (or smitty in a terminal).
These commands will often handle the cleanup items like renaming home directories, if you ask them to.

Источник

How to Change Your Username in Linux

Linux enables multiple users to access the same system at a time, or we can say it is a multi-user OS. Along with that, there are multiple responsibilities that the admin must manage like giving permissions to users, allotting unique id names, etc. When we talk about changing the credentials of users like changing their ids or name, Linux also enables us to change the name of the user using commands.

To change the username in the Linux operating system, we use a command called “usermod”. The “usermod” command is a command line utility or library which is provided by Linux that is used to change the properties that are related to the user information. Like other command line utilities, this utility also takes options as input to analyze what action to perform. In our case, what this command does is that it changes or modifies the root system files which are called system account files to make the changes that are specified in the command line prompt. One thing to keep in mind before performing such an activity is that we should not edit or modify the /etc/passwd file manually or by any text editor.

Syntax:

To change or rename the user, we use the following syntax:

The “-l” option is used to tell the system that we are changing the username. The command replaces the existing name with the “old-name” parameter and replaces it with “login-name”. This command does not change or edit anything else. When we are changing the username, we must change the user’s home directory to make an impact on the change that we made.

By passing other options to this command instead of “-l”, we can also make changes to the other properties of the user like adding it to a group, changing its home directory, etc.

Example:

In this example, we try to change the username of our operating system. As we know, the user details and their credentials are always stored in a /etc/passwd. We can also change the username manually by editing that file. But sometimes, it can cause some issues in the system. So, mostly, this is avoided by the admin that changes it manually. For this reason, most of the time, the usermod utility is used in Linux to perform any modifications in the user details and manage them. To perform this procedure, you must have an access to the super user account unless you cannot manage those tasks.

Читайте также:  Удалить linux kde linux удалить kde

Adding New User

Let us suppose we want to change the name of the user. For that, we first try to add a new user of which we then try to change the name. To create a new user, we run the following command in our terminal:

After running the previous command, it asks for the password of the admin that is creating the new user. One thing to keep in mind is it won’t display the password while you are entering it on the terminal. Just type the password and then press the enter key.

After entering the password, you get the following output in which the directory for the new user is created:

It asks you to set a new password for the new user. When the password is entered, it then takes some details which may include the full name of the user that you want to use. Some additional details are “room number”, “work phone”, “home phone”, and “other”. You can enter any details or press escape so you can skip it. By providing all the details, it asks for your confirmation. By entering the “y” value, you allow the system to create a new user.

At last, by logging out of the user that you are currently using, you can check that the new user is created with the name, “testuser”. As can be seen in the following snippet, both the new and the old users are shown:

Renaming the User

Now, it’s time to rename the user that we created. We first log in to the system. After logging into the system, we open the terminal where, using the command, we try to rename our user. To do so, we have to run a simple command which is shown in the next line:

In the previously-mentioned command, we use the usermod command that is used to modify the system files. The “-l” flag is used to rename any file while using it along with the usermod command enables us to rename the user. The next keyword, “testuser1”, is the name that we use as a new name for the user. Then, we use the “-d” flag that denotes the directory. We pass the path of the directory in which the credentials of the users are located. The “-m” denotes the modification of the name, “testuser”. When we press the enter key, it asks us to enter the password of the user. After entering the password, it won’t display any output in the terminal. It simply hands over the terminal to the user to run the further commands.

Now, to check whether the username has changed or not, we simply log out of the current user and check it in our log-in area.

Or by logging into our user which we renamed, we can see that the directory name that is created in our home directory name is also changed.

Or by just opening the terminal, it can be seen that the name of the terminal is changed to “testuser1” as shown in the following snippet:

Conclusion

We introduced you to the procedure to change the username in the Linux operating system. Sometimes, you can go through a situation when it is necessary to change the username. Then, we tried to explain the complete procedure by explaining it to you via an example in which we explained each step that has been taken while implementing these commands.

Читайте также:  Pulse secure linux client

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.

Источник

How to rename Linux users and their home directory

In this tutorial, you will learn how to a rename a user in CentOS, Debian, Ubuntu and most other Linux distributions. More than that, you will also learn how to rename thier home directory, primary group, and change their UID.

Renaming Users in Linux

Linux provides a tool named usermod specifically for making modifications to user accounts. In this case we are using it to rename a user account, which is done using the -l flag.

For example, to rename a user named student1 to johndoe, you would run the usermod command as follows.

sudo usermod -l johndoe student1

The -l flag will only change the user’s name. All other things attached to the user will remain unaffected, such as home directory. and UID.

Changing a User’s Home Directory in Linux

After renaming a user it may make sense to change their home directory, too. Otherwise, it would cause a lot of confusion trying to explain why johndoe ‘s home directory is /home/student .

To change the user’s home directory we once again use the usermod command. However, this time we need to perform to actions: change the home directory to a new path, and move the contents from the old path into the new path.

We set the new home directory path using the -d or —home flag with the path to the new directory. We must also use the -m flag to copy the contents of the old home directory into the new one.

sudo usermod -d /home/joedoe -m johndoe 

Every user on a Linux system is created with a group of the same name. When we change the name of a user their group name is left untouched. It’s a good idea to also change the user’s primary group name as well.

To change a user’s primary group name we use the groupmod command with the -n flag. We must supply the old name and a new name.

For example, to rename the newly renamed user johndoe ‘s primary group to johndoe from student1 , we would run the following command.

sudo groupmod -n johndoe student1

Changing a User’s UID

A little more rare than renaming a user or changing their home directory is changing their UID. A User’s UID is their unique ID on a Linux system. When we assign permissions to file and directories, we use their UID. Processes started by a user are also executed using a user’s UID.

To change a user’s ID we use the usermod command with the -u flag, followed by a new, unique integer.

For example, to set johndoes UID to 5001, we would run the following usermod command.

sudo usermod -u 5001 johndoe

Источник

Оцените статью
Adblock
detector