"dd" 명령을 안전하게 만들 수 있나요?

"dd" 명령을 안전하게 만들 수 있나요?

우리 모두(또는 누군가를 알고 있음)는 실수로 destroy-disk(dd) 주문하다. 다음이나 유사한 방법으로 명령을 변경하는 방법(있는 경우)은 무엇입니까? /dev/sda출력 파일( of=/dev/sda)로 제공되면 명령이 실행되지 않거나 "계속하시겠습니까?"와 같은 확인 메시지가 표시되지 않습니다.

.bashrc 파일로 비슷한 것을 얻을 수 있습니까?

일반적으로 특정 매개변수를 전달할 때 특정 명령의 실행을 중지하는 방법이 있습니까?

편집하다: 이 명령은 루트로 실행됩니다.

답변1

Arkadiusz가 말했듯이 래퍼를 만들 수 있습니다.

dd() {
  # Limit variables' scope
  local args command output reply

  # Basic arguments handling
  while (( ${#} > 0 )); do
    case "${1}" in
    ( of=* )
      output="${1#*=}"
      ;;
    ( * )
      args+=( "${1}" )
      ;;
    esac
    shift || break
  done

  # Build the actual command
  command=( command -- dd "${args[@]}" "of=${output}" )

  # Warn the user
  printf 'Please double-check this to avoid potentially dangerous behavior.\n' >&2
  printf 'Output file: %s\n' "${output}" >&2

  # Ask for confirmation
  IFS= read -p 'Do you want to continue? (y/n): ' -r reply

  # Check user's reply
  case "${reply}" in
  ( y | yes )
    printf 'Running command...\n' >&2
    ;;
  ( * )
    printf 'Aborting\n' >&2
    return
    ;;
  esac

  # Run command
  "${command[@]}"
}

예:

$ dd if=/dev/urandom of=file.txt bs=4M count=5
Please double-check this to avoid potentially dangerous behavior.
Output file: file.txt
Do you want to continue? (y/n): y
Running command...
5+0 records in
5+0 records out
20971520 bytes (21 MB, 20 MiB) copied, 0.443037 s, 47.3 MB/s

필요에 맞게 수정하세요(POSIX 호환으로 만들기, 다른 조건 확인 등).

답변2

AFAIK, 슈퍼유저가 아닌 사용자에 대한 재정의는 없습니다 /dev/sda. 슈퍼유저 액세스 권한이 부여된 운영자를 신뢰할 수 없다면 그보다 더 큰 문제가 발생하게 됩니다 /bin/dd.

관련 정보