{"id":8286,"date":"2021-07-15T10:35:24","date_gmt":"2021-07-15T10:35:24","guid":{"rendered":"https:\/\/linuxways.net\/?p=8286"},"modified":"2021-07-15T10:35:24","modified_gmt":"2021-07-15T10:35:24","slug":"usermod-command-with-examples","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/usermod-command-with-examples\/","title":{"rendered":"Usermod Command with Examples"},"content":{"rendered":"<p>The Linux usermod command is a command-line utility that allows you to modify a user\u2019s attributes on a Linux system. Such attributes include the groups that a user belongs to, login directory, password, and so on. In this guide, we showcase some of the usermod commands that you can use to change a regular user\u2019s properties.<\/p>\n<h2><strong>Usermod Syntax<\/strong><\/h2>\n<p>The usermod command takes the following syntax:<\/p>\n<pre><strong>$ usermod options username<\/strong><\/pre>\n<h2><strong>Adding a regular user to a Group<\/strong><\/h2>\n<p>Probably the common use case of the <strong>usermod<\/strong> command is adding or placing a user in another group. By default, a primary group is assigned to the user upon creation of the user account. Additionally, you can add the user to another group. To achieve this , use the <strong>-a -G<\/strong> ir simply <strong>-aG<\/strong> in the syntax shown:<\/p>\n<pre><strong>$ usermod -aG group user<\/strong><\/pre>\n<p>In the example below, we are adding a regular user <strong>bob<\/strong> to the <strong>sudo<\/strong> group.<\/p>\n<pre><strong>$ usermod -aG sudo group<\/strong><\/pre>\n<p>Thereafter, invoke the <strong>groups <\/strong>command to verify the groups that the user <strong>bob<\/strong> belongs to. Initially, the user only belonged to the group <strong>bob<\/strong>. After adding the user to the <strong>sudo<\/strong> group, the user now belongs to 2 groups: <strong>bob<\/strong> and <strong>sudo<\/strong> groups.<\/p>\n<pre><strong>$ groups bob<\/strong><\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"721\" height=\"256\" class=\"wp-image-8287\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-365.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-365.png 721w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-365-300x107.png 300w\" sizes=\"auto, (max-width: 721px) 100vw, 721px\" \/><\/strong><\/p>\n<h2><strong>Change a user\u2019s home directory <\/strong><\/h2>\n<p>With <strong>usermod -d<\/strong> command, you can also change the default home directory as follows.<\/p>\n<pre><strong>$ usermod -d \/home\/new_directory user<\/strong><\/pre>\n<p>In the example below, we have changed the home directory of user bob to the <strong>\/home\/test <\/strong>directory.<\/p>\n<pre><strong>$ sudo usermod -d \/home\/test bob<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"758\" height=\"138\" class=\"wp-image-8288\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-366.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-366.png 758w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-366-300x55.png 300w\" sizes=\"auto, (max-width: 758px) 100vw, 758px\" \/><\/p>\n<h2><strong>Change a user\u2019s login name<\/strong><\/h2>\n<p>Using the <strong>-l<\/strong> flag, you can change the user\u2019s login name as follows:<\/p>\n<pre><strong>$ sudo usermod -l robert bob <\/strong><\/pre>\n<p>In this example, the command changes the user\u2019s login name from <strong>bob<\/strong> to <strong>robert.<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"165\" class=\"wp-image-8289\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-367.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-367.png 787w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-367-300x63.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-367-768x161.png 768w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/p>\n<h2><strong>Change User ID ( UID ) for a user<\/strong><\/h2>\n<p>The <strong>-u<\/strong> flag allows you to change the User ID of the user. In the example below, we have set the UID of user robert from the default <strong>1005 <\/strong>to <strong>4321.<\/strong><\/p>\n<pre><strong>$ sudo usermod -u 4321 robert<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"220\" class=\"wp-image-8290\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-368.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-368.png 787w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-368-300x84.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-368-768x215.png 768w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/p>\n<h2><strong>How to lock\/unlock a user<\/strong><\/h2>\n<p>In addition, you can also lock a user account using the <strong>-L <\/strong>option. In the example below, we are locking the user the user <strong>robert.<\/strong><\/p>\n<pre><strong>$ sudo usermod -L robert<\/strong><\/pre>\n<p>When you try logging in, you get an authentication error because the user is locked.<\/p>\n<p>To unlock the user, use the <strong>-U <\/strong>option as shown.<\/p>\n<pre><strong>$ sudo usermod -U robert<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"332\" class=\"wp-image-8291\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-369.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-369.png 782w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-369-300x127.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-369-768x326.png 768w\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" \/><\/p>\n<h2><strong>Change a user\u2019s expiry date<\/strong><\/h2>\n<p>With the <strong>-e<\/strong> option, you can specify the expiry date of a user account. In this scenario, the expiry date of user <strong>robert <\/strong>is set to <strong>2021-10-01.<\/strong><\/p>\n<pre><strong>$ sudo usermod -e 2021-10-01 robert<\/strong><\/pre>\n<p>To verify, use the <strong>chage<\/strong> command as follows.<\/p>\n<pre><strong>$ sudo chage -l robert<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"223\" class=\"wp-image-8292\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-370.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-370.png 786w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-370-300x85.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-370-768x218.png 768w\" sizes=\"auto, (max-width: 786px) 100vw, 786px\" \/><\/p>\n<h2><strong>Modify a user\u2019s primary group <\/strong><\/h2>\n<p>The primary group of the user is the default group that the user belongs to upon creation.<\/p>\n<p>Here, you can see that the primary group for the user <strong>bob<\/strong> is <strong>bob<\/strong>.<\/p>\n<pre><strong>$ id bob<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"91\" class=\"wp-image-8293\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-371.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-371.png 785w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-371-300x35.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-371-768x89.png 768w\" sizes=\"auto, (max-width: 785px) 100vw, 785px\" \/><\/p>\n<p>To change the primary group to another group, say <strong>games,<\/strong> run <strong>usermod<\/strong> command with the <strong>-g <\/strong>option as follows<\/p>\n<pre><strong>$ sudo usermod -g games bob<\/strong><\/pre>\n<p>Thereafter, use the id command to confirm that the group has changed.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"784\" height=\"164\" class=\"wp-image-8294\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-372.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-372.png 784w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-372-300x63.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/07\/word-image-372-768x161.png 768w\" sizes=\"auto, (max-width: 784px) 100vw, 784px\" \/><\/strong><\/p>\n<h2><strong>Summary<\/strong><\/h2>\n<p>That was a roundup of some of the useful nature of the usermod command. As observed, it comes quite in handy in changing various properties or attributes of regular users. It\u2019s our hope that you are now competent in making the most of the usermod command.<\/p>","protected":false},"excerpt":{"rendered":"<p>The Linux usermod command is a command-line utility that allows you to modify a user\u2019s attributes on a Linux system. Such attributes include the groups that a user&hellip;<\/p>","protected":false},"author":1,"featured_media":8427,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[418],"class_list":["post-8286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-usercommand-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=8286"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/8286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/8427"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=8286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=8286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=8286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}