Linux change root user's home directory

centos7

#1

How do I change the home directory of the “root” user in CentOS Linux server 7?


#2

Yes, you can change root users directory. The default is /root/. You can read /etc/passswd file to find out current directory for the root user:

grep ^root /etc/passwd

OR

awk -F: '/^root/{ print "User " $1 " has " $6 " setup as a home dir"}' /etc/passwd

How to Linux change root user’s home directory

  1. Create a new directory for root user, say /foo:
sudo mkdir /foo/
  1. Setup correct permissions:
chown -R root:root /foo/
chmod -R 0700 /foo/
  1. Run vipw command:
sudo vipw
  1. Locate root user entry:
    Find:
root:x:0:0:root:/root:/bin/bash

Replace with /root with /foo:

root:x:0:0:root:/foo:/bin/bash

Save and close the file in vi/vim
5. Verify it:

awk -F: '/^root/{ print "User " $1 " has " $6 " setup as a home dir"}' /etc/passwd