{"id":276,"date":"2010-11-28T13:08:40","date_gmt":"2010-11-28T18:08:40","guid":{"rendered":"http:\/\/blog.alanporter.com\/?p=276"},"modified":"2010-11-28T13:08:40","modified_gmt":"2010-11-28T18:08:40","slug":"passwd-and-chage","status":"publish","type":"post","link":"https:\/\/alanporter.com\/blog\/2010\/11\/28\/passwd-and-chage\/","title":{"rendered":"Disabling Linux user accounts &#8211; passwd and chage"},"content":{"rendered":"<p>On internet-facing machines, I like to disable the root password so that you can only log in as root using an SSH key. \u00a0This is done by setting the seemingly-scary option <code>PermitRootLogin without-password<\/code> in <strong>\/etc\/ssh\/sshd_config<\/strong> . \u00a0This option means that you can ONLY use a key to log in as root&#8230; a password will never be accepted, and so you can not guess it by trial-and-error.<\/p>\n<p>While I am at it, I go ahead and disable the root password completely. \u00a0That&#8217;ll be one less thing for me to remember, and one less thing to keep secret.<\/p>\n<p>First, be sure that you have at least one &#8220;sudoer&#8221; user, or at least one SSH key in <strong>~root\/.ssh\/authorized_keys<\/strong> .  Otherwise, you&#8217;ll realize in an &#8220;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Sniglet\">ignisecond<\/a>&#8221; that you&#8217;ve just locked yourself out.<\/p>\n<p>Then, use the <code>passwd<\/code> command to &#8220;lock&#8221; the account.<\/p>\n<pre>\n$ passwd -l root\n<\/pre>\n<p>This command will put a <strong>!<\/strong> character in the password field of the <strong>\/etc\/shadow<\/strong> file so that no password hash will ever match the string in the shadow file.<\/p>\n<p>But on SOME SYSTEMS, it also does a second thing &#8212; it may change the account expiration date to January 2, 1970.  This will prevent SSH access, even with a key.<\/p>\n<p>While researching this article, I had this &#8220;world-shifting-under-me&#8221; feeling, as I distinctly remember having to work around this issue.  However, on every system I tried, I could not reproduce the expiration-date-changing behavior.  Then I found that it was an issue that flip-flopped in the Debian community between 2006 and 2008.  In 2006, they made the <code>passwd -l<\/code> command <em>lock the password<\/em> and <strong>also<\/strong> <em>expire the user account<\/em>.  But in 2008, they decided that the change affected too many people, where most had grown accustomed to the <code>passwd<\/code> command affecting the <em>password<\/em> only, and not touching the account expiration date.<\/p>\n<p>If you want to change the account&#8217;s expiration date, you should use the <code>chage<\/code> command.<\/p>\n<pre>\n$ chage -E -1 root  # never expire\n$ chage -E 1 root  # expire on Jan 2, 1970\n$ chage -E 0 root  # not recommended, undefined behavior\n$ chage -E 2010-12-25  # expire on a specific date\n<\/pre>\n<p>So here I was, all set to share a nugget of wisdom with the world, and instead, it ends up being a trip down memory lane.  However, in the process, I learned a couple of things.<\/p>\n<p>A very safe way to disable a user account&#8217;s password, while keeping the account open to SSH access is like this: <code>passwd -l user && chage -E -1 user<\/code> .  The <code>chage<\/code> part is unnecessary on modern systems, but it does not hurt anything.<\/p>\n<p>A quick way to check on whether a password is locked, or a user account is expired, is to use <code>passwd -S user<\/code>, like this.<\/p>\n<pre>\n$ passwd -S user\nuser P 09\/11\/2007 0 99999 7 -1\n<\/pre>\n<p>This says that <strong>user<\/strong>&#8216;s password <em>is<\/em> set (P), it was changed way back 2007, there is no minimum age (restriction on how often they can change their password), there&#8217;s a very long maximum age (time when they are forced to change their password), the warning period is one week, and the account is not inactive\/expired.<\/p>\n<p>So there you go, two for one, a Linux tip and a history lesson!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On internet-facing machines, I like to disable the root password so that you can only log in as root using an SSH key. \u00a0This is done by setting the seemingly-scary option PermitRootLogin without-password in \/etc\/ssh\/sshd_config . \u00a0This option means that you can ONLY use a key to log in as root&#8230; a password will never [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,15],"tags":[],"class_list":["post-276","post","type-post","status-publish","format-standard","hentry","category-software","category-tips-tricks","count-0","even alt","author-alan","last"],"_links":{"self":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/comments?post=276"}],"version-history":[{"count":0,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts\/276\/revisions"}],"wp:attachment":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/media?parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/categories?post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/tags?post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}