hi
How can I change the home directory of a user without using usermod -d command?
( by modifying /etc/passwd)
I must ask, is there a specific reason why you can't use the usermod command?
because this command is not working, someone made modification in kernel
I ran across this script with a simple google search for
awk -vold=$old_homedir_path -vnew=$new_homedir_path -F: '
BEGIN {OFS = ":"}
{sub(old,new,$6);print}' /etc/passwd > /etc/passwd.new
Oddly enough, there's no sed here
With sed it should be something like:
sed '/user/s!\(.*:\).*:\(.*\)!\1/newdir/name:\2!' /etc/passwd > /etc/passwd.new
Regards
the user will enter the username and then new home dir path, and I will change it according to these new variables.
echo "enter username to change homedir";
read username;
echo "enter new path for this user";
read pathh;
## Awk or sed command
I cannot be able to change the homedir of a user by using the above variables.
The posted awk and sed command is a little bit diifrent.
Just a head start:
awk -v user="user" -v path="new_path" 'BEGIN{FS=OFS=":"}$1==user{$6=new_path}1' /etc/passwd
I used your suggestion, but doesnt work:
echo "Enter username to change home directory:";
read username;
echo "Enter new home directory path:";
read new_path;
awk -v user=$username -v path=$new_path 'BEGIN{FS=OFS=":"}$1==user{$6=$new_path}1' /etc/passwd;;
Watch for typo and use a temp file to make the changes permanent.
it again doent work
Maybe if you can tell what shell are you scripting for?
echo $SHELL
and what part of your script is not working (Please post your script (copy/paste) and use the [code] tag's.), maybe we can help you.
danmero means 'doesn't work' is meaningless when it comes to getting it to work.
echo "Enter username to change home directory:";
read username;
echo "Enter new home directory path:";
read new_path;
awk -v user="$username" -v path="$new_path" 'BEGIN{FS=OFS=":"}$1==user{$6=$new_path}1' /etc/passwd;;
After I run above script, the home directory for the entered user doesnt change.
and also when I run the script entire /etc/passwd file is displayed.
The result of the echo $SHELL is:
/bin/bash
awk don't edit files "in place", use a temporary file:
#!bin/sh
echo -n "Enter username to change home directory: "
read username
echo -n "Enter new home directory path: "
read new_path
awk -v user="$username" -v path="$new_path" 'BEGIN{FS=OFS=":"}$1==user{$6=new_path}1' /etc/passwd > temp_file
mv temp_file /etc/passwd
You must have root permissions to run this script.
awk will not change your file, use a temporary file for that
awk -v user="$username" -v path="$new_path" 'BEGIN{FS=OFS=":"}$1==user{$6=$new_path}1' /etc/passwd > /etc/passwd.new && mv /etc/passwd.new /etc/passwd;;
useradd temp
cat /etc/ passwd
temp:x:502:505::/home/temp:/bin/bash
After I run the script with the following variables:
username: temp
new_path: /home/temp
cat /etc/passwd
temp:x:502:505::temp:x:502:505:::/bin/bash
I think there is something wrong with the script.
also, home directory should be /home/temp
username=temp
new_path=/home/temp
yea
I tried with those variables:
username=temp
new_path=/home/temp
but not correctly working