Linux cheat sheet
DETAILS: Tier: Free, Premium, Ultimate Offering: GitLab Self-Managed
This is the GitLab Support Team's collection of information regarding Linux, that they sometimes use while troubleshooting. It is listed here for transparency, and for users with experience with Linux. If you are currently having an issue with GitLab, you may want to check your support options first, before attempting to use this information.
WARNING:
It is beyond the scope of GitLab Support to assist in systems administration. GitLab administrators are expected to know these commands for their distribution
of choice. If you are a GitLab Support Engineer, consider this a cross-reference to
translate yum
-> apt-get
and the like.
Most of the commands below have not been labeled as to which distribution they work on. Contributions are welcome to help add them.
System Commands
Distribution Information
# Debian/Ubuntu
uname -a
lsb_release -a
# CentOS/RedHat
cat /etc/centos-release
cat /etc/redhat-release
# This will provide a lot more information
cat /etc/os-release
Shut down or Reboot
shutdown -h now
reboot
Permissions
# change the user:group ownership of a file/dir
chown root:git <file_or_dir>
# make a file executable
chmod u+x <file>
Files and directories
# create a new directory and all subdirectories
mkdir -p dir/dir2/dir3
# Send a command's output to file.txt, no STDOUT
ls > file.txt
# Send a command's output to file.txt AND see it in STDOUT
ls | tee /tmp/file.txt
# Search and Replace within a file
sed -i 's/original-text/new-text/g' <filename>
See all set environment variables
env
Searching
Filenames
# search for a file in a filesystem
find . -name 'filename.rb' -print
# locate a file
locate <filename>
# see command history
history
# search CLI history
<ctrl>-R
File contents
# -B/A = show 2 lines before/after search_term
grep -B 2 -A 2 search_term <filename>
# -<number> shows both before and after
grep -2 search_term <filename>
# Search on all files in directory (recursively)
grep -r search_term <directory>
# Grep namespace/project/name of a GitLab repository
grep 'fullpath' /var/opt/gitlab/git-data/repositories/@hashed/<repo hash>/.git/config
# search through *.gz files is the same except with zgrep
zgrep search_term <filename>
# Fast grep printing lines containing a string pattern
fgrep -R string_pattern <filename or directory>
CLI
# View command history
history
# Run last command that started with 'his' (3 letters min)
!his
# Search through command history
<ctrl>-R
# Execute last command with sudo
sudo !!
Managing resources
Memory, Disk, & CPU usage
# disk space info. The '-h' gives the data in human-readable values
df -h
# size of each file/dir and its contents in the current dir
du -hd 1
# or alternative
du -h --max-depth=1
# find files greater than certain size(k, M, G) and list them in order
# get rid of the + for exact, - for less than
find / -type f -size +100M -print0 | xargs -0 du -hs | sort -h
# Find free memory on a system
free -m
# Find what processes are using memory/CPU and organize by it
# Load average is 1/CPU for 1, 5, and 15 minutes
top -o %MEM
top -o %CPU
Strace
# strace a process
strace -tt -T -f -y -yy -s 1024 -p <pid>
# -tt print timestamps with microsecond accuracy
# -T print the time spent in each syscall
# -f also trace any child processes that forked
# -y print the path associated with file handles
# -yy print socket and device file handle details
# -s max string length to print for an event
# -o output file
# run strace on all puma processes
ps auwx | grep puma | awk '{ print " -p " $2}' | xargs strace -tt -T -f -y -yy -s 1024 -o /tmp/puma.txt
Be aware that strace can have major impacts to system performance when it is running.
Strace Resources
- See the strace zine for a quick walkthrough.
- Brendan Gregg has a more detailed explanation of how to use strace.
- We have a series of GitLab Unfiltered videos on using strace to understand GitLab.
The Strace Parser tool
Our strace-parser tool can be used to
provide a high level summary of the strace
output. It is similar to strace -C
,
but provides much more detailed statistics.
MacOS and Linux binaries are available, or you can build it from source if you have the Rust compiler.
How to use the tool
First run the tool with summary
flag to get a summary of the top processes sorted by time spent actively performing tasks.
You can also sort based on total time, # of system calls made, PID #, and # of child processes
using the -s
or --sort
flag. The number of results defaults to 25 processes, but
can be changed using the -c
/--count
option. See --help
for full details.
shutdown -h now
reboot
```0
Based on the summary, you can then view the details of system calls made by one or more
processes using the `-p`/`--pid` for a specific process, or `-s`/`--stats` flags for
a sorted list. `--stats` takes the same sorting and count options as summary.
```shell
shutdown -h now
reboot
```1
In the example above, we can see which files took longer to open for `PID 16815`.
When nothing stands out in the results, a good way to get more context is to run `strace`
on your own GitLab instance while performing the action performed by the customer,
then compare summaries of both results and dive into the differences.
#### Stats for the open syscall
Rough numbers for calls to `open` and `openat` (used to access files) on various configurations.
Slow storage can cause the dreaded `DeadlineExceeded` error in Gitaly.
Also [see this entry](../operations/filesystem_benchmarking.md)
in the handbook for quick tests customers can perform to check their file system performance.
Keep in mind that timing information from `strace` is often somewhat inaccurate, so
small differences should not be considered significant.
|Setup | access times |
|:--------------|:--------------|
| EFS | 10 - 30 ms |
| Local Storage | 0.01 - 1 ms |
## Networking
### Ports
```shell
shutdown -h now
reboot
```2
### Internet/DNS
```shell
shutdown -h now
reboot
```3
## Package Management
```shell
shutdown -h now
reboot
```4
## Logs
```shell
shutdown -h now
reboot
```5