fr-itaya / training

0 stars 0 forks source link

Linux - コマンドを使う #3

Closed fr-sato closed 10 years ago

fr-sato commented 10 years ago

■目的

Windowsではマウスをクリックするれば大抵のことはできますが、Linuxではコマンドを使って操作していきます。ここではPuTTYを用いて基本的なコマンドを覚えてもらいます。

■アクセス方法

自分のPCからPuTTYを使用して開発環境にリモートアクセスします。

■課題

下記のコマンドを実行しその結果を確認して下さい。入力したコマンドや実行前、実行後の確認や出力結果は、Issueのコメントに記述して下さい。記述する際にはMarkdown記述を用いて見やすいよう工夫して下さい。

  1. ホームディレクトリの下にworkディレクトリを作成
  2. workディレクトリ内に移動
  3. その中にhello.shを作成
  4. エディタを使いhello.shを次の通り編集 echo "hello world";
  5. hello.shをコマンドで実行
  6. 自分以外のユーザーがhello.shを読み書き実行出来ないようパーミッションを変更
  7. hello.shをhello_copy.shにコピー
  8. hello.shをhello_move.shにリネーム
  9. hello_move.shを削除
  10. ホームディレクトリに戻る
  11. workディレクトリをwork_copyにコピー
  12. work_copyディレクトリをwork_moveにリネーム
  13. work_moveディレクトリを削除
  14. 今まで発行したコマンドの履歴を確認
  15. 14からlsコマンドを使用した行だけを表示
  16. lsコマンドのヘルプを表示
  17. 自分のアカウントのパスワードを変更
  18. 自分のsambaアカウントを追加
fr-itaya commented 10 years ago

本課題の工数見積もり(4/7分)は以下の通りとします。


  1. ホームディレクトリの下にworkディレクトリを作成
  2. workディレクトリ内に移動
  3. その中にhello.shを作成
  4. エディタを使いhello.shを次の通り編集 echo "hello world";
  5. hello.shをコマンドで実行
  6. 自分以外のユーザーがhello.shを読み書き実行出来ないようパーミッションを変更

ここまでを夜礼までの1時間半で行います。 宜しくお願い致します。

fr-itaya commented 10 years ago
1. ホームディレクトリの下にworkディレクトリを作成
2. workディレクトリ内に移動
3. その中にhello.shを作成
4. エディタを使いhello.shを次の通り編集 echo "hello world";
5. hello.shをコマンドで実行
6. 自分以外のユーザーがhello.shを読み書き実行出来ないようパーミッションを変更

夜礼が近いので本日はここまでとし、7以降は明日行います。

fr-sato commented 10 years ago

コマンドと出力結果は基本的にOKです。

入力したコマンドや実行前、実行後の確認や出力結果は、Issueのコメントに記述して下さい。

ただ上記の仕様は満たしていないです。例えば1や3の場合、ディレクトリやファイルが作成されたかどうかlsコマンドを使って確認する所までしないと、本当に作成したかどうかの確認にはならないです。

$ touch hello.sh
$ ls -l hello.sh 
-rw-r--r--  1 ***  ***  0  4  7 18:42 hello.sh

あとコマンドの実行結果を残す時は↑のようになるべく全ての履歴を残すようにしましょう、そうすると他の人が確認したり、自分で後から確認した時に状況を把握しやすくなります。

のでその辺を意識して再度 https://github.com/fr-itaya/training/issues/3#issuecomment-39710513 を書き直してみて下さい。

fr-itaya commented 10 years ago

本日の作業は14:05開始、工数見積もりは4時間と致します。 内訳は下記の通りです。


宜しくお願い致します。

fr-itaya commented 10 years ago

https://github.com/fr-itaya/training/issues/3#issuecomment-39710513 修正です。


1. ホームディレクトリの下にworkディレクトリを作成
$ mkdir work
$ ls -l
drwxrwxr-x 2 *** ***  4096 Apr  8 05:15 work
2. workディレクトリ内に移動
$ cd work
$ pwd
/home/***/work
3. その中にhello.shを作成
$ touch hello.sh
$ ls -l
-rw-rw-r-- 1 *** *** 0 Apr  8 05:24 hello.sh
4. エディタを使いhello.shを次の通り編集 echo "hello world";
$ ls -l
total 0
-rw-rw-r-- 1 *** *** 0 Apr  8 05:24 hello.sh
$ vim hello.sh
$ ls -l
total 4
-rw-rw-r-- 1 *** *** 20 Apr  8 05:30 hello.sh
5. hello.shをコマンドで実行
$ ./hello.sh
-bash: ./hello.sh: Permission denied
6. 自分以外のユーザーがhello.shを読み書き実行出来ないようパーミッションを変更
$ ls -l
total 4
-rw-rw-r-- 1 *** *** 20 Apr  8 05:30 hello.sh
$ chmod 700 hello.sh
$ ls -l
total 4
-rwx------ 1 *** *** 20 Apr  8 05:30 hello.sh
$ ./hello.sh
hello world

