How to copy a line from one file and replace the line in another?












1














Looking to be able to have a part in a script that pulls the shadow file entry from one computer, and then find that user's entry on another computer and replace that line (to update their password on multiple computers).



Note that this is part of a user creation script, so the users will be created on each computer first, then adding a password on the server, which would then copy it to the other workstations.










share|improve this question




















  • 5




    You should use usermod rather than manually edit /etc/{passwd,shadow}
    – cryptarch
    Dec 19 '18 at 22:38






  • 2




    If the password is coming from the server then that means that the computers are authenticating via the server. With that being said, why do you need to create the users on the computers? It would help if you'd be more clear on your setup and what you are trying to do.
    – Nasir Riley
    Dec 19 '18 at 22:46










  • Script is being run on the server, there is no authentication via the network. Presently, we have to add users and set their passwords on each computer, trying to do it from one instead.
    – Daniel
    Dec 19 '18 at 23:24






  • 1




    Failing the ability to e.g. use LDAP for this, have you considered using ansible? You can store hashes on one machine and push them to as many target machines as needed ...
    – tink
    Dec 19 '18 at 23:58
















1














Looking to be able to have a part in a script that pulls the shadow file entry from one computer, and then find that user's entry on another computer and replace that line (to update their password on multiple computers).



Note that this is part of a user creation script, so the users will be created on each computer first, then adding a password on the server, which would then copy it to the other workstations.










share|improve this question




















  • 5




    You should use usermod rather than manually edit /etc/{passwd,shadow}
    – cryptarch
    Dec 19 '18 at 22:38






  • 2




    If the password is coming from the server then that means that the computers are authenticating via the server. With that being said, why do you need to create the users on the computers? It would help if you'd be more clear on your setup and what you are trying to do.
    – Nasir Riley
    Dec 19 '18 at 22:46










  • Script is being run on the server, there is no authentication via the network. Presently, we have to add users and set their passwords on each computer, trying to do it from one instead.
    – Daniel
    Dec 19 '18 at 23:24






  • 1




    Failing the ability to e.g. use LDAP for this, have you considered using ansible? You can store hashes on one machine and push them to as many target machines as needed ...
    – tink
    Dec 19 '18 at 23:58














1












1








1







Looking to be able to have a part in a script that pulls the shadow file entry from one computer, and then find that user's entry on another computer and replace that line (to update their password on multiple computers).



Note that this is part of a user creation script, so the users will be created on each computer first, then adding a password on the server, which would then copy it to the other workstations.










share|improve this question















Looking to be able to have a part in a script that pulls the shadow file entry from one computer, and then find that user's entry on another computer and replace that line (to update their password on multiple computers).



Note that this is part of a user creation script, so the users will be created on each computer first, then adding a password on the server, which would then copy it to the other workstations.







shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 19 '18 at 23:50









Rui F Ribeiro

39k1479130




39k1479130










asked Dec 19 '18 at 22:33









Daniel

111




111








  • 5




    You should use usermod rather than manually edit /etc/{passwd,shadow}
    – cryptarch
    Dec 19 '18 at 22:38






  • 2




    If the password is coming from the server then that means that the computers are authenticating via the server. With that being said, why do you need to create the users on the computers? It would help if you'd be more clear on your setup and what you are trying to do.
    – Nasir Riley
    Dec 19 '18 at 22:46










  • Script is being run on the server, there is no authentication via the network. Presently, we have to add users and set their passwords on each computer, trying to do it from one instead.
    – Daniel
    Dec 19 '18 at 23:24






  • 1




    Failing the ability to e.g. use LDAP for this, have you considered using ansible? You can store hashes on one machine and push them to as many target machines as needed ...
    – tink
    Dec 19 '18 at 23:58














  • 5




    You should use usermod rather than manually edit /etc/{passwd,shadow}
    – cryptarch
    Dec 19 '18 at 22:38






  • 2




    If the password is coming from the server then that means that the computers are authenticating via the server. With that being said, why do you need to create the users on the computers? It would help if you'd be more clear on your setup and what you are trying to do.
    – Nasir Riley
    Dec 19 '18 at 22:46










  • Script is being run on the server, there is no authentication via the network. Presently, we have to add users and set their passwords on each computer, trying to do it from one instead.
    – Daniel
    Dec 19 '18 at 23:24






  • 1




    Failing the ability to e.g. use LDAP for this, have you considered using ansible? You can store hashes on one machine and push them to as many target machines as needed ...
    – tink
    Dec 19 '18 at 23:58








5




5




You should use usermod rather than manually edit /etc/{passwd,shadow}
– cryptarch
Dec 19 '18 at 22:38




You should use usermod rather than manually edit /etc/{passwd,shadow}
– cryptarch
Dec 19 '18 at 22:38




2




2




If the password is coming from the server then that means that the computers are authenticating via the server. With that being said, why do you need to create the users on the computers? It would help if you'd be more clear on your setup and what you are trying to do.
– Nasir Riley
Dec 19 '18 at 22:46




If the password is coming from the server then that means that the computers are authenticating via the server. With that being said, why do you need to create the users on the computers? It would help if you'd be more clear on your setup and what you are trying to do.
– Nasir Riley
Dec 19 '18 at 22:46












Script is being run on the server, there is no authentication via the network. Presently, we have to add users and set their passwords on each computer, trying to do it from one instead.
– Daniel
Dec 19 '18 at 23:24




Script is being run on the server, there is no authentication via the network. Presently, we have to add users and set their passwords on each computer, trying to do it from one instead.
– Daniel
Dec 19 '18 at 23:24




1




1




Failing the ability to e.g. use LDAP for this, have you considered using ansible? You can store hashes on one machine and push them to as many target machines as needed ...
– tink
Dec 19 '18 at 23:58




Failing the ability to e.g. use LDAP for this, have you considered using ansible? You can store hashes on one machine and push them to as many target machines as needed ...
– tink
Dec 19 '18 at 23:58










1 Answer
1






active

oldest

votes


















1














While agreeing with the above comments that there are a lot more simpler ways to handle this, here is a bit of help with the actual question:



$MY_USER="anything"
HASH=$(ssh <source_server> "cat /etc/shadow | grep ^$MY_USER:" | awk -F: '{print $2}')
ssh <target_server> "echo '$MY_USER:$HASH' | chpasswd -e"


Replace the MY_USER variable for your particular user and loop through your servers with the last row of the script.



Some other comments:




  1. You should make sure that all of your servers use the same password encoding (Also creating a backup from the entry being replaced, just in case).

  2. If you do not have access to the root password, and have no nopasswd sudo rules set up, sudo -S could be useful.

  3. Make sure you have other ways of access before changing the password.






share|improve this answer























  • This looks to be precisely what I was looking for, thank you!
    – Daniel
    Dec 20 '18 at 18:38











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490029%2fhow-to-copy-a-line-from-one-file-and-replace-the-line-in-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














While agreeing with the above comments that there are a lot more simpler ways to handle this, here is a bit of help with the actual question:



$MY_USER="anything"
HASH=$(ssh <source_server> "cat /etc/shadow | grep ^$MY_USER:" | awk -F: '{print $2}')
ssh <target_server> "echo '$MY_USER:$HASH' | chpasswd -e"


Replace the MY_USER variable for your particular user and loop through your servers with the last row of the script.



Some other comments:




  1. You should make sure that all of your servers use the same password encoding (Also creating a backup from the entry being replaced, just in case).

  2. If you do not have access to the root password, and have no nopasswd sudo rules set up, sudo -S could be useful.

  3. Make sure you have other ways of access before changing the password.






share|improve this answer























  • This looks to be precisely what I was looking for, thank you!
    – Daniel
    Dec 20 '18 at 18:38
















1














While agreeing with the above comments that there are a lot more simpler ways to handle this, here is a bit of help with the actual question:



$MY_USER="anything"
HASH=$(ssh <source_server> "cat /etc/shadow | grep ^$MY_USER:" | awk -F: '{print $2}')
ssh <target_server> "echo '$MY_USER:$HASH' | chpasswd -e"


Replace the MY_USER variable for your particular user and loop through your servers with the last row of the script.



Some other comments:




  1. You should make sure that all of your servers use the same password encoding (Also creating a backup from the entry being replaced, just in case).

  2. If you do not have access to the root password, and have no nopasswd sudo rules set up, sudo -S could be useful.

  3. Make sure you have other ways of access before changing the password.






share|improve this answer























  • This looks to be precisely what I was looking for, thank you!
    – Daniel
    Dec 20 '18 at 18:38














1












1








1






While agreeing with the above comments that there are a lot more simpler ways to handle this, here is a bit of help with the actual question:



$MY_USER="anything"
HASH=$(ssh <source_server> "cat /etc/shadow | grep ^$MY_USER:" | awk -F: '{print $2}')
ssh <target_server> "echo '$MY_USER:$HASH' | chpasswd -e"


Replace the MY_USER variable for your particular user and loop through your servers with the last row of the script.



Some other comments:




  1. You should make sure that all of your servers use the same password encoding (Also creating a backup from the entry being replaced, just in case).

  2. If you do not have access to the root password, and have no nopasswd sudo rules set up, sudo -S could be useful.

  3. Make sure you have other ways of access before changing the password.






share|improve this answer














While agreeing with the above comments that there are a lot more simpler ways to handle this, here is a bit of help with the actual question:



$MY_USER="anything"
HASH=$(ssh <source_server> "cat /etc/shadow | grep ^$MY_USER:" | awk -F: '{print $2}')
ssh <target_server> "echo '$MY_USER:$HASH' | chpasswd -e"


Replace the MY_USER variable for your particular user and loop through your servers with the last row of the script.



Some other comments:




  1. You should make sure that all of your servers use the same password encoding (Also creating a backup from the entry being replaced, just in case).

  2. If you do not have access to the root password, and have no nopasswd sudo rules set up, sudo -S could be useful.

  3. Make sure you have other ways of access before changing the password.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 21 '18 at 14:28

























answered Dec 20 '18 at 13:48









T. Hajdara

212




212












  • This looks to be precisely what I was looking for, thank you!
    – Daniel
    Dec 20 '18 at 18:38


















  • This looks to be precisely what I was looking for, thank you!
    – Daniel
    Dec 20 '18 at 18:38
















This looks to be precisely what I was looking for, thank you!
– Daniel
Dec 20 '18 at 18:38




This looks to be precisely what I was looking for, thank you!
– Daniel
Dec 20 '18 at 18:38


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490029%2fhow-to-copy-a-line-from-one-file-and-replace-the-line-in-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Morgemoulin

Scott Moir

Souastre