free software resistance
the cost of computing freedom is eternal vigilance
### tcoyc-poking-around-freebsd
*originally posted:* dec 2023
technically speaking, theres nothing the command line can do that a gui cannot. any feature you could find on the command line could technically be implemented as a gui too, but it doesnt work that way in practice.
with graphical recovery tools, this line is blurred further- sometimes a feature is only available or unlocked through a graphical console, though the old rule that everything is on the command line, some things are available on the gui still applies to most operating systems.
beyond practical reasons like text can be redirected to a serial port, or graphics libraries and systems are not always initialised, part of the reason for this is that writing to text-based stdout is just easier. to write to stdout you just write to stdout:
```
print("this writes to stdout")
```
```
echo "this writes to stdout"
```
to initialise the graphics library requires more resources, more work and every graphics library works slightly differently. the most sophisticated libraries are probably the most obtuse to use, so standardising on obtuse and tedious probably isnt better. at least some libraries are relatively easy to use. practically none are as little work as print though.
using graphical tools remotely also sucks- it is possible, with x-forwarding, which sucks, or vpn, which sucks, but its not impossible. text-based tools lend themselves really well to remote use.
they also lend themselves to automation. in shell scripts, most of the time when you add a text-based tool, its like adding another command to your programming language. shell scripts really arent a programming language- "commands" can come from anywhere, you can even make your own- but shell scripts work very similar to a programming language.
also, shell scripts tend to have some native commands. because shell scripts mostly run external programs but often have native commands, while programming languages can import third-party libraries and even run shell commands, the line between the two is possible but maybe not easy to distinguish.
as was noted in a previous chapter, when you install a new program, even a graphical one, it creates an executable file (a file you can run as a program) containing either binary code or a script that requires a compatible interpreter. for a python script or shell script, you must have a python interpreter installed or the particular shell that script uses.
if you have ever seen python "compiled" to an executable, what youve actually seen is an executable zip file containing the python code and a copy of the python interpreter.
because so much of a bsd system or a gnu/linux system is accessible from the command line, it is worth becoming more familiar with the command line if you want to really control your computing. a few exceptions to this are noted, but they are still exceptions. more command line familiarity is more power.
we will start off using openbsd to create a bootable usb with freebsd on it. although freebsd is more mainstream, i cant seem to get it to install the gui until after the installer for the entire system has run. openbsd includes the gui in its base installation. but we will install a gui in freebsd anyway. if you prefer, you might look up a video online of how to install the gui on freebsd. i am more familiar with openbsd.
because the giant 4gb dvd image didnt appear to include or install the gui, and i had to go online from freebsd to install it anyway, i dont recommend downloading the bigger image. instead, i recommend the "disc1" iso that you can download from freebsd.org:
=> https://freebsd.org
i have saved it as amd64-disc1.iso, so im going to write it to rsd8c like this as root:
```
dd if=amd64-disc1.iso of=/dev/rsd8c bs=1M
```
okay thats a lie, i actually used /dev/rsd1c because thats where the usb is. but this way you have to pay more attention and you likely dont have an rsd8c, so this is safer if youre not paying attention.
dd is jokingly nicknamed "destroy disk", because it can write over an entire disk including the partition. you really want to be certain that you are pointing "of=" to the right device, because if you point it to a wrong one and the wrong one exists, you now have a bootable image of the freebsd installer there- no matter what you had there before.
the openbsd version of dd gives you no warnings, even if you are writing to a mounted partition. it will simply start overwriting. using dd requires vigilance- backups are also good.
once you have "destroyed" the data on your usb with freebsd, you are ready to boot the installer. hopefully the settings on your laptop, desktop or server already allow this, otherwise you will need to poke around your firmware settings too. sometimes theres a key you can press for a boot menu without changing your settings, but it varies from machine to machine.
you can pause the boot menu by hitting space, but before youre able the freebsd menu will probably have selected the installer for you. thats okay, youll get a text dialogue that says "welcome to freebsd!"
when you see this, you can use the arrow keys to select "live cd" or simply hit "install" to start the installer.
dual booting is fraught with complications, and basically never the best way to install an os the first time. you should be running the installer on a system that contains nothing you care about on it- and you should let freebsd have the entire system for the sake of simplicity.
so we hit enter on install, hit enter on default keymap unless your keyboard is different obviously, type a hostname- you can just type freebsd if you want, hit enter and when you get to the list of "optional system components" that even i dont really follow- just hit enter, because theres nothing extra we need right now and if we did, you would likely have to be online to get them.
you can choose auto (zfs) but i wont, because zfs has some rare and obscure bugs right now, and i dont have the obsession with zfs that so many people do because apparently its the best thing since subdirectories. thus im choosing ufs.
hit enter for entire partition. im also going to hit mbr just because its highlighted. you may want gpt.
hit enter to "finish" then again to "commit".
now the installer is "destroying" the drive on this machine, too.
the freebsd installer is not the worst, but its not my favourite. also if you have a wonky usb keyboard like i apparently do, the screen will become messy with kernel messages and it will be hard to tell what setting you have selected.
if youre confident you had the right option selected, you can press enter and the next screen will be cleaner. otherwise, you will see new options as you highlight them with the arrow keys.
the openbsd installer avoids this problem, it simply prints the next options on the screen without fancy dialogues or arrow key navigation. its not as pretty, but its also more reliable.
now youre being asked for a root password. this is the password you will type when you use the su command to become superuser- to become root. some people use sudo for that. hit enter when youve typed in a password. it wont show the password as you type. youll type it again to confirm.
bad news if the next dialogue doesnt show your network interface. even if it does, select cancel and hit enter.
is the machine set to utc? it really should be- hit yes. note that it will tell you to do the opposite if you dont know. only you can decide. it may seem trivial to you, but a number of things depend on a properly set clock so, yeah.
personally i always set my clock to utc, so here is where i scroll down to utc at number 11 and hit enter.
does the abbreviation "utc" look reasonable? yes, but this dialogue looks unnecessary. oh, the joys of the mainstream option... hitting enter.
skipping the date setting- enter. again to skip time setting.
i dont know about you, but im selecting moused and ntpd and ntpd sync on start. other options im leaving unchanged.
for this install im not selecting any hardening options. i might want to if im actually using this system though im tired of this installer. enter.
add users? yes.
if you dont want to think of a username, just use "u".
hit enter until it says "enter password"- then enter a password that is not the same as your root password.
hit enter to not lock out the account.
hit enter to say ok.
hit enter to not add more.
hit enter to apply configuration and quit.
this installer has so many steps that by the time freebsd 15 is released, the steps here will probably be obsolete.
hit enter again.
select shutdown, hit enter. once its off you can remove the usb. attach a network cable if you have one, then turn the machine on.
login as root, by typing root, hitting enter and then typing your password. if you selected ntpd and you cant get this online its going to be annoying. you can try stopping this like so:
```
service ntpd onestop
```
run ifconfig by typing it and hitting enter. if you see "lo0" thats not a useful section to you right now, youre looking for a network interface other than lo0. we will call that "nic" for network interface, but you should call it whatever the other interface is.
if it exists it will be on the left, like lo0 is, and it will be followed by a colon, like lo0 is. be sure that "nic" is the interface name WITHOUT the colon- in other words, dont type the colon when you use the nic in a moment:
heres a trick to find out what the dhcp client is on your distro or on bsd, type "dh" and hit tab. unsurprisingly, freebsd has dhclient. so type this and hit enter:
```
dhclient nic
```
where nic is the network interface you got from ifconfig. if applicable.
it immediately tells me the network is down. it is not. it then tells me it is bound to an address. good. if it tells you that too, we can install additional things- like a gui.
freebsd uses pkg, which is a pretty decent and intuitive package installer- i mean compared to others. but honestly, i dont hate it.
just to see if its working, lets try a simple one:
```
pkg install pkg_tree
```
pkg now offers to install itself? dont try to question it. just hit "y" and enter.
61m to install perl5 and pkg_tree. what the hell was in that 1gb we downloaded? freebsd is a mystery to me, this seems pretty unusual for a base installer. the default is no so we must hit y, then enter.
it also installs pkg_tree, which is nice. it shows you a tree of every installed package and the packages each package depends on.
lets install some other things, like tor browser:
```
pkg install tor-browser
```
it says it will download 191m and also bring in python39, parts of xorg and wayland.
hit y and enter?
you would think it would be satisfied now, but when we try to run startx it isnt installed. okay, lets try:
```
pkg install xorg
```
another 197m to download, okay. its really nice that openbsd includes xorg in the base install- their version is called xenocara, but it still has traditional X11 pathnames, etc.
you dont want xorg without a better window manager, so lets also install:
```
pkg install dwm icewm xterm
```
a funny thing about xorg, you should never run it as root- and it doesnt like to run as anything else. normally what you do is setup a dm, and openbsd includes xenodm for this purpose.
we could install lightdm, then some greeter, but i only tried to install xorg in freebsd the other day, tried adding lightdm so i could run xorg properly, and for the purpose of this chapter we are simply going to break the rules and run startx directly.
you shouldnt do this, and you shouldnt consider it secure. but for the purpose of poking around with freebsd it should be fine.
startx lets you specify a window manager. we installed dwm because it has roughly zero dependencies and once you get used to letting dwm take care of window sizing for you, you probably wont want to go back to a "normal"- that is, stacking, window manager.
however, being realistic, icewm has been installed because its very intuitive and familiar to those who got by with older versions of windows. both are very fast options- icewm requires imlib2 which has no maintainer, so hopefully freebsd will not drop icewm. either way i prefer dwm not only because i like the way it tiles, but because icewm is github-based. someone fork it, please.
we cant run startx like this:
```
startx icewm-session
```
because it doesnt check the path for the window manager. we can find the location of icewm-session this way:
```
which icewm-session
```
it tells us /usr/local/bin/icewm-session, and if we ran startx with that, it would work. instead, heres a shortcut:
```
startx $(which icewm-session)
```
the $() runs the commands in the parentheses first, and the output of "which icewm-session" is /usr/local/bin/icewm-session, so its as if we said startx /usr/local/bin/icewm-session. do it the way you prefer.
xorg should run as root now, and hitting ctrl-esc should open the "bsd" menu so you can open programs. i recommend starting xterm.
you can run tor-browser as root but you REALLY really shouldnt, so type "su u" or whatever user you setup during installation to become that user. because you are doing this from root, you dont need to type your password.
lets try opening tor browser:
```
tor-browser
```
because we started xorg as root, we are getting an error that it cant open the display. programs run as root can, but we really dont want to do that with the browser.
so hit ctrl-d to leave the user login and go back to being root, then type:
```
xhost +local:root
```
normally you would use that command to run graphical programs AS ROOT, if it complained when you had started xorg the way you should. fortunately it also lets you run graphical programs "as user", if you started xorg as root.
become "user" again by typing "su u" or su whatever user you setup before. now you can run tor-browser.
if you run a graphical program from xterm this way, it will take over xterm to use for outputting certain information that can be hypothetically useful to someone. if you add an ampersand like this:
```
tor-browser &
```
it will let you go back to root when you hit ctrl-d and close xterm when you hit ctrl-d again. this also works:
```
tor-browser &exit
```
but since you were root before you became user, &exit will only drop the user login, you will still have to tell it to close the term.
if this seems like a lot of arcane stuff you wont ever use, dont worry about that- you will get used to these things if you do use them, and if you dont need them you wont get used to them since you wont do them.
now that xorg is working and we installed a graphical program, we are actually going to go back to the command line. hit ctrl-alt-f1 and it should switch to the screen- called a "vt" or virtual term- that we ran startx from.
because we DIDNT use &, we can now stop xorg and every program xorg is running by hitting ctrl-c. note that this tells xorg to quit without caring what you do or dont have saved- including your browser session.
closing xorg this way will get you a message that says "xinit: unexpected signal 2". the "signal 2" is what ctrl-c sent to xorg.
since installing tor browser also installed python3, we could start python now by typing "python3" and hitting tab to complete the filename. python3 is needed by many programs, but i consider python3 unstable- they keep breaking features and making people change their programs, like rust does.
since i have no respect for that, i prefer pypy. unlike python2, pypy is maintained and updated- but they dont break existing compatibility with python2 programs. if you want a stable version of python, this is what i recommend.
unfortunately, pypy is not available as a package for freebsd. if you find it in the ports tree perhaps you can compile it, and for now python2 (which definitely isnt maintained, it even has some security vulnerabilities) is available in freebsd:
```
pkg install py27-tkinter
```
will install both python2 and a version of pytk that works with it. 16m more to download.
you will notice that the python2 port in freebsd is deprecated, and that it was scheduled to be removed "on or after" three years ago.
there are other nice things about pypy besides its support of both python2 and python3. there are precompiled binaries for windows, macos and gnu/linux, its a shame that pypy doesnt have a precompiled binary for freebsd or better yet, openbsd.
anyway:
```
cd /usr/local/bin
ln -s python2.7 python2
```
that will make it so we dont have to keep tab-completing python2.7 to run python. now we can say:
```
python2
```
this prompt is a bit different- we have a shell, technically- but the environment is python, rather than sh. we also have a prompt that says ">>> " for python instead of "# " for root or "$ " for user.
we could have also run a python script like this:
```
python2 name-of-script.py
```
that works even if the program isnt named something.py.
if "which python2.7" gives you /usr/local/bin/python2.7 you can also add this to the top of your python script:
```
#!/usr/local/bin/python2.7
# coding: utf-8
```
and then:
```
chmod +x name-of-script
```
and now your python script will run like a standalone program- though really it still needs python2.7, you just wont need to say python2 anymore.
but whats some code we can run from python? type in the following:
```
from os import system
# just input() for python 3
while 1: dosomething = raw_input() ; n = system(dosomething)
```
when it replies: "..." hit enter; that would let you type more lines of python that are part of the while loop.
now you have a shell program, running from the python shell, running from the shell. type pkg_tree and hit enter- it will run the pkg_tree program from python.
if you type "ls", it will list the files in that folder. if you type:
```
ls -l
```
it will list files with information about permissions, links, user, group, size, and the date the file was modified, with the name.
and if you do this:
```
find / -type f | cat -n
```
it will list every file in every folder, and "pipe" that information to cat -n, which will add a count to the left side.
whatever number it shows, you should get a similar number like this:
```
find / -type f | wc -l
```
only now it just gives you the count of files on the system. with everything installed so far, its still fewer than 81,000 files, including system files and devices. but thats in december 2023, and next year they will probably add more.
hit ctrl-c to stop the program loop.
try this:
```
from os import popen
```
theres a more complicated way to do this in python3, for people who like it when a beginner-friendly language becomes a corporate-friendly behemoth for the sake of google workers.
what? no, this book isnt biased at all, why do you ask?
```
dosomething = popen('find / -type d')
```
nothing happens. system() will simply run the program and output will go to the python shell- if you mix print() statements (or functions) with system() output, the output may be unpredictable in the order things show up on the screen- print statements (or functions) may race against programs run from the shell and win.
if you want to run a gui program from python, system() is good for that. you can use it for text programs too, if you arent going to mix that with print statements.
but if you use popen(), instead of going to the screen the output will go to an array. then not only can python "read" the program output from the shell, it can print it using print statements- which wont race against other print statements.
it also means python wont do anything else until the output of the program running from the shell is complete.
```
dosomething = dosomething.readlines()
print len(dosomething)
```
that will give you the number of "directories", or folders that the find command got.
```
print dosomething[-5:]
```
that will give you the last 5 lines read from popen(), if you already used the readlines() function.
```
for each in dosomething: print each.rstrip()
```
will print every line you got from the most recent use of popen().
python can do a lot more than just run a shell from a shell; if you installed tkinter you can create gui programs using python and tk. tk is one of the simplest gui toolkits you can use, and the official python ide was written using tk.
youll probably think its ugly, but people rarely try to theme it even though you can.
try this:
```
help(popen)
```
it will give you some very terse but potentially useful information about popen().
hit ctrl-d to leave python.
try this:
```
find / -type f | grep python | cat -n
```
note you can use the up arrow to find and edit (and run) previously typed in commands.
this one counts and displays every file with "python" in the name of the file- or in any of the folders the file is in.
```
find / -type f | grep "\.txt" | cat -n
```
the same except files with ".txt" in them. the "\." is because "." is a wildcard to grep; \. tells it to look for the actual dot.
```
man find
```
gives you the manual page for the find command. sometimes man pages are less terse than help() in python. but help() is for python things and man is for shell things. you can do man python2.7 though.
```
man find | grep exec
```
displays every line of the man page for find that has the string "exec" in it.
```
find / -type f -name '*.txt' -exec wc -l {} +
```
find every file ending in ".txt" and do a line count on each of those files.
```
find / -type f -name '*.txt' -exec wc -c {} + | sort -n
```
do file size instead of line count and sort by size.
```
find / -type f -name '*.txt' -exec wc -c {} + | sort -n | less
```
less is a pager, by piping text to it you can page through the output using arrow keys or pgup and pgdn. a few gui text editors will let you pipe text to them too. jed can be used as a pager this way, then it can edit the results.
the man page for less shows that it has betrayed the free software world by remaining on github, even though its a gnu program.
microsoft gnu! sadly this is not the only gnu program that is a hostage of microsoft. gnu was more principled years ago.
openbsd actually has a fork of less, so they arent using the github version. that isnt the reason they forked, but it did result in a non-github version of less.
many command line tutorials exist, ive written more comprehensive ones than this- but they were not for bsd and they were not focused on finding your way around the system- they were more general than that.
the commands demonstrated here can help you manage packages, manage files, get started coding in python or possibly inspire you to learn more about the shell in freebsd.
this was going to also cover openbsd, but instead that will be saved for the next chapter.
check the battery:
```
apm
```
you can:
```
reboot
```
or:
```
shutdown -p now
```
license: 0-clause bsd
```
# 2019, 2020, 2021, 2022, 2023
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
```
=> https://freesoftwareresistance.neocities.org