• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
Hw05-Command-Line-Enviroment

Hw05-Command-Line-Environment

Job Control

  1. using command pkill and pgrep to kill the tasks typed sleep 2000
lee@LAPTOP-6VVDNARH:~$ sleep 20000
^Z
[1]+  Stopped                 sleep 20000
lee@LAPTOP-6VVDNARH:~$ jobs
[1]+  Stopped                 sleep 20000
lee@LAPTOP-6VVDNARH:~$ pgrep sleep
6472
lee@LAPTOP-6VVDNARH:~$ pkill sleep
[1]+  Terminated              sleep 20000
lee@LAPTOP-6VVDNARH:~$ jobs

image-20220712212002339

  1. Say you don’t want to start a process until another completes. How would you go about it? In this exercise, our limiting process will always be sleep 60 &. One way to achieve this is to use the wait command. Try launching the sleep command and having an ls wait until the background process finishes.

image-20220715083313663

​

Well, I've try these command, but it didn't work in different bash sessions.

​ kill -0 pid did not return signal, but it will return nonzero status number while the proceed doesn't exist.

​ It's important that while in bash normally act if the statements is 1

but it will be 0 if you are using command!

​ In order to do not consume un-nesseary resource, we should sleep 1

#!/bin/bash
pid={$1}

pid_wait(){
        while kill -0 $1
        do
        sleep 1
        done
        ls
}

image-20220715085858848

Aliases

  1. Create an alias dc that resolves to cd for when you type it wrongly.

image-20220715090202718

alias dc=cd

2.Run history | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 10 to get your top 10 most used commands and consider writing shorter aliases for them. Note: this works for Bash; if you’re using ZSH, use history 1 instead of just history.

image-20220715090457275

image-20220715090526045

Dotfiles

  1. Create a folder for your dotfiles and set up version control.

    mkdir ~/dotfile
    
  2. Add a configuration for at least one program, e.g. your shell, with some customization (to start off, it can be something as simple as customizing your shell prompt by setting $PS1).

    git init dotfiles
    

    image-20220715091250869

  3. Set up a method to install your dotfiles quickly (and without manual effort) on a new machine. This can be as simple as a shell script that calls ln -s for each file, or you could use a specialized utility.

    #!bin/bash
    files="bashrc vimrc "
    
    for files in $files; do
            ln -s ~/dotfiles/$file ~/.$files
    do
    

    ln -s will create a symlinks for you dotfiles that your dotfiles will be well to control also version control

Remote Machines

Install a Linux virtual machine (or use an already existing one) for this exercise. If you are not familiar with virtual machines check out this tutorial for installing one.

  1. Go to ~/.ssh/ and check if you have a pair of SSH keys there. If not, generate them with ssh-keygen -o -a 100 -t ed25519. It is recommended that you use a password and use ssh-agent , more info here.

image-20220715093835206

  1. Edit .ssh/config to have an entry as follows

     Host vm
         User username_goes_here
         HostName ip_goes_here
         IdentityFile ~/.ssh/id_ed25519
         LocalForward 9999 localhost:8888
    
  2. Use ssh-copy-id vm to copy your ssh key to the server.

image-20220715102928577

Then, we can use ssh vm directly login the server without password

image-20220715103515065

  1. Start a webserver in your VM by executing python -m http.server 8888. Access the VM webserver by navigating to http://localhost:9999 in your machine.

image-20220715103756861

image-20220715104219868

Recently , We use config LocalForward 9999 localhost:8888 to make ssh host 8888 to our laptop 9999

  1. Edit your SSH server config by doing sudo vim /etc/ssh/sshd_config and disable password authentication by editing the value of PasswordAuthentication. Disable root login by editing the value of PermitRootLogin. Restart the ssh service with sudo service sshd restart. Try sshing in again.

image-20220715104737524

  1. (Challenge) Install mosh in the VM and establish a connection. Then disconnect the network adapter of the server/VM. Can mosh properly recover from it?
sudo apt install mosh

Well, this answer I did not do that.

  1. Challenge) Look into what the -N and -f flags do in ssh and figure out a command to achieve background port forwarding.
-N      Do not execute a remote command.  This is useful for just forwarding ports.
-f      Requests ssh to go to background just before command execution.  This is
        useful if ssh is going to ask for passwords or passphrases, but the user
        wants it in the background.  This implies -n.  The recommended way to
        start X11 programs at a remote site is with something like ssh -f host
        xterm.
-L [bind_address:]port:host:hostport
     -L [bind_address:]port:remote_socket
     -L local_socket:host:hostport
     -L local_socket:remote_socket

so the command will be the following

ssh -fN -L 9999:localhost:8888 vm

image-20220715105528868

posted on 2022-07-15 10:56  Jack404  阅读(18)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3