{"id":6431,"date":"2017-01-21T11:20:03","date_gmt":"2017-01-21T03:20:03","guid":{"rendered":"http:\/\/rmohan.com\/?p=6431"},"modified":"2017-01-21T11:20:03","modified_gmt":"2017-01-21T03:20:03","slug":"how-to-change-linux-users-password-in-one-command-line","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=6431","title":{"rendered":"How to Change Linux User\u2019s Password in One Command Line"},"content":{"rendered":"<p>I frequently create new user accounts and change or set password for these accounts on a batch of Linux boxes. The create new user can be done by one command line. The problem is to change the password. In Linux, we use <a class=\"external external-man\" href=\"http:\/\/www.pkill.info\/linux\/man\/1-passwd\/\"><code class=\"not-hl\">passwd<\/code><\/a> to change password, but passwd requires input from stdin to get the new password. With the help of pipe and a little tricky, we can change user\u2019s password in one command line. This will save much time especially when creating a batch of user accounts.<\/p>\n<div id=\"st-in-post-1\"><\/div>\n<p>We use one example to introduce how to change Linux user\u2019s password in one command line. Suppose we login as root and want to change user linuxuser\u2018s password to linuxpassword.<\/p>\n<p>The <code class=\"not-hl\">passwd<\/code> command asks for the new password twice. And these two inputs (the same password) is separated by one \u201cEnter\u201d. We can emulate this by the echo command with the <code class=\"not-hl\">-e<\/code> option set. When <code class=\"not-hl\">-e<\/code> is in effect, escaped characters will be interpreted. Hence, <code class=\"not-hl\">n<\/code> in echo\u2019s input is echoed as \u201cnew line\u201d. In addition, on modern Linux with sufficiently new <code class=\"not-hl\">passwd<\/code>, you can use the <code class=\"not-hl\">--stdin<\/code> option to let <code class=\"not-hl\">passwd<\/code> accept a password from the STDIN instead of asking for the new password twice.<\/p>\n<p>So to change the password in our example, we just execute this one command:<\/p>\n<pre class=\"code not-hl\"># echo \"linuxpassword\" | passwd --stdin linuxuser<\/pre>\n<p>on modern Linux. (Thanks to DAVID for this tip)<\/p>\n<p>or<\/p>\n<pre class=\"code not-hl\"># echo -e \"linuxpassword\\nlinuxpassword\" | passwd linuxuser<\/pre>\n<p>This can also be put into one bash script or executed on remote node by the <code class=\"not-hl\">ssh<\/code> command.<\/p>\n<p>For example, we can change the password of linuxuser on a batch of servers (100 servers: 10.1.0.1 to 10.1.0.100) by:<\/p>\n<pre class=\"code not-hl\"># for ((i=1;i&lt;=100;i++)); do \\\r\nssh 10.1.0.$i 'echo -e \"linuxpassword\\nlinuxpassword\" | passwd linuxuser'; \\\r\ndone;<\/pre>\n<p>Even further, we can create one user and set its initial password remotely by:<\/p>\n<pre class=\"code not-hl\"># ssh remoteserver \\\r\n'useradd newuser; echo -e \"passwdofuser\\npasswdofuser\" | passwd newuser'<\/pre>\n<p>If you want to update your own password as a normal user, you may use<\/p>\n<pre class=\"code not-hl\">$ echo -e \"your_current_pass\\nlinuxpassword\\nlinuxpassword\" | passwd<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I frequently create new user accounts and change or set password for these accounts on a batch of Linux boxes. The create new user can be done by one command line. The problem is to change the password. In Linux, we use passwd to change password, but passwd requires input from stdin to get the [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6431"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6431"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6431\/revisions"}],"predecessor-version":[{"id":6432,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/6431\/revisions\/6432"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}