Difference between revisions of "Change username"

From ArchWiki
Jump to: navigation, search
m
Line 5: Line 5:
  
 
==Procedure==
 
==Procedure==
{{Warning|Make certain that you're not logged in as the user whose name you can changed!  Open a new tty ({{Keypress|Ctrl}}+{{Keypress|Alt}}+{{Keypress|F1}}) and log in as root or as another user and su to root.}}
+
{{Warning|Make certain that you're not logged in as the user whose name you are about to change!  Open a new tty ({{Keypress|Ctrl}}+{{Keypress|Alt}}+{{Keypress|F1}}) and log in as root or as another user and su to root.}}
  
===Changing the username/group===
+
===Change A User's Login===
Basically, you have four commands to type as root to change the username and group. They are as follows:
+
This will change only the user's login name.
# mv /home/oldname /home/newname
+
<pre>
 
  # usermod -l newname oldname
 
  # usermod -l newname oldname
# usermod -d /home/newname newname
+
</pre>
  
If you want to change the group also:
+
===Change A User's $HOME===
 +
This will only change the home directory of <b>username</b>
 +
<pre>
 +
# usermod -d /my/new/home username
 +
</pre>
 +
 
 +
===Change A User's $HOME and Move Contents===
 +
This will move the contents of <b>username</b>'s home directory to <b>/my/new/home</b> and set the user's home directory to the new one.
 +
<pre>
 +
# usermod -md /my/new/home username;
 +
</pre>
 +
 
 +
===Change Group Name===
 +
If you want to change the user's group also:
 +
<pre>
 
  # groupmod -n newname oldname
 
  # groupmod -n newname oldname
 +
</pre>
 +
{{note|This will change a group name but not the numerical GID of the group.}}
 +
 +
For further information see the manpages for usermod and groupmod.
 +
 +
===Manually With /etc/passwd===
 +
When possible, you should use the above commands to modify usernames and home directories, however for those of you who want to know the 'guts' of the operations, it can be done manually.
 +
 +
====/etc/passwd File Format====
 +
Each line of the file follows a specific format. There are seven fields, each delimited by (":") a colon.
 +
<pre>
 +
<login name>:<password>:<numerical UID>:<numerical GID>:<Real name/comments>:<home directory>:<user command interpreter>
 +
</pre>
 +
{{Warning|It is unsafe to set the <password> field in /etc/passwd. Passwords should be changed (by root) with the <b>passwd</b> command!}}
 +
* <login name> This field can not be blank. Standard *NIX naming rules apply.
 +
* <password> would be an encrypted password, however it should be marked with a lowercase "x" (without quotes) to signify the password is located in <b>/etc/shadow</b>.
 +
* Each user and group name has a corresponding numerical UID and GID (User ID and Group ID). In Arch, the first login name (after root) is UID 1000 by default. Subsequent UID/GID entries for users should be greater than 1000. GID should match the primary group for the particular user. Numeric values for GIDs are listed in <b>/etc/group</b>.
 +
* <Real name/comments> is used by services such as <b>finger</b>. This field is optional and may be left blank.
 +
* <home directory> is used by the login command to set the $HOME environment variable. Several services with their own users use "/" which is safe for services, but not recommended for normal users.
 +
* <user command interpreter> is the path to the user's default shell. This is normally [[Bash]], but there are several other command line interpreters available. The default setting is "/bin/bash" (without quotes) for users. If you use another CLI, set the path to it here. This field is optional.
 +
 +
Example (user):
 +
jack:x:1001:100:Jack Smith,some comment here,,:/home/jack:/bin/bash
 +
 +
Broken down, this means: user jack (who's password is in /etc/shadow) is UID 1001 and his primary group is 100 (users). Jack Smith is his full name and there is a comment associated to his account. His home directory is /home/jack and he is using Bash.
  
If you want to see what each command is doing, check out the manpages for usermod and groupmod.
 
  
 
==Gotchas==
 
==Gotchas==

Revision as of 00:33, 9 February 2011

Introduction

Changing a username under Arch (or any flavor of Linux) is safe and easy when done properly. You can also change the associated groupname for the user if you wish. Following the procedure below will do just this retaining your UID/GID for the affected user thus not roaching any file permissions you have setup.

Procedure

Warning: Make certain that you're not logged in as the user whose name you are about to change! Open a new tty (Template:Keypress+Template:Keypress+Template:Keypress) and log in as root or as another user and su to root.

Change A User's Login

This will change only the user's login name.

 # usermod -l newname oldname

Change A User's $HOME

This will only change the home directory of username

 # usermod -d /my/new/home username

Change A User's $HOME and Move Contents

This will move the contents of username's home directory to /my/new/home and set the user's home directory to the new one.

 # usermod -md /my/new/home username;

Change Group Name

If you want to change the user's group also:

 # groupmod -n newname oldname
Note: This will change a group name but not the numerical GID of the group.

For further information see the manpages for usermod and groupmod.

Manually With /etc/passwd

When possible, you should use the above commands to modify usernames and home directories, however for those of you who want to know the 'guts' of the operations, it can be done manually.

/etc/passwd File Format

Each line of the file follows a specific format. There are seven fields, each delimited by (":") a colon.

<login name>:<password>:<numerical UID>:<numerical GID>:<Real name/comments>:<home directory>:<user command interpreter>
Warning: It is unsafe to set the <password> field in /etc/passwd. Passwords should be changed (by root) with the passwd command!
  • <login name> This field can not be blank. Standard *NIX naming rules apply.
  • <password> would be an encrypted password, however it should be marked with a lowercase "x" (without quotes) to signify the password is located in /etc/shadow.
  • Each user and group name has a corresponding numerical UID and GID (User ID and Group ID). In Arch, the first login name (after root) is UID 1000 by default. Subsequent UID/GID entries for users should be greater than 1000. GID should match the primary group for the particular user. Numeric values for GIDs are listed in /etc/group.
  • <Real name/comments> is used by services such as finger. This field is optional and may be left blank.
  • <home directory> is used by the login command to set the $HOME environment variable. Several services with their own users use "/" which is safe for services, but not recommended for normal users.
  • <user command interpreter> is the path to the user's default shell. This is normally Bash, but there are several other command line interpreters available. The default setting is "/bin/bash" (without quotes) for users. If you use another CLI, set the path to it here. This field is optional.

Example (user):

jack:x:1001:100:Jack Smith,some comment here,,:/home/jack:/bin/bash

Broken down, this means: user jack (who's password is in /etc/shadow) is UID 1001 and his primary group is 100 (users). Jack Smith is his full name and there is a comment associated to his account. His home directory is /home/jack and he is using Bash.


Gotchas

  • If you're using sudo make sure you update your Template:Filename to reflect the new username(s) (via the visudo command as root)
  • If you modified your PATH statement in your Template:Filename, make sure you change it to reflect the new username
  • Likewise, be sure you change any config file such as Template:Filename or whatever if you are pointing it to a script or mountpoint, etc. within the old user's home directory.
  • I had to repeat the procedure to [enable spell checking] in Firefox or else the check-as-you-type spelling didn't work for me after renaming my user.
  • I had to reinstall my Thunderbird addons ([Enigmail]) after renaming my user.
  • Anything on your system (desktop shortcuts, shell scripts, etc.) that uses an absolute path to your home dir (i.e. /home/oldname) will need to be changed to reflect your newname. To avoid these problems in shell scripts, simply use the ~ variable for home directories.