Ubuntu
Basics
:x (Anytime you are in VIM, MAN page, LESS, etc, this is how you exit)
whereis bash (find absolute location of bash, or any file)
whatis ls (tells you about the command)
man ls (manual to give you a list of all command arguments for any command)
locate crontab (I like mlocate more)
which python (location of a program)
clear (dlears the terminal)
date (current datetime)
echo (output to terminal)
env (See environment variables)
hostname (See your hostname)
Apt
Apt (Or Aptitude) is the package manager for Ubuntu to manage packages and remove them.
Tip: You can use the
-y
flag in any apt command to skip the[Y/n]
dialog.
Apt Install
You need super user permissions, or sudo
before the command.
To install packages, let's use an example such as ruby which should have a list of items.
sudo apt-get install rub<TAB> ; Press the tab key to see a list, type :q to exit the list
sudo apt-get install ruby2.3
Apt Update
This updates the sources list located in /etc/apt/sources.list.d/
which is where the OS knows where to download files from. You will do this time to time if it's been a while.
sudo apt-get update
Apt Upgrade
This will upgrade packages that have newer versions.
sudo apt-get upgrade
Apt Remove
Removing a package is quite simple. However, this will not remove configuration files, so if you were to re-install it they would be preserved. You would use apt-get purge ruby2.3*
to purge all files.
sudo apt-get remove ruby2.3
Apt Lock Error
If you get an error such as Unable to lock the administration directory (/var/lib/dpkg/) is another process
, follow these steps:
- Make sure you are not logged in as another user running apt
- Delete the lock and archive file files:
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo dpkg --configure -a
- If nothing works, attempt rebooting the server
Listing and Navigating
; Navigating
ls (list files)
ls -la (list all files, permissions, and hidden too)
pwd (print working directory)
cd .. (go down a directory)
cd / (go to lowest level)
cd ~ (go to logged in user's home)
cd /var/www (go to absolute path)
; Manage Files and Folders
mkdir <dir> (create a directory)
rmdir <dir> (remove an empty directory)
rm -rf <dir> (remove a directory with contents)
touch <file> (create an empty file)
mv <file> <loc> (move file from location to new location)
cp <file> <loc> (copy file from location to new location)
mv <dir> <loc> (move directory from location to new location)
cp -R <dir> <loc> (copy directory from location to new location)
; Reading Files
cat <file> (read entire file)
head <file> (read top of a file)
head <file> -n 20 (read top of file 20 lines)
tail <file> (read bottom of a file)
tail <file> -n 20 (read bottom of file 20 lines)
tail <file> -f (stream file as it's updated, eg: an error log)
Users
su - username (switch users)
sudo su (switch to root)
passwd (change logged in users password)
passwd username (change another users password)
useradd -m -s /bin/bash username
usermod -a -G existing_group existing_user
who (show all logged in users)
whoami (show which user you are)
Groups
Do not delete groups you don't know what they are used for, that's dangerous!
groups (see what groups current user belongs to)
groupadd name (create a group)
groupadd -g 900 name (create a group with custom GroupID aka gid)
groupdel name (delete a group)
useradd <group> (add current user to a group)
usermod -aG <group> <user> (append any user to an additional group)
cat /etc/group (list all groups)
cut -d: -f1 /etc/group (list all groups, cleaner)
Permissions
There are two ways to manage permissions, one is by text the other is by an octal value.
Easy Permissions
; Change Mode
; Options: (O)wner (U)sers (G)roup or (A)ll
; File: Owner: rwx, Group: rwx, User: rwx
; Misc: Besides rwx there is:
; s = setuid of owner for old/new files
; Single File read/write permissions
chmod g+rw file
chmod og+rw file.txt
; Change Ownership
chown user:group files_or_folder
chgrp group files_or_folder
; Recursively:
chown -R user:group files_or_folder
chgrp -R group files_or_folder
chmod -R og+rw files_or_folder
chmod -R g+s files_or_folder
Preserve Group Permissions
A fantastic way to structure your users is within groups. A common example would be your www-data
group.
If I have a user jesse
, I can add him with sudo usermod -aG www-data jesse
.
After adding any users I would like, I want to have a folder where all the members of the www-data
group
can read/write a folder. If they are using git, I also want the permissions to stay the same, meaning if they
pull the permissions will not change.
To accomplish this, here is an example:
sudo chown -R deploy:www-data /var/www
sudo chmod -R g+rws /var/www
The g+s
sets the file(s)/folder(s) a gid (setgid
) so that new files will inherit the original group!
Octal Permissions
You may have seen this a lot, you can use octal or decimal (begins with a 0) to do the same thing.
Permissions:
0 = None
1 = Execute (e)
2 = Write (w)
4 = Read (r)
- There are 3 Permission types (Read, Write, Execute), or 4 if you count "None".
- There are 3 Sets: Owner/User/Group (In that order)
- So if you did
chmod 700 file.txt
it would allow the user to Read, Write and Execute- Because
7
is the total of4 + 2 + 1
- Because
Octal Examples
chmod 600 file.txt – Owner Read, Write
chmod 660 file.txt – Owner Read, Write; User Read, Write
chmod 770 file.txt – Owner Read, Write, Execute
chmod 770 file.txt – Owner Read, Write, Execute; User Read, Write, Execute
chmod 666 file.txt – All Read, Write
chmod 777 file.txt – All Read, Write, Execute
OS Details
Get fundamental information about your OS with the following commands, you may have to run them as sudo
, eg: sudo lsb_release -a
.
Operating System
lsb_release
lsb_release -a
lsb_release -as # Short Information
lsb_release --help
CPU Info
nproc # How many Processing Units
cpuid # Must install cpuid from terminal
cat /proc/cpuinfo # Lots of info
Usage Info
free -h # Human readable, or do --help for options
vmstat -s
cat /proc/meminfo # Lots of info
Disk Space
df
df -B MB (In Megabtyes, KB for Kilobytes, GB for Gigabytes)
System Processes
top
htop # If you installed it
IP Address
Your IP is after inet addr
. If you are connect via ethernet it's under eth0 (Ethernet)
otherwise, wirelessly it is likely under wlan0 (Wireless LAN)
.
ifconfig
ip
ip addr show
ip addr show wlan
ip addr show eth0
GUI Processes
gnome-system-monitor
CLI Processes
top
htop (My favorite, sudo apt-get install htop)
nmon
List all Keybindings
gsettings list-recursively org.gnome.desktop.wm.keybindings | sort | more
See Keypressed
xev
; Or for a lot of details:
xev | grep KeyPress
Kernal
The Kernal is the lowest level item that ties everything together from hardware to software. Without a kernal you cannot do anything on linux.
Remove Old Kernals
See What version you are currently using
sudo uname -a
See all the Kernals on the OS
sudo dpkg --get-selections | grep linux
The BYOBU is quite nice
sudo apt install byobu
sudo purge-old-kernels
OS Shutdown
shutdown
reboot
shutdown -h now
shutdown -h +10 (shutdown 10 mins)
shutdown -r now (reboot now)
Crontab
crontab -e (edit crontab for current user)
crontab -l (list crontab for other user)
crontab -u jesse -l (see crontabs for specific user)
Services
Service Commands
Use the service command (Requires sudo)
service ssh status (service status)
service --status-all (all services status)
Almost every service has the following commands, some may have more like apache graceful-restart
:
service servicename start
service servicename stop
service servicename restart
service servicename status
service servicename force-reload
Autostart
Add Service links:
sudo update-rc.d servicename defaults
Whether you get a warning if they already exist or not, enable it now:
sudo update-rc.d servicename enable
Remove Autostart
Pass the Force flag
sudo update-rc.d -f servicename remove
Autostart Daemons
There is are several startup popular daemons:
- CentOS uses SystemV
- Ubuntu 14 uses Upstart
- Ubuntu 14.10+ uses SystemD (15, 16, 17..)
Focus on SystemD.
SystemD Commands
This would only apply to Ubuntu 14.10+, otherwise you would use Upstart.
systemctl <-- You'll use this more often
journalctl <-- You'll use this more often
update-rc.d <-- You'll use this more often
--------------------------
Installs/Removes System-V style init script links
Note: System-V Style, but it's really SystemD. (Confusing huh?)
"NNname" is the runlevel, lower means startup sooner
----------------------------------------------------
The Location is: /etc/rcrunlevel.d/NNname
The Target is: /etc/init.d/name.
notify
analyze
cgis
cgtop
loginctl
nspawn
System State
uname -a (get linux info)
top (See running processes/system status, I suggest installing `htop`)
top -u www-data
htop -u www-data
df (display disk space in bytes, default)
df -h (display disk space human readable)
df -Th (display disk space with partitions)
free (see memory used)
free -g (in gigabytes)
Processes
ps -ef | more (current running processes)
ps -efH | more (current running processes in a tree)
ps -ef | grep vim (find vim process id)
kill -9 <id> (no brackets)
Bash
Bash is my shell of choice, which is why I have a .bashrc
file.
Bash Paths
Executables and commands are automatically in the path, see your path with:
echo $PATH
Add to Path
# I suggest editing your ~/.profile
vim ~/.profile
if [ -d "/path/to/your/bin" ] ; then
PATH="$PATH:/path/to/your/bin"
fi
Note: Order of Linux Reading files: ~/.bash_profile, ~/.bash_login, and ~/.profile, so don't try to use a ~/.profile variable within ~/.bash_profile
Bash Completions
The locations for bash completio0ns can be found at:
cd /usr/share/bash-completion/completions.d/
cd /etc/bash_completions.d/