Friday, 22 March 2019

Useful Linux commands

User management


To request security privileges of superuser (root):

$ sudo 


/usr/local/etc/sudoers

To log in and run the current shell as root use:

user@computer:~$ sudo -i
root@computer:~# whoami
root
root@computer:~# exit

logout
user@computer:~$

.profile file 


There is one global profile file (executed when anyone logs in):

/etc/profile

There are three user-specific bash profile files (executed when current/specific user logs in):

~/.profile
~/.bash_profile
~/.bashrc

If ~/.profile doesn't exist, just create it.

This is the comment at the beginning of ~/.profile:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022

To add en environment variable during the session of a particular user (and also make them available in terminal) append the desired var name and its value to ~/,profile file. Example:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

We'd need to restart the terminal in order to get these changes visible but to make terminal fetch them in the current session, we can update the current shell session with:

source ~/.profile


Permissions


Chmod permissions (flags) explained

To allow only file user to read and write (but not to execute):

$ chmod 600 filename

Working with directories


To list the content of the directory:

ls

To list all files (including hidden) in the current directory:

ls -a

To list files in some specific directory use:

$ ls target_directory

Example:
$ ls /usr/local/go/

To list directories and files in form of a tree install tree package:

$ sudo apt install tree

...and use it as e.g.:

$ tree -I *node*

-I = ignores directories that match given pattern

Working with files

Creating a file


To create a file use touch:

$ touch filename

Getting the information about a file


To see the last couple of lines in the file use command tail:

$ tail myfile

Copying files


cp - copy


File viewing and editing

To simply view the content of some file, use cat:

$ cat filename

To edit some file, you can use vi editor. Example:

$ vi ~/.profile 

gedit can also be used as graphic editor:

sudo gedit ~/.profile


Symbolic links

ln

$ sudo ln -s /usr/local/go/bin/go /usr/local/bin/go

Hard links


How to create hardlink of one file in different directories in linux


Working with Environment Variables

To list all environment variables and their values use:

$ env

To set environment variables for the single command:

Example:

$ env GOOS=linux GOARCH=amd64 go build cmd/main.go

From the executable's point of view, the same would have been achieved without using env:

$ GOOS=linux GOARCH=amd64 go build cmd/main.go

To set environment variables for the current terminal session:

$ export GOPATH=/mnt/c/dev/go

export is a bash builtin. export key=value is extended syntax and should not be used in portable scripts (i.e. #! /bin/sh)

What's the difference between set, export and env and when should I use each?
What is the difference between set, env, declare and export when setting a variable in a Linux shell?


Package management


apt (Advanced Packaging Tool) - It is not a command itself but a package which contains set of tools which manage installation and removal of other packages.

apt-get

apt-get update - downloads the package lists from the repositories and "updates" them to get information on the newest versions of packages and their dependencies. It will do this for all repositories and PPAs.

http://askubuntu.com/questions/222348/what-does-sudo-apt-get-update-do

apt-cche


add-apt-repository - adds a repository to the list of repositories


To apply latest security updates on Ubuntu:

sudo apt-get update

sudo apt-get -y upgrade


Difference Between apt and apt-get Explained

Should I use apt or apt-get?

You might be thinking if you should use apt or apt-get. And as a regular Linux user, my answer is to go with apt.

apt is the command that is being recommended by the Linux distributions. It provides the necessary option to manage the packages. Most important of all, it is easier to use with its fewer but easy to remember options.

I see no reason to stick with apt-get unless you are going to do specific operations that utilize more features of apt-get.


-----------------------------------------------------------------------------------------------------------------------


To download a file into some specific directory:

cd /dest_dir
wget https://example.com/archive_file.tar.gz


To install a package/software in Ubuntu, it is usually enough to copy it to /usr/local directory.
To move dir1 to some other location e.g. /usr/local use:

mv new_app /usr/local


A word on Linux directories

[source]

/usr/src is meant for source code for the binaries that come with your system's installation. For example, it could contain the source code for your kernel, tools such as ls, passwd, cp, mv etc, which are all installed BY YOUR DISTRIBUTION. If you upgrade your OS from source, all the source code would go here, when you rebuild your system. You DON'T want to put any software that you install BY YOURSELF in here, because they may get overwritten when you upgrade your system. In general, files that are in /, /usr/bin, /usr/sbin, /bin, /sbin etc. have their source code in /usr/src.

The /usr/local directory tree is meant to be used for software that you install by yourself, without using the distribution CD(s). For example, /usr/local/bin, /usr/local/sbin are for the binaries that are installed by you separately, /usr/local/etc is for config files etc. Thus /usr/local/src is for source files that you yourself downloaded.

Example: go (binary distribution) gets installed in /usr/local/go.

If you upgrade your system, files under the /usr tree get overwritten, such as /usr/bin, /usr/sbin, /usr/src etc. However, anything under /usr/local will not be touched. This is why all the software you installed separately should go to /usr/local tree.



Installing Software


How to install software distributed via .deb files?  


$ sudo dpkg -i /path/to/deb/file 
$ sudo apt-get install -f

The latter is necessary in order to fix broken packages (install eventual missing dependencies).

How to install a deb file, by dpkg -i or by apt?



Working with Archive files


To unpack the .tar.gz in the current directory use:

tar -xvf archive_file.tar.gz

-x = extract
-f (--file) = use archive file
-c (--verbose) = verbose output
-z (--gzip, --gunzip, --ungzip)  = filter the archive through gzip

To unpack the .tar.gz in the specific output directory use:

tar zxvf archive_file.tar.gz output_directory

Or:

$ tar -C path/to/dest_dir -xzf archive_file.tar

-C (--directory) stands for "Change to directory"

Example:
$ tar -C /usr/local -xzf go1.12.1.linux-amd64.tar


Hardware management

To verify if you're running a 64-bit system:

uname -m 

x86_64 is the output in case of 64-bit system.

SSH


How to test password for private key?
If id_rsa and id_rsa.pub is a keypair, we can execute (after cd ~/.ssh/):

$ ssh-keygen -y -f id_rsa

...which will prompt us to enter the password. If correct, this will output the public key.

-y = This option will read a private OpenSSH format file and print an OpenSSH public key to stdout.
-f filename = Specifies the filename of the key file.

Another example:

$ ssh-keygen -t rsa -C "your_email@example.com"

-t = Specifies the type of key to create.  The possible values are “dsa”, “ecdsa”, “ed25519”, or “rsa”.
-C comment = Provides a new comment.

To copy the contents of the id_rsa.pub file to clipboard:

$ xclip -sel clip < ~/.ssh/id_rsa.pub

No comments: