# Linux Setup # Install VirtualBox [Download VirtualBox](https://www.virtualbox.org/wiki/Downloads) # Install Vagrant [Download Vagrant](https://www.vagrantup.com/downloads) ***Vagrant*** is a **CLI** utility that manages the lifecycle of your virtual machines. The virtual machine platforms include vSphere, Hyper-v, Virtual Box and also Docker. # Setup ```= cd /path/to/wks vim VAGRANTFILE ========== Content ========== Vagrant.configure("2") do |config| config.vm.box = "chenhan/ubuntu-desktop-20.04" config.vm.provider :virtualbox do |v| v.memory = 2048 v.cpus = 2 v.customize ["modifyvm", :id, "--vram", "128"] end end ============================= ``` > The `.box` chooses an image from [here](https://app.vagrantup.com/boxes/search). > The `.provider` is a virtual machine platform. > The rest of content are resource limitations. ```= vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'chenhan/ubuntu-desktop-20.04' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'chenhan/ubuntu-desktop-20.04' default: URL: https://vagrantcloud.com/chenhan/ubuntu-desktop-20.04 ==> default: Adding box 'chenhan/ubuntu-desktop-20.04' (v20200424) for provider: virtualbox default: Downloading: https://vagrantcloud.com/chenhan/boxes/ubuntu-desktop-20.04/versions/20200424/providers/virtualbox.box default: ==> default: Successfully added box 'chenhan/ubuntu-desktop-20.04' (v20200424) for 'virtualbox'! ==> default: Importing base box 'chenhan/ubuntu-desktop-20.04'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'chenhan/ubuntu-desktop-20.04' version '20200424' is up to date... ==> default: Setting the name of the VM: 90DaysDevOps_default_1659320603010_92271 Vagrant is currently configured to create VirtualBox synced folders with the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant guest is not trusted, you may want to disable this option. For more information on this option, please refer to the VirtualBox manual: https://www.virtualbox.org/manual/ch04.html#sharedfolders This option can be disabled globally with an environment variable: VAGRANT_DISABLE_VBOXSYMLINKCREATE=1 or on a per folder basis within the Vagrantfile: config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 (guest) => 2222 (host) (adapter 1) ==> default: Running 'pre-boot' VM customizations... ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. default: default: Inserting generated public key within guest... default: Removing insecure key from the guest if it's present... default: Key inserted! Disconnecting and reconnecting using new SSH key... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: The guest additions on this VM do not match the installed version of default: VirtualBox! In most cases this is fine, but in rare cases it can default: prevent things such as shared folders from working properly. If you see default: shared folder errors, please make sure the guest additions within the default: virtual machine match the version of VirtualBox you have installed on default: your host and reload your VM. default: default: Guest Additions Version: 5.2.34 default: VirtualBox Version: 6.1 ==> default: Mounting shared folders... default: /vagrant => D:/opt/repos/90DaysDevOps vagrat ssh ``` If you are using GUI view in VirtualBox, get credential issue. The default account/password is `vagrant`/`vagrant`. # Commands List documented commands ```= man man ``` Execute with superuser ```= sudo command su root command exit ``` Move cursor to top ```= clear ``` File management ```bash= ls -al [PATH] mkdir DIR_NAME cd DIR_NAME touch CREATED_FILE mv FILE TO_DIR/ cp FILE TO_DIR/ mv FILE RENAME rm FILE rm -R DIR ``` File access ```bash= echo "content_appended" >> FILE cat PRINT_ALL_CONTENT cat FILE | grep "STRING_MATCH" # column filter command | awk '{print $1}' # sort is sorting by lines # xargs combines lines into oneline and with gap of a space cut -d"DELEMETER" -f"COL_FROM_1" < FILENAME | sort | xargs ``` Current location ```= pwd ``` File search ```= find FROM_DIR [options] PATTERN [options] -name -iname // case-insensitive -regex ``` Command history ```bash= history # Clean history history -c # Settings echo 'export HISTTIMEFORMAT="%d-%m-%Y %T "' >> ~/.bash_profile echo 'export HISTSIZE=100000' >> ~/.bash_profile echo 'export HISTFILESIZE=10000000' >> ~/.bash_profile ``` > Be careful to write sensitive content in commands User management ```bash= password sudo useradd USER_NAME sudo groupadd GROUP_NAME sudo usermod USER_NAME -a -G GROUP_NAME # Add sudo group to an user usermod -a -G sudo USER_NAME chmod -R [authority] FILE_NAME # [authority] # rwx = 421 = 4+2+1 = 7 chown -R USER_NAME[:GROUP_NAME] FILE_NAME ``` # System Management ```bash= sudo apt update sudo apt install PACKAGE sudo apt remove PACKAGE ``` Install from other package repository ```bash= curl -fsSL https://apt.release.../gpg | sudo apt-key add - sudo apt-add-repository "deb [arch=amd64] https://apt.release... ($lsb_release -cs) main" ``` Storage information ```bash= # Show disk and its partitions lsblk df -f ``` Add new disk ```bash= sudo mkfs -t ext4 /dev/sdb sudo mkdir MNT_DIR sudo mount /dev/sdb MNT_DIR sudo umount /dev/sdb ``` > Automatically mount the disk at boot, adds the disk information to `/etc/fstab`, or you have to do the same thing above. # System Directories - `/bin` - binary files - `/sbin` - binary files for superuser - `/boot` - configurations for boot - `/proc` - knernel & process information - `/dev` - device information - `/run` - application states - `/etc` - configuration files - `/home` - for user directory - `/lib` - shared libs for those binary files - `/usr` - installed software packages - `/opt` - optional software packages - `/var` - dynamic files, like application logs - `/tmp` - temporary files - `/mnt` - temporary mount point - `/media` - removable devices # Text editor ## nano ```= nano FILENAME ``` ## vim ```= vim FILENAME ===== normal mode ===== h // left arrow j // down arrow k // up arrow l // right arrow yy // copy line p // Paste at next line P // Paste at previous line dd // remove line x // delete next character X // delete previous character u // undo ======================= ===== command mode ===== ESC :q // exit :w // write data :%s/STRING_REPLACED/NEW_STRING ======================== ===== insert mode ===== i // write at curser a // write behind curser with one character ======================= ===== visual mode ===== ======================= ``` # SSH ```bash= sudo systemctl status ssh ``` If no ssh server exist ```bash= sudo apt install OpenSSH-server sudo ufw allow ssh ``` Setup network bridge in VirtualBox ```= Right Click container -> Network -> Port Transfering -> Set port in Host and port(22 for ssh) in Client ``` ssh without password ```bash= # create key in client machine ssh-keygen -t ed25519 # upload key to remote machine ssh-copy-id vagrant@localhost -p PORT ``` ssh denides password log in ```bash= # In remote machine sudo vim /etc/ssh/sshd_config # Modify PasswordAuthentication PasswordAuthentication no sudo systemctl reload sshd ``` # Script Script context ```= ===== Script Content ===== #!/bin/bash ...commands ========================== chmod +x SCRIPT.sh ./SCRIPT.sh ``` Standard input ```bash= #!/bin/bash echo "enter name" read name # No display of input read -s password echo "Your name is $name" ``` Conditions ```bash= #!/bin/bash income=20 if [ $income -eq 10 ] then echo eq10 elif [ $income -gt 10 ] then echo gt10 else echo "else" fi ``` > `eq`, `ne`, `gt`, `ge`, `lt`, `le` for `==`, `!=`, `>`, `>=`, `<`, `<=`. > `-d FILENAME`, is `FILENAME` a directory > `-e FILENAME`, is `FILENAME` existed > `-f FILENAME`, is `FILENAME` a file > `-r FILENAME`, is `FILENAME` writable > `-s FILENAME`, is `FILENAME` non-zero size Arguments ```bash= #!/bin/bash # First agrument, not command name echo $1 ``` # Others [Other Notes](https://hackmd.io/FZVVtNnERRmYURUvXYuNPw) ###### tags: `Linux`