続いて14:50から7以降を実行、出力結果の確認に入ります。

fr-itaya commented 10 years ago
7. hello.shをhello_copy.shにコピー
$ cp hello.sh hello_copy.sh
$ ls -l
total 8
-rwx------ 1 *** *** 20 Apr  8 06:02 hello_copy.sh
-rwx------ 1 *** *** 20 Apr  8 05:30 hello.sh
8. hello.shをhello_move.shにリネーム
$ mv hello.sh hello_move.sh
$ ls -l
total 8
-rwx------ 1 *** *** 20 Apr  8 06:02 hello_copy.sh
-rwx------ 1 *** *** 20 Apr  8 05:30 hello_move.sh
9. hello_move.shを削除
$ rm hello_move.sh
$ ls -l
total 4
-rwx------ 1 *** *** 20 Apr  8 06:02 hello_copy.sh
10. ホームディレクトリに戻る
$ pwd
/home/***/work
$ cd
$ pwd
/home/***
11. workディレクトリをwork_copyにコピー
$ ls -l
total 116
-rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 doc
-rw-rw-r-- 1 *** *** 44645 Apr  7 06:16 ds.zip
drwxrwxr-x 2 *** ***  4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 nerdtree_plugin
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 plugin
drwxrwxr-x 3 *** ***  4096 Mar 20 02:49 public_html
-rw-rw-r-- 1 *** *** 43528 Apr  4 07:38 TEST
drwxrwxr-x 2 *** ***  4096 Apr  8 06:08 work
$ ls -l  work
total 4
-rwx------ 1 *** *** 20 Apr  8 06:02 hello_copy.sh
$ cp -Rv work work_copy
`work' -> `work_copy'
`work/hello_copy.sh' -> `work_copy/hello_copy.sh'
12. work_copyディレクトリをwork_moveにリネーム
$ ls -l
total 120
-rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 doc
-rw-rw-r-- 1 *** *** 44645 Apr  7 06:16 ds.zip
drwxrwxr-x 2 *** ***  4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 nerdtree_plugin
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 plugin
drwxrwxr-x 3 *** ***  4096 Mar 20 02:49 public_html
-rw-rw-r-- 1 *** *** 43528 Apr  4 07:38 TEST
drwxrwxr-x 2 *** ***  4096 Apr  8 06:08 work
drwxrwxr-x 2 *** ***  4096 Apr  8 06:15 work_copy
$ mv work_copy work_move
$ ls -l
total 120
-rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 doc
-rw-rw-r-- 1 *** *** 44645 Apr  7 06:16 ds.zip
drwxrwxr-x 2 *** ***  4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 nerdtree_plugin
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 plugin
drwxrwxr-x 3 *** ***  4096 Mar 20 02:49 public_html
-rw-rw-r-- 1 *** *** 43528 Apr  4 07:38 TEST
drwxrwxr-x 2 *** ***  4096 Apr  8 06:08 work
drwxrwxr-x 2 *** ***  4096 Apr  8 06:15 work_move
13. work_moveディレクトリを削除
$ rm -r work_move
$ ls -l
total 116
-rw-rw-r-- 1 *** ***   952 Apr  7 06:18 dl.tar.gz
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 doc
-rw-rw-r-- 1 *** *** 44645 Apr  7 06:16 ds.zip
drwxrwxr-x 2 *** ***  4096 May 26  2010 gist414375-24c6049f621f07db839e8cdd133e396756d642db
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 nerdtree_plugin
drwxrwxr-x 2 *** ***  4096 Apr  7 06:17 plugin
drwxrwxr-x 3 *** ***  4096 Mar 20 02:49 public_html
-rw-rw-r-- 1 *** *** 43528 Apr  4 07:38 TEST
drwxrwxr-x 2 *** ***  4096 Apr  8 06:08 work
14. 今まで発行したコマンドの履歴を確認
$ history
    1  sudo yum update
    2  yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libffi-devel openssl-devel git vim tmux
    3  sudo su -
    4  mkdir public_html
    5  mkdir public_html/training
15. 14からlsコマンドを使用した行だけを表示
$ history|grep ls
  139  ls -l
  143  ls -l
  147  ls -l
  150  ls -l
  153  ls -l
16. lsコマンドのヘルプを表示
$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print octal escapes for nongraphic characters
      --block-size=SIZE      use SIZE-byte blocks.  See SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information)
                               with -l: show ctime and sort by name
                               otherwise: sort by ctime
  -C                         list entries by columns
      --color[=WHEN]         colorize the output.  WHEN defaults to `always'
                               or can be `never' or `auto'.  More info below
  -d, --directory            list directory entries instead of contents,
                               and do not dereference symbolic links
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append `*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files.
                               augment with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l, print sizes in human readable format
                               (e.g., 1K 234M 2G)
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                             that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k                         like --block-size=1K
  -l                         use a long listing format
  -L, --dereference          when showing file information for a symbolic
                               link, show information for the file the link
                               references rather than for the link itself
  -m                         fill width with a comma separated list of entries
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print raw entry names (don't treat e.g. control
                               characters specially)
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of non graphic characters
      --show-control-chars   show non graphic characters as-is (default
                             unless program is `ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always, c, escape
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -s, --size                 print the allocated size of each file, in blocks
  -S                         sort by file size
      --sort=WORD            sort by WORD instead of name: none -U,
                             extension -X, size -S, time -t, version -v
      --time=WORD            with -l, show time as WORD instead of modification
                             time: atime -u, access -u, use -u, ctime -c,
                             or status -c; use specified time as sort key
                             if --sort=time
      --time-style=STYLE     with -l, show times using style STYLE:
                             full-iso, long-iso, iso, locale, +FORMAT.
                             FORMAT is interpreted like `date'; if FORMAT is
                             FORMAT1<newline>FORMAT2, FORMAT1 applies to
                             non-recent files and FORMAT2 to recent files;
                             if STYLE is prefixed with `posix-', STYLE
                             takes effect only outside the POSIX locale
  -t                         sort by modification time
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time
                               with -l: show access time and sort by name
                               otherwise: sort by access time
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           assume screen width instead of current value
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -1                         list one file per line

SELinux options:

  --lcontext                 Display security context.   Enable -l. Lines
                             will probably be too wide for most displays.
  -Z, --context              Display security context so it fits on most
                             displays.  Displays only mode, user, group,
                             security context and file name.
  --scontext                 Display only security context and file name.
      --help     display this help and exit
      --version  output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
KB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).

Report ls bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'ls invocation'
17. 自分のアカウントのパスワードを変更
$ sudo passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
18. 自分のsambaアカウントを追加
$ sudo smbpasswd -a ***
New SMB password:
Retype new SMB password:
tdbsam_open: Converting version 0.0 database to version 4.0.
tdbsam_convert_backup: updated /etc/samba/passdb.tdb file.
account_policy_get: tdb_fetch_uint32 failed for type 1 (min password length), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 2 (password history), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 3 (user must logon to change password), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 4 (maximum password age), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 5 (minimum password age), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 6 (lockout duration), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 7 (reset count minutes), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 8 (bad lockout attempt), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 9 (disconnect time), returning 0
account_policy_get: tdb_fetch_uint32 failed for type 10 (refuse machine password change), returning 0
Added user ***.
fr-sato commented 10 years ago

17. 自分のアカウントのパスワードを変更

$ sudo passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

出力結果をよく見てほしいのですが、これはrootユーザーのパスワードを変更しています。変更して欲しいのは自分のアカウントのパスワードなのでそれを変更して下さい。

  1. sudoでec2-userのパスワードを変更
  2. ec2-userでsudo使わずにパスワード変更

を試して下さい。

それ以外はOKです。

fr-itaya commented 10 years ago

レビュー頂き、ありがとうございます。 17-1, 2を試しましたので、ご確認よろしくお願いいたします。


17. 自分のアカウントのパスワードを変更
  1. sudoでec2-userのパスワードを変更
    • 使用コマンド $ sudo passwd (username)
    • 実行結果
$ sudo passwd ***
Changing password for user ***.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
  1. ec2-userでsudo使わずにパスワード変更
    • 使用コマンド $ passwd
    • 実行結果
$ passwd
Changing password for user ***.
Changing password for ***.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
fr-sato commented 10 years ago

OKです、お疲れ様でした。

fr-itaya commented 10 years ago

Software Design 2014年6月号p55-89、特集「UNIX必須コマンドトレーニング」より 新たな知見が得られましたので、ここに纏めます。

  1. コマンドを探すとき
    • whatis:コマンド名からマニュアルの要約を検索・表示
      • オプション-w:ワイルドカード(*)を使う
    • apropos:引数に渡された文字列をコマンドの名前と要約から検索・表示
  2. パイプと標準入力/標準出力/標準エラー出力
    • UNIXコマンドの出力結果は、別コマンドの入力にしたり、ファイルへの出力にしたり出来る。
    • UNIXコマンドは3つの入出力チャンネルを持つ
      • 標準入力:プロセスへの入力(キーボード)
      • 標準出力:プロセスの結果を出力(ディスプレイ)
      • 標準エラー出力:プロセスのエラーメッセージを出力(ディスプレイ)
    • コマンドの出力結果を他のコマンドに渡すには、パイプ( | )を使ってコマンドを連結すれば良い。
    • コマンドの出力結果をファイルへ出力するには、リダイレクト機能を使う。 以下のコマンドはどちらもファイルへの出力コマンドだが、違いは次の通り。
    • >:既にファイルが存在する場合、空にして書込む【取扱注意】
    • >>:既にファイルが存在する場合、末尾に追記する

今日は一先ずここまで。