쉘 스크립트 내에서 passwd 명령 사용
새 사용자를 자동으로 추가하고 암호를 업데이트하는 셸 스크립트를 작성 중입니다. 대화식으로 새 암호를 묻는 대신 쉘 스크립트에서 읽을 수 있도록 passwd를 얻는 방법을 모르겠습니다. 내 코드는 다음과 같습니다.
adduser $ 1 암호 $ 1 $ 2 $ 2
" man 1 passwd"에서 :
--stdin
This option is used to indicate that passwd should read the new
password from standard input, which can be a pipe.
그래서 당신의 경우
adduser "$1"
echo "$2" | passwd "$1" --stdin
[ 업데이트 ] 댓글에서 몇 가지 문제가 제기되었습니다.
귀하의 passwd명령은 없을 수 있습니다 --stdin옵션을 : 사용 chpasswd에 의해 제안, 대신 유틸리티를 ashawley .
bash 이외의 셸을 사용하는 경우 "echo"는 내장 명령이 아닐 수 있으며 셸은 /bin/echo. 암호가 프로세스 테이블에 표시되고와 같은 도구로 볼 수 있기 때문에 이것은 안전하지 않습니다 ps.
이 경우 다른 스크립팅 언어를 사용해야합니다. 다음은 Perl의 예입니다.
#!/usr/bin/perl -w
open my $pipe, '|chpasswd' or die "can't open pipe: $!";
print {$pipe} "$username:$password";
close $pipe
유일한 솔루션은 Ubuntu 12.04에서 작동합니다.
echo -e "new_password\nnew_password" | (passwd user)
그러나 두 번째 옵션은 다음에서 변경할 때만 작동합니다.
echo "password:name" | chpasswd
에:
echo "user:password" | chpasswd
원본 게시물의 설명보기 : 스크립트를 통해 비밀번호 변경
다음에서 현명한 단어를 읽으십시오.
나는 인용한다 :
bash에서 할 수있는 것은 아무것도 작동하지 않습니다. passwd (1)은 표준 입력에서 읽지 않습니다. 이것은 의도적 인 것입니다. 그것은 당신의 보호를위한 것입니다. 암호는 프로그램에 넣거나 프로그램에 의해 생성되지 않았습니다. 그것들은 기능적 뇌를 가진 실제 인간의 손가락으로 만 입력되도록 의도되었으며, 어디에도 기록되지 않았습니다.
그럼에도 불구하고 우리는 35 년의 유닉스 보안을 어떻게 피할 수 있는지 묻는 수많은 사용자를 얻습니다.
shadow(5)암호를 올바르게 설정하는 방법을 설명 하고 GNU- I-only-care-about-security-it-doesn't-make-me-think-too-much -way of abusing을 보여줍니다. passwd(1).
당신은 바보 GNU의 passwd를 (1) 확장을 사용하려는 경우 마지막으로 --stdin, 하지 않는 명령 줄에서 퍼팅 암호를 전달합니다.
echo $mypassword | passwd --stdin # Eternal Sin.
echo "$mypassword" | passwd --stdin # Eternal Sin, but at least you remembered to quote your PE.
passwd --stdin <<< "$mypassword" # A little less insecure, still pretty insecure, though.
passwd --stdin < "passwordfile" # With a password file that was created with a secure `umask(1)`, a little bit secure.
마지막은 GNU로 할 수있는 최선의 방법입니다 passwd. 나는 여전히 그것을 추천하지 않을 것입니다.
명령 줄에 암호를 입력한다는 것은 상자에 대한 가장 원격 액세스 힌트를 가진 사람이라도 모니터링을 ps하거나 암호를 훔칠 수 있음을 의미 합니다. 상자가 안전하다고 생각하더라도; 그것은 당신이 어떤 대가를 치르더라도 피하는 습관 을 가져야 할 것입니다 (예, 일을 완수하는 데 조금 더 많은 어려움을 겪는 비용조차도).
요즘:
echo "user:pass" | chpasswd
sudoers 파일의 사용자 계정에 로그인하는 스크립트를 통해 원격으로 '루트로 실행'해야하는 사람들을 위해 나는 의심 할 여지없이 매우 안전하지 않은 사악한 끔찍한 해킹을 발견했습니다.
sshpass -p 'userpass' ssh -T -p port user@server << EOSSH
sudo -S su - << RROOT
userpass
echo ""
echo "*** Got Root ***"
echo ""
#[root commands go here]
useradd -m newuser
echo "newuser:newpass" | chpasswd
RROOT
EOSSH
chpasswd를 사용할 수 있습니다.
echo $1:$2 | chpasswd
Have you looked at the -p option of adduser (which AFAIK is just another name for useradd)? You may also want to look at the -P option of luseradd which takes a plaintext password, but I don't know if luseradd is a standard command (it may be part of SE Linux or perhaps just an oddity of Fedora).
Tested this on a CentOS VMWare image that I keep around for this sort of thing. Note that you probably want to avoid putting passwords as command-line arguments, because anybody on the entire machine can read them out of 'ps -ef'.
That said, this will work:
user="$1"
password="$2"
adduser $user
echo $password | passwd --stdin $user
Sometimes it is useful to set a password which nobody knows. This seems to work:
tr -dc A-Za-z0-9 < /dev/urandom | head -c44 | passwd --stdin $user
I stumbled upon the same problem and for some reason the "--stdin" option was not available on the version of passwd I was using (shipped on a Ubuntu 14.04).
If any of you happen to experiment the same, you can work it around as I did: by using the chpasswd command like this:
echo "<user>:<password>" | chpasswd
This is the definitive answer for a teradata node admin.
Go to your /etc/hosts file and create a list of IP's or node names in a text file.
SMP007-1
SMP007-2
SMP007-3
Put the following script in a file.
#set a password across all nodes
printf "User ID: "
read MYUSERID
printf "New Password: "
read MYPASS
while read -r i; do
echo changing password on "$i"
ssh root@"$i" sudo echo "$MYUSERID":"$MYPASS" | chpasswd
echo password changed on "$i"
done< /usr/bin/setpwd.srvrs
Okay I know I've broken a cardinal security rule with ssh and root but I'll let you security folks deal with it.
Now put this in your /usr/bin subdir along with your setpwd.srvrs config file.
When you run the command it prompts you one time for the User ID then one time for the password. Then the script traverses all nodes in the setpwd.srvrs file and does a passwordless ssh to each node, then sets the password without any user interaction or secondary password validation.
You can use the expect utility to drive all programs that read from a tty (as opposed to stdin, which is what passwd does). Expect comes with ready to run examples for all sorts of interactive problems, like passwd entry.
참고URL : https://stackoverflow.com/questions/714915/using-the-passwd-command-from-within-a-shell-script
'IT TIP' 카테고리의 다른 글
| Underscore.js : 객체에있는 키를 사용하여 객체 목록에서 맵을 만듭니다. (0) | 2020.12.09 |
|---|---|
| Android Studio의 미리보기에서 "리소스를 확인할 수 없습니다" (0) | 2020.12.09 |
| Rails, 모델에서 뷰 / 부분을 렌더링하는 방법 (0) | 2020.12.09 |
| bower 프록시 구성 (0) | 2020.12.09 |
| Linux 셸에서 정규식을 사용하여 파일에서 IP 주소를 어떻게 추출합니까? (0) | 2020.12.09 